Change line height / line height in EditText at character level

I implemented bold, large, non-editable smart quotes at the beginning and end of my EditText:

enter image description here

Quotations are the non-selectable part of the text on which I set RelativeSizeSpan(2f) . The result is what I wanted.

 s.setSpan(spanLeft, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); s.setSpan(spanRight, s.length() - 1, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

However, as you can see, a larger font size leads to an increase in height, which affects the entire line. This is somehow irrelevant on the first line, but inconvenient recently. I was wondering if it is possible for these two characters to keep the font size, but reduce the line height.

enter image description here

I want to reduce the leading part, so that the first and last line have a consistent height with the rest of the paragraph. My highest desire was for quotation marks to return to the initial level, while maintaining their appearance.

Do you have any suggestions? Is this possible through some kind of Span ? Should I give up? Do you see any workaround?

I see that I can create two png with quotes and use ImageSpan (although I never used it and didn’t know how it would work), but it annoys me a bit (I'm not quite in the graph).

+4
source share
2 answers

I believe that you need to implement your own Span by simply leaving updateMeasureState empty :)
Try

 import android.text.TextPaint; import android.text.style.MetricAffectingSpan; public class TestSubScriptSpan extends MetricAffectingSpan { private final float mProportion; public TestSubScriptSpan(float proportion) { mProportion = proportion; } @Override public void updateMeasureState(TextPaint p) { // DO NOTHING! } @Override public void updateDrawState(TextPaint tp) { tp.setTextSize(tp.getTextSize() * mProportion); tp.baselineShift -= (int) (tp.ascent() / 2); } } 

and then

 TestSubScriptSpan tss = new TestSubScriptSpan(2.0f); s.setSpan(tss, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); s.setSpan(tss, s.length() - 1, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

Pay attention to what you are doing inside updateDrawState (), as this is important.

0
source

I created a similar effect in one of my applications, where I had a gap that made the stretched text smaller and made it stick to the x-height of the main text, starting from the baseline. I created a custom range that extends MetricAffectingSpan and implements a baseline shift. I used SuperscriptSpan.java from the Android SDK as an example.

The idea in this case is that you pull out the baseline of the big end quote to make the overall linear level lower. I am not 100% sure that this will work, because I do not know how to “smartly” calculate the line height.

If this works, be sure to check it out on multiple screens. I had to make a float fit int somewhere where the rounding error was introduced, which was so noticeable at some densities that we had to fix.

0
source

All Articles