klinker24 / Android-TextView-LinkBuilder

Insanely easy way to define clickable links within a TextView.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The text view got cut off when adding bold spannable text

Hellothi11 opened this issue · comments

I added a link with bold style but the textview got cut off at the tail because the bold text is bigger than normal font. So please help to fix it. Thank you

I find out the solution
I created a custom TypefaceSpan
then change the function of applyLink to:

/**
 * Set the link rule to the spannable text.
 *
 * @param link  rule we are applying.
 * @param range the start and end point of the link within the text.
 * @param text  the spannable text to add the link to.
 */
private void applyLink(Link link, Range range, Spannable text) {
    TouchableSpan span = new TouchableSpan(context, link);
    text.setSpan(span, range.start, range.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    if (link.getTypeface() != null) {
        BoldTypefaceSpan typefaceSpan = new BoldTypefaceSpan("Custom", link.getTypeface());
        text.setSpan(typefaceSpan, range.start, range.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

Could you create a pull request? What if the typeface on the link is not bold, what if it is italic or something? This method only seems to handle bold typefaces and convert any other typefaces to bold? Unless I am missing something with your BoldTypefaceSpan class

My BoldTypefaceSpan class is just a name of my custom typeface span, I should rename it to CustomTypefaceSpan, its implementation handles italic case so. I found it on google:

public class BoldTypefaceSpan extends TypefaceSpan {
private final Typeface newType;

public BoldTypefaceSpan(String family, Typeface type) {
    super(family);
    newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}

}

When you do it this way, do you lose all the attributes of TouchableSpan though? I guess I am a bit confused if there is something you want me to include in the project or not

The TouchableSpan works well, this implementation just makes the size of text view is re-calculated and adds the [Break line] at right position. I did not think much about this impact to your library, but now everything looks ok. So you can check it more to decide if need update or not.

The point of the library is to create clickable links, by not using the TouchableSpan, it kind of removes the point of the library, doesn't it? With this, all you are doing is bolding text, not actually creating a link

I know it, that the reason I use your library, the links are still clickable, the action is right, all my extra implementing just makes the display is correct.