Skip to content

Commit b2cf0c5

Browse files
committed
1.修复center_vertical时文字重叠问题
2.新增autoMaxHeight熟悉 -修复左右文字大小大于中间文字大小高度不准问题 -修复英文由于基线导致展示不全问题
1 parent 9bb2ada commit b2cf0c5

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

library/src/main/java/com/study/xuan/library/span/EasyVerticalCenterSpan.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@ public class EasyVerticalCenterSpan extends ReplacementSpan {
1515
private float fontSizeSp; //字体大小sp
1616
private float paintColor;
1717

18-
public EasyVerticalCenterSpan(float fontSizeSp,float paintColor){
18+
public EasyVerticalCenterSpan(float fontSizeSp, float paintColor) {
1919
this.fontSizeSp = fontSizeSp;
2020
this.paintColor = paintColor;
2121
}
2222

2323
@Override
24-
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
24+
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt
25+
fm) {
26+
paint.setTextSize(fontSizeSp);
2527
text = text.subSequence(start, end);
2628
return (int) paint.measureText(text.toString());
2729
}
2830

2931
@Override
30-
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
32+
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int
33+
y, int bottom, Paint paint) {
3134
text = text.subSequence(start, end);
3235
paint.setTextSize(fontSizeSp);
3336
paint.setColor((int) paintColor);
3437
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
35-
canvas.drawText(text.toString(), x, y - ((y + fm.descent + y + fm.ascent) / 2 - (bottom + top) / 2), paint); //此处重新计算y坐标,使字体居中
38+
canvas.drawText(text.toString(), x, y - ((y + fm.descent + y + fm.ascent) / 2 - (bottom +
39+
top) / 2), paint); //此处重新计算y坐标,使字体居中
3640
}
37-
3841
}

library/src/main/java/com/study/xuan/library/widget/EasyTextView.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public class EasyTextView extends TextView {
7373
private TypedValue textValue;//左右文字支持xml中设置iconFont
7474
//icon的index
7575
private int iconIndex = 0;
76+
//是否开启计算文字边界,开启后会以最大文字大小为View高度,并且会增加部分文字高度,防止部分英文类型y,g由于基线的原因无法显示完全
77+
private boolean autoMaxHeight = false;
7678

7779
public EasyTextView(Context context) {
7880
this(context, null);
@@ -135,7 +137,8 @@ private void initIconFont() {
135137

136138
if (!TextUtils.isEmpty(mTextRight)) {
137139
AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan((int) mTextPadding);
138-
stringBuilder.setSpan(sizeSpan, iconIndex + centerSize, iconIndex + centerSize + 1, Spanned
140+
stringBuilder.setSpan(sizeSpan, iconIndex + centerSize, iconIndex +
141+
centerSize + 1, Spanned
139142
.SPAN_EXCLUSIVE_EXCLUSIVE);
140143
}
141144

@@ -196,7 +199,8 @@ private void initIconFont() {
196199
for (SpanContainer container : rightContainer) {
197200
for (Object o : container.spans) {
198201
try {
199-
stringBuilder.setSpan(o, start + container.start, start + container.end, container.flag);
202+
stringBuilder.setSpan(o, start + container.start, start + container.end,
203+
container.flag);
200204
} catch (Exception e) {
201205
//please check invoke clearSpan() method first
202206
}
@@ -269,7 +273,8 @@ private void setLeftTextAttr(SpannableStringBuilder stringBuilder) {
269273
}
270274
}
271275

272-
private void initTextSize(SpannableStringBuilder stringBuilder, int start, int end, float textSize, int mCurColor) {
276+
private void initTextSize(SpannableStringBuilder stringBuilder, int start, int end, float
277+
textSize, int mCurColor) {
273278
if (textSize != 0) {
274279
CharacterStyle sizeSpan;
275280
final int gravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
@@ -297,7 +302,8 @@ private void initTextLeftColor(SpannableStringBuilder stringBuilder, int end) {
297302
}
298303
}
299304

300-
private void initTextStyle(int textStyle, SpannableStringBuilder stringBuilder, int start, int end) {
305+
private void initTextStyle(int textStyle, SpannableStringBuilder stringBuilder, int start,
306+
int end) {
301307
StyleSpan span;
302308
if (textStyle != Typeface.NORMAL) {
303309
span = new StyleSpan(textStyle);
@@ -376,6 +382,7 @@ private void initAttr(Context context, AttributeSet attrs) {
376382
mTextLeftStyle = array.getInt(R.styleable.EasyTextView_textLeftStyle, Typeface.NORMAL);
377383
mTextRightStyle = array.getInt(R.styleable.EasyTextView_textRightStyle, Typeface.NORMAL);
378384
mTextCenterStyle = array.getInt(R.styleable.EasyTextView_textCenterStyle, Typeface.NORMAL);
385+
autoMaxHeight = array.getBoolean(R.styleable.EasyTextView_autoMaxHeight, false);
379386
array.recycle();
380387
}
381388

@@ -730,4 +737,17 @@ public EasyTextView build() {
730737
init();
731738
return this;
732739
}
740+
741+
@Override
742+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
743+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
744+
if (autoMaxHeight) {
745+
int lead = 0;
746+
if (getPaint() != null) {
747+
lead = getPaint().getFontMetricsInt().leading * 3;
748+
}
749+
setMeasuredDimension(getMeasuredWidth(), (int) (Math.max(getMeasuredHeight(), Math.max
750+
(mLeftSize, mRightSize)) + lead));
751+
}
752+
}
733753
}

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535
<attr name="textRightColor" format="reference|color"/>
3636
<attr name="textLeftSize" format="dimension"/>
3737
<attr name="textRightSize" format="dimension"/>
38+
<attr name="autoMaxHeight" format="boolean"/>
3839
</declare-styleable>
3940
</resources>

0 commit comments

Comments
 (0)