I have the following String RM123.456. I would like to
I had a look at the RelativeSizeSpan
and found a rather simple implementation. So you could just implement your own RelativeSizeSpan
for your purpose. The only difference here is that it doesn't implement ParcelableSpan
, since this is only intended for framework code. AntiRelativeSizeSpan
is just a fast hack without much testing of course, but it seems to work fine. It completely relies on Paint.getTextBounds()
to find the best value for the baselineShift
, but maybe there'd be a better approach.
public class AntiRelativeSizeSpan extends MetricAffectingSpan {
private final float mProportion;
public AntiRelativeSizeSpan(float proportion) {
mProportion = proportion;
}
public float getSizeChange() {
return mProportion;
}
@Override
public void updateDrawState(TextPaint ds) {
updateAnyState(ds);
}
@Override
public void updateMeasureState(TextPaint ds) {
updateAnyState(ds);
}
private void updateAnyState(TextPaint ds) {
Rect bounds = new Rect();
ds.getTextBounds("1A", 0, 2, bounds);
int shift = bounds.top - bounds.bottom;
ds.setTextSize(ds.getTextSize() * mProportion);
ds.getTextBounds("1A", 0, 2, bounds);
shift += bounds.bottom - bounds.top;
ds.baselineShift += shift;
}
}