Skip to content

Commit

Permalink
修复padding异常的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JinJieGu committed Mar 7, 2018
1 parent ac5e2df commit 7e3990c
Showing 1 changed file with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public class CustomLabelSpan extends ReplacementSpan implements OnClickStateChan
private int pressBgColor;

public CustomLabelSpan(String normalSizeText, SpecialLabelUnit specialLabelUnit) {
mSpecialLabelUnit = specialLabelUnit;
this.mSpecialLabelUnit = specialLabelUnit;
mSpecialText = mSpecialLabelUnit.getText();
mNormalSizeText = normalSizeText;
isClickable = mSpecialLabelUnit.isClickable();
mBgColor = mSpecialLabelUnit.getBgColor();
this.mNormalSizeText = normalSizeText;
this.isClickable = mSpecialLabelUnit.isClickable();
this.mBgColor = mSpecialLabelUnit.getBgColor();

mBitmap = mSpecialLabelUnit.getBitmap();
if (null == mBitmap) {
Expand Down Expand Up @@ -110,13 +110,20 @@ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.Fon
}
}
}

//修复上下padding不生效,修复normalSizeText为空导致高度为0,draw不调用的问题
Paint.FontMetricsInt metrics = paint.getFontMetricsInt();
if (fm != null) {
fm.top = metrics.top - mPaddingTop;
fm.ascent = metrics.ascent - mPaddingBottom;
fm.descent = metrics.descent + mPaddingTop;
fm.bottom = metrics.bottom + mPaddingBottom;
}
return Math.round(mFinalWidth);
}

@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
float finalUnitHeight = 2*bottom - top;
float finalUnitHeight = bottom - top;
float bgTop = bottom - finalUnitHeight;
if (isClickable && isSelected && pressBgColor != 0) {
// click background
Expand Down Expand Up @@ -216,13 +223,11 @@ private float initFinalWidth(Paint paint) {

int labelBgWidth = mSpecialLabelUnit.getLabelBgWidth();
mSpecialTextWidth = paint.measureText(mSpecialText, 0, mSpecialText.length());
// if (labelBgWidth > 0 && labelBgWidth > mSpecialTextWidth) {
// mFinalWidth = labelBgWidth;
// } else {
// mFinalWidth = mSpecialTextWidth + mPaddingLeft + mPaddingRight;
// }

mFinalWidth = labelBgWidth;
if (labelBgWidth > 0 && labelBgWidth > mSpecialTextWidth) {
mFinalWidth = labelBgWidth;
} else {
mFinalWidth = mSpecialTextWidth + mPaddingLeft + mPaddingRight;
}
}

return mFinalWidth;
Expand All @@ -232,26 +237,35 @@ private float initFinalHeight(Paint paint) {
if (mFinalHeight <= 0) {
int labelBgHeight = mSpecialLabelUnit.getLabelBgHeight();

Rect specialTextRect = new Rect();
paint.getTextBounds(mNormalSizeText, 0, mNormalSizeText.length(), specialTextRect);
mLineTextHeight = specialTextRect.height();
mLineTextBaselineOffset = specialTextRect.bottom;

float labelTextSize = mSpecialLabelUnit.getLabelTextSize();
if (labelTextSize > 0 && labelTextSize != paint.getTextSize()) {
paint.setTextSize(labelTextSize);
}

Rect specialTextRect = new Rect();
paint.getTextBounds(mNormalSizeText, 0, mNormalSizeText.length(), specialTextRect);
//修复当高度为固定值时由于padding为0导致高度为自适应而非预设高度
if (labelBgHeight > 0){
mPaddingBottom = (labelBgHeight - specialTextRect.bottom + specialTextRect.top) / 2;
mPaddingTop = mPaddingBottom;
}
specialTextRect.bottom += mPaddingBottom;
specialTextRect.top -= mPaddingBottom;
mLineTextHeight = specialTextRect.height();
mLineTextBaselineOffset = specialTextRect.bottom;

paint.getTextBounds(mSpecialText, 0, mSpecialText.length(), specialTextRect);
specialTextRect.bottom += mPaddingBottom;
specialTextRect.top -= mPaddingBottom;
mSpecialTextHeight = specialTextRect.height();
mSpecialTextBaselineOffset = specialTextRect.bottom;

// if (labelBgHeight > 0 && labelBgHeight > mSpecialTextHeight && labelBgHeight <= mLineTextHeight) {
// mFinalHeight = labelBgHeight;
// } else {
// mFinalHeight = mSpecialTextHeight + mPaddingTop + mPaddingBottom;
// }
mFinalHeight = labelBgHeight;
if (labelBgHeight > 0 && labelBgHeight > mSpecialTextHeight && labelBgHeight <= mLineTextHeight) {
mFinalHeight = labelBgHeight;
} else {
mFinalHeight = mSpecialTextHeight + mPaddingTop + mPaddingBottom;
}

if (mFinalHeight > mLineTextHeight) {
mFinalHeight = mLineTextHeight;
}
Expand Down

0 comments on commit 7e3990c

Please sign in to comment.