Skip to content

Commit 43e56f4

Browse files
[core] EditText - fix measure crash
> EditText customize fonts > EditText fix measure crash > Allow load direct font from resources
1 parent ba4ff63 commit 43e56f4

3 files changed

Lines changed: 50 additions & 15 deletions

File tree

core/res/ex-layout/main.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
<Button
1818
width="match_parent"
1919
height="wrap_content"
20+
text="Hello"
2021
margin="5dp"/>
2122
<Button
2223
width="match_parent"
2324
height="wrap_content"
25+
text="World"
2426
margin="5dp"/>
2527
<Button
2628
width="match_parent"
@@ -37,7 +39,7 @@
3739

3840

3941
<ScrollView
40-
width="256dp"
42+
width="400dp"
4143
height="match_parent">
4244
<EditText
4345
width="match_parent"

core/src/br/nullexcept/mux/res/Resources.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ public Typeface getFont(String family, int style) {
171171
if (family.toLowerCase().equals("default")) {
172172
family = "roboto";
173173
}
174-
XmlElement fonts = requestXml(fixPath(family, "font"));
174+
String path = fixPath(family, "font");
175+
if (Manager.exists(path+".ttf")) {
176+
return requestFont(path);
177+
}
178+
XmlElement fonts = requestXml(path);
175179
Typeface font = Typeface.DEFAULT;
176180

177181
boolean bold = (style & Typeface.STYLE_BOLD) != 0;

core/src/br/nullexcept/mux/widget/EditText.java

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import br.nullexcept.mux.view.View;
1717
import br.nullexcept.mux.view.ViewGroup;
1818

19+
import java.util.Arrays;
20+
import java.util.List;
21+
1922
public class EditText extends View {
2023
private final Paint paint = new Paint();
2124
private final Paint paintSelection = new Paint();
@@ -47,16 +50,36 @@ public EditText(Context context, AttributeList init) {
4750
super(context, init);
4851
setFocusable(true);
4952
setOnClickListener(view -> requestFocus());
50-
AttributeList list = initialAttributes();
51-
list.searchColorList(AttrList.textColor, this::setTextColor);
52-
list.searchDimension(AttrList.textSize, this::setTextSize);
53-
list.searchBoolean(AttrList.singleLine, this::setSingleLine);
54-
list.searchBoolean(AttrList.editable, this::setEditable);
55-
list.searchColorList(AttrList.selectionColor, this::setSelectionColor);
56-
list.searchText(AttrList.text, this::setText);
57-
list.searchText(AttrList.hint, this::setHint);
53+
AttributeList attrs = initialAttributes();
54+
attrs.searchColorList(AttrList.textColor, this::setTextColor);
55+
attrs.searchDimension(AttrList.textSize, this::setTextSize);
56+
attrs.searchBoolean(AttrList.singleLine, this::setSingleLine);
57+
attrs.searchBoolean(AttrList.editable, this::setEditable);
58+
attrs.searchColorList(AttrList.selectionColor, this::setSelectionColor);
59+
attrs.searchText(AttrList.text, this::setText);
60+
attrs.searchText(AttrList.hint, this::setHint);
61+
62+
attrs.searchColorList(AttrList.hintColor, this::setHintColor);
63+
64+
{ // Customize typeface
65+
String[] fontFamily = new String[]{"default"};
66+
int[] fontStyle = new int[1];
67+
attrs.searchRaw(AttrList.fontFamily, value -> fontFamily[0] = value);
68+
attrs.searchRaw(AttrList.textStyle, value -> {
69+
List<String> values = Arrays.asList(value.toLowerCase().split("\\|"));
70+
if (values.contains("italic")) {
71+
fontStyle[0] |= Typeface.STYLE_ITALIC;
72+
}
73+
if (values.contains("bold")) {
74+
fontStyle[0] |= Typeface.STYLE_BOLD;
75+
}
76+
});
5877

59-
list.searchColorList(AttrList.hintColor, this::setHintColor);
78+
Typeface font = context.getResources().getFont(fontFamily[0], fontStyle[0]);
79+
if (font != null) {
80+
setTypeface(font);
81+
}
82+
}
6083
}
6184

6285
public void setOnTextChangedListener(OnTextChangedListener textChangedListener) {
@@ -239,10 +262,12 @@ protected void onKeyEvent(KeyEvent keyEvent) {
239262
}break;
240263
case KeyEvent.KEY_V: {
241264
if (keyEvent.hasCtrl()) {
265+
String content = ((ClipboardApplet)getContext().getApplet(Context.CLIPBOARD_APPLET)).getContent();
266+
if (content == null) break;
242267
if (text.getSelection().length() > 0) {
243268
text.delete();
244269
}
245-
text.insert(((ClipboardApplet)getContext().getApplet(Context.CLIPBOARD_APPLET)).getContent());
270+
text.insert(preFormatText(content));
246271
changeText();
247272
}
248273
} break;
@@ -295,8 +320,12 @@ private void checkMeasure() {
295320
}
296321

297322
private void measureText(boolean force) {
298-
int mw = (int) (getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - paint.getFontMetrics().measureText(" "));
299-
int mh = (int) (getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - paint.getFontMetrics().measureText(" "));
323+
measureText(force, getMeasuredWidth(), getMeasuredHeight());
324+
}
325+
326+
private void measureText(boolean force, int width, int height) {
327+
int mw = (int) (width - getPaddingLeft() - getPaddingRight() - paint.getFontMetrics().measureText(" "));
328+
int mh = (int) (width - getPaddingTop() - getPaddingBottom() - paint.getFontMetrics().measureText(" "));
300329
mw = Math.max(1, mw);
301330
mh = Math.max(1, mh);
302331
if (force || (textViewport.width != mw || textViewport.height != mw)) {
@@ -330,7 +359,7 @@ protected Size onMeasureContent(int parentWidth, int parentHeight) {
330359
h = parentHeight;
331360
}
332361

333-
measureText(true);
362+
measureText(true, w,h);
334363

335364
return new Size(currentLayout().getWrapSize());
336365
}

0 commit comments

Comments
 (0)