Skip to content Skip to sidebar Skip to footer

How Linespace Affects Staticlayout Height In One Line Text

Consider this simple example: I have a single line text like this: 'Hello' I wanna measure this text using StaticLayout. So I wrote something like this: StaticLayout layout = new S

Solution 1:

I did some quick digging in the source code, seems to be that this part is the culprit:

if (needMultiply && !lastLine) {
        double ex = (below - above) * (spacingmult - 1) + spacingadd;
        if (ex >= 0) {
            extra = (int)(ex + EXTRA_ROUNDING);
        } else {
            extra = -(int)(-ex + EXTRA_ROUNDING);
        }
    } else {
        extra = 0;
    }

Older versions are missing the !lastLine condition and thus also add the spacing to the last line.

The condition was added in this commit, which, if my github foo doesn't fail me, should be included starting with Android 5.

Apparently, just like the commit mentions, this only affects single line texts, for multiline texts the height seems to be calculated correctly. So an easy fix might be to check whether the text only has a single line (using getLineCount()) and if the Android version is less than 5, and if so substract the line spacing once.

Post a Comment for "How Linespace Affects Staticlayout Height In One Line Text"