Skip to content

Commit 6214c32

Browse files
duom青源duom青源
authored andcommitted
feat: add android fontSize&&fontFamily props
1 parent 0314c75 commit 6214c32

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

android/src/main/java/com/variabletextinput/VariableTextInputViewManager.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,18 @@ public void setColor(VariableTextInput view, @Nullable Integer color) {
6363
view.setTextColor(color);
6464
}
6565
}
66-
66+
@ReactProp(name = ViewProps.FONT_SIZE, customType = "FontColor")
67+
public void setFontSize(VariableTextInput view, @Nullable Integer fontSize) {
68+
if (fontSize != null) {
69+
view.setFontSize(fontSize);
70+
}
71+
}
72+
@ReactProp(name =ViewProps.FONT_FAMILY,customType = "FontFamily")
73+
public void setFontFontFamily(VariableTextInput view,@Nullable String fontFamily){
74+
if (fontFamily != null){
75+
view.setFontFamily(fontFamily);
76+
}
77+
}
6778
@ReactProp(name = "selectionColor", customType = "Color")
6879
public void setSelectionColor(VariableTextInput view, @Nullable Integer color) {
6980
if (color != null) {

android/src/main/java/com/variabletextinput/view/VariableTextInput.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.graphics.Bitmap;
77
import android.graphics.BitmapFactory;
88
import android.graphics.PorterDuff;
9+
import android.graphics.Typeface;
910
import android.graphics.drawable.BitmapDrawable;
1011
import android.graphics.drawable.Drawable;
1112
import android.text.Editable;
@@ -16,6 +17,7 @@
1617
import android.text.TextUtils;
1718
import android.text.TextWatcher;
1819
import android.text.style.ImageSpan;
20+
import android.util.DisplayMetrics;
1921
import android.util.Log;
2022
import android.view.Gravity;
2123
import android.view.inputmethod.InputMethodManager;
@@ -31,6 +33,7 @@
3133
import com.facebook.react.bridge.WritableMap;
3234
import com.facebook.react.uimanager.Spacing;
3335
import com.facebook.react.uimanager.events.RCTEventEmitter;
36+
import com.facebook.react.views.text.ReactFontManager;
3437
import com.variabletextinput.R;
3538
import com.variabletextinput.bean.RichTextBean;
3639
import com.variabletextinput.util.ActivityConst;
@@ -61,7 +64,7 @@ public VariableTextInput(Context context) {
6164
editText.setLayoutParams(new ScrollView.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
6265
editText.setGravity(Gravity.TOP);
6366
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);
6568
// This was used in conjunction with setting raw input type for selecting lock notes.
6669
// However, it causes the keyboard to not come up for editing existing notes.
6770
// 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) {
130133
// ...
131134
oldHeight = newHeight; // 更新旧的高度
132135
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);
135140
WritableMap event = Arguments.createMap();
136141
event.putMap("contentSize", contentSize);
137142
final Context context = getContext();
138143
if (context instanceof ReactContext) {
144+
Log.d("输入框高度", "afterTextChanged: "+event);
139145
((ReactContext) context).getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onAndroidContentSizeChange", event);
140146
}
141147
}
142148
}
143149
});
144150
}
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+
}
146156
@Override
147157
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
148158
super.onLayout(changed, left, top, right, bottom);
@@ -307,7 +317,17 @@ public void setPlaceholder(String placeholder) {
307317
public void setUnderLineColorAndroid(Integer color) {
308318
editText.setBackgroundTintList(ColorStateList.valueOf(color));
309319
}
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+
}
311331
public void insertImage(String imagePath) {
312332
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
313333
Drawable drawable = new BitmapDrawable(getResources(), bitmap);

example/src/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ const styles = StyleSheet.create({
113113
box: {
114114
backgroundColor: 'blue',
115115
color: '#fff',
116-
fontSize: 10,
116+
fontSize: 18,
117117
width: '100%',
118118
minHeight: 100,
119-
padding: 0,
120119
},
121120
});

src/index.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,28 @@ const VariableTextInputView = forwardRef(
162162
const onContentSizeChange = (event: any) => {
163163
const { style } = props;
164164
const styles = StyleSheet.flatten(style);
165-
if (!styles.height && styles.flex !== 1) {
165+
if (styles.height === undefined && styles.flex !== 1) {
166166
const contentSizeHeight = event.nativeEvent.contentSize.height;
167167
if (!!styles.maxHeight && contentSizeHeight > styles.maxHeight) {
168168
return;
169169
}
170+
if (!!styles.minHeight && contentSizeHeight < styles.minHeight) {
171+
return;
172+
}
170173
setCurrentHeight(event.nativeEvent.contentSize.height);
171174
}
172175
};
173176
const onAndroidContentSizeChange = (event: any) => {
174177
const { style } = props;
175178
const styles = StyleSheet.flatten(style);
176-
if (!styles.height && styles.flex !== 1) {
179+
if (styles.height === undefined && styles.flex !== 1) {
177180
const contentSizeHeight = event.nativeEvent.contentSize.height;
178181
if (!!styles.maxHeight && contentSizeHeight > styles.maxHeight) {
179182
return;
180183
}
184+
if (!!styles.minHeight && contentSizeHeight < styles.minHeight) {
185+
return;
186+
}
181187
setCurrentHeight(event.nativeEvent.contentSize.height);
182188
}
183189
};
@@ -196,17 +202,17 @@ const VariableTextInputView = forwardRef(
196202
props.onChangeText && props.onChangeText(e.nativeEvent.text);
197203
props.onChange && props.onChange(e);
198204
};
199-
const style = StyleSheet.flatten([{ height: currentHeight }, props.style]);
205+
const style = StyleSheet.flatten([props.style, { height: currentHeight }]);
200206
return (
201207
<RNTVariableTextInputView
202208
ref={nativeRef}
203209
onChange={_onChange}
204-
style={style}
205210
text={props.value}
206211
onContentSizeChange={onContentSizeChange}
207212
onAndroidChange={onAndroidChange}
208213
onAndroidContentSizeChange={onAndroidContentSizeChange}
209214
{...props}
215+
style={style}
210216
/>
211217
);
212218
}

0 commit comments

Comments
 (0)