|
6 | 6 | import android.graphics.Bitmap; |
7 | 7 | import android.graphics.BitmapFactory; |
8 | 8 | import android.graphics.PorterDuff; |
| 9 | +import android.graphics.Typeface; |
9 | 10 | import android.graphics.drawable.BitmapDrawable; |
10 | 11 | import android.graphics.drawable.Drawable; |
11 | 12 | import android.text.Editable; |
|
16 | 17 | import android.text.TextUtils; |
17 | 18 | import android.text.TextWatcher; |
18 | 19 | import android.text.style.ImageSpan; |
| 20 | +import android.util.DisplayMetrics; |
19 | 21 | import android.util.Log; |
20 | 22 | import android.view.Gravity; |
21 | 23 | import android.view.inputmethod.InputMethodManager; |
|
31 | 33 | import com.facebook.react.bridge.WritableMap; |
32 | 34 | import com.facebook.react.uimanager.Spacing; |
33 | 35 | import com.facebook.react.uimanager.events.RCTEventEmitter; |
| 36 | +import com.facebook.react.views.text.ReactFontManager; |
34 | 37 | import com.variabletextinput.R; |
35 | 38 | import com.variabletextinput.bean.RichTextBean; |
36 | 39 | import com.variabletextinput.util.ActivityConst; |
@@ -61,7 +64,7 @@ public VariableTextInput(Context context) { |
61 | 64 | editText.setLayoutParams(new ScrollView.LayoutParams(MATCH_PARENT, WRAP_CONTENT)); |
62 | 65 | editText.setGravity(Gravity.TOP); |
63 | 66 | editText.setInputType(editText.getInputType() | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT | InputType.TYPE_TEXT_FLAG_MULTI_LINE); |
64 | | - |
| 67 | + editText.setPadding(0,0,0,0); |
65 | 68 | // This was used in conjunction with setting raw input type for selecting lock notes. |
66 | 69 | // However, it causes the keyboard to not come up for editing existing notes. |
67 | 70 | // Tested while offline using brand new installation on Android 6 emulator, but a user with Android 7 also reported it. |
@@ -130,19 +133,26 @@ public void afterTextChanged(Editable s) { |
130 | 133 | // ... |
131 | 134 | oldHeight = newHeight; // 更新旧的高度 |
132 | 135 | WritableMap contentSize = Arguments.createMap(); |
133 | | - contentSize.putDouble("height", newHeight * 4 / 11); |
134 | | - contentSize.putDouble("width", editText.getWidth() * 4 / 11); |
| 136 | + int pdHeight = pxToDp(newHeight); |
| 137 | + int pdWidth = pxToDp(editText.getWidth()); |
| 138 | + contentSize.putDouble("height", pdHeight); |
| 139 | + contentSize.putDouble("width", pdWidth); |
135 | 140 | WritableMap event = Arguments.createMap(); |
136 | 141 | event.putMap("contentSize", contentSize); |
137 | 142 | final Context context = getContext(); |
138 | 143 | if (context instanceof ReactContext) { |
| 144 | + Log.d("输入框高度", "afterTextChanged: "+event); |
139 | 145 | ((ReactContext) context).getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onAndroidContentSizeChange", event); |
140 | 146 | } |
141 | 147 | } |
142 | 148 | } |
143 | 149 | }); |
144 | 150 | } |
145 | | - |
| 151 | + public int pxToDp(int px) { |
| 152 | + DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics(); |
| 153 | + int dpi = displayMetrics.densityDpi; |
| 154 | + return Math.round(px / (dpi / 160f)); |
| 155 | + } |
146 | 156 | @Override |
147 | 157 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
148 | 158 | super.onLayout(changed, left, top, right, bottom); |
@@ -307,7 +317,17 @@ public void setPlaceholder(String placeholder) { |
307 | 317 | public void setUnderLineColorAndroid(Integer color) { |
308 | 318 | editText.setBackgroundTintList(ColorStateList.valueOf(color)); |
309 | 319 | } |
310 | | - |
| 320 | + public void setFontSize(Integer fontSize){ |
| 321 | + editText.setTextSize(fontSize); |
| 322 | + } |
| 323 | + public void setFontFamily(String fontFamily){ |
| 324 | + int style = Typeface.NORMAL; |
| 325 | + if (editText.getTypeface() != null){ |
| 326 | + style = editText.getTypeface().getStyle(); |
| 327 | + } |
| 328 | + Typeface newTypeFace = ReactFontManager.getInstance().getTypeface(fontFamily,style,editText.getContext().getAssets()); |
| 329 | + editText.setTypeface(newTypeFace); |
| 330 | + } |
311 | 331 | public void insertImage(String imagePath) { |
312 | 332 | Bitmap bitmap = BitmapFactory.decodeFile(imagePath); |
313 | 333 | Drawable drawable = new BitmapDrawable(getResources(), bitmap); |
|
0 commit comments