首页 文章

将两种不同的字体样式应用于TextView

提问于
浏览
28

我想将两种不同的字体样式应用于单个TextView中的文本 .

我的情况与Android - two sentences, two styles, one TextView相同 . 唯一的区别是我想在整个文本上设置自定义字体 . 我已将Helvetica Font作为资产包含在我的项目中,并希望将该字体应用于TextView,文本的第一部分将是Helvetica BOLD,其余部分为Helvetica NORMAL . 有什么建议怎么办?

需要以下格式的文字 . 具有不同样式和单个textview的自定义文本 .

enter image description here

5 回答

  • 73

    一种方法是扩展TypefaceSpan

    import android.graphics.Paint;
    import android.graphics.Typeface;
    import android.text.TextPaint;
    import android.text.style.TypefaceSpan;
    
        public class CustomTypefaceSpan extends TypefaceSpan {
            private final Typeface newType;
    
            public CustomTypefaceSpan(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);
            }
        }
    

    然后当你想使用两种不同的字体时调用:

    String firstWord = "first ";
    String secondWord = "second";
    
    // Create a new spannable with the two strings
    Spannable spannable = new SpannableString(firstWord+secondWord);
    
    // Set the custom typeface to span over a section of the spannable object
    spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    // Set the text of a textView with the spannable object
    textView.setText( spannable );
    
  • 3

    这是一个更直接的解决方案,您可以使用HTML在同一个 TextView 上设置不同的样式 .

    例如:

    // Styled label
    String styledText = "<big><b><font color='#333333'>title</font></b></big> <small><b><font color='#CC5490'>subtitle</font></b></small>";
    
    // Apply the styled label on the TextView
    textView.setText(Html.fromHtml(styledText));
    

    您需要以下导入:

    import android.text.Html;
    
  • 0

    这可能有效 - 创建自己的自定义TextView,然后在其中的一部分上使用StyleSpan:

    public class CustomTextView extends TextView {
    
        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void setTypeface(Typeface tf, int style) {
            if (style == 1){
                //replace "HelveticaBOLD.otf" with the name of your bold font
                tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "HelveticaBOLD.otf");
            }else{
                //replace "HelveticaNORMAL.otf" with the name of your normal font
                tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "HelveticaNORMAL.otf");
            }
            super.setTypeface(tf, 0);
        }
    }
    

    然后你可以这样做:

    int index1 = 0; //wherever bold should begin
    int index2 = 5; //wherever bold should end
    
    Spannable span = new SpannableString("some string");
    span.setSpan(new StyleSpan(Typeface.BOLD),index1, index2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    ((CustomTextView)findViewById(R.id.yourTextView)).setText(span);
    
  • -2

    您可以使用 canvas.drawText 方法创建自定义视图并使用两个 Paint 对象渲染文本

  • 2

    在应用可翻转文本之前,您是否尝试在TextView上设置自定义字体?

    Typeface face = Typeface.createFromAsset(ctx.getAssets(), "fonts/boost.ttf")
    TextView tv = (TextView) ctx.findViewById(id);
    tv.setTypeface(face);
    

    然后从链接的问题中应用SpannableStringBuilder - 我只假设如果你的ttf支持'normal'和'bold',它会相应地呈现:)

相关问题