You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by GitBox <gi...@apache.org> on 2018/10/24 09:55:26 UTC

[GitHub] YorkShen closed pull request #1336: [android] weex custom font not rendered right when font is first down…

YorkShen closed pull request #1336: [android] weex custom font not rendered right when font is first down…
URL: https://github.com/apache/incubator-weex/pull/1336
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/WXText.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/WXText.java
index 5376d12946..98928139ed 100644
--- a/android/sdk/src/main/java/com/taobao/weex/ui/component/WXText.java
+++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/WXText.java
@@ -45,10 +45,15 @@
 import android.text.style.AbsoluteSizeSpan;
 import android.text.style.AlignmentSpan;
 import android.text.style.ForegroundColorSpan;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.FrameLayout;
+
 import com.taobao.weex.WXEnvironment;
 import com.taobao.weex.WXSDKInstance;
 import com.taobao.weex.annotation.Component;
 import com.taobao.weex.common.Constants;
+import com.taobao.weex.dom.CSSConstants;
 import com.taobao.weex.dom.TextDecorationSpan;
 import com.taobao.weex.dom.WXAttr;
 import com.taobao.weex.dom.WXCustomStyleSpan;
@@ -247,6 +252,7 @@ public void onReceive(Context context, Intent intent) {
             WXLogUtils.w("WXText", "Layout not created");
           }
           hostView.invalidate();
+          resize();
         }
         WXLogUtils.d("WXText", "Font family " + fontFamily + " is available");
       }
@@ -254,4 +260,45 @@ public void onReceive(Context context, Intent intent) {
 
     LocalBroadcastManager.getInstance(WXEnvironment.getApplication()).registerReceiver(mTypefaceObserver, new IntentFilter(TypefaceUtil.ACTION_TYPE_FACE_AVAILABLE));
   }
+
+  public void resize() {
+
+    TextPaint textPaint = getHostView().getTextLayout().getPaint();
+    float newWidth = getTextWidth(getHostView().getText().toString(), getHostView().getText(), textPaint, Float.NaN, false);
+
+    View RealView = getHostView();
+    if (RealView == null) {
+      return;
+    }
+
+    ViewGroup.LayoutParams params = RealView.getLayoutParams();
+
+    if (params instanceof FrameLayout.LayoutParams && ((FrameLayout.LayoutParams) params).leftMargin != 0) {
+      ((FrameLayout.LayoutParams) params).leftMargin += (params.width - newWidth) / 2;
+    }
+    params.width = (int) newWidth;
+    RealView.setLayoutParams(params);
+
+  }
+
+  public float getTextWidth(String mText, CharSequence spanned, TextPaint textPaint, float outerWidth, boolean forceToDesired) {
+    if (mText == null) {
+      if (forceToDesired) {
+        return outerWidth;
+      }
+      return 0;
+    }
+    float textWidth;
+    if (forceToDesired) {
+      textWidth = outerWidth;
+    } else {
+      float desiredWidth = Layout.getDesiredWidth(spanned, textPaint);
+      if (CSSConstants.isUndefined(outerWidth) || desiredWidth < outerWidth) {
+        textWidth = desiredWidth;
+      } else {
+        textWidth = outerWidth;
+      }
+    }
+    return textWidth;
+  }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services