Text with custom font and bold style

2020-03-04 07:03发布

I am developing an application. I am using custom font. ".ttf" file to customize font of text view. I used the code as:

Typeface tfArchitectsDaughter = Typeface.createFromAsset(getAssets(), "fonts/ArchitectsDaughter.ttf");
textview.setTypeface(tfArchitectsDaughter);

Now the need is: I want to make the text customize as I done above as well as the style as BOLD in .java file.

How to do this please suggest. And what other styles or customization can be done on font please suggest.

5条回答
够拽才男人
2楼-- · 2020-03-04 07:04

Use the code for making text bold as :

TextView.setTextAppearance(getApplicationContext(), R.style.boldText);
查看更多
聊天终结者
3楼-- · 2020-03-04 07:08

Use a SpannableString. Also have a look at this tutorial.http://blog.stylingandroid.com/archives/177.

String tempString="Copyright";
TextView text=(TextView)findViewById(R.id.text);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
text.setText(spanString);

You can also use different color for text's in textview.

SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");  
// make "Lorem" (characters 0 to 5) red  
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);  
textView.setText(text, BufferType.SPANNABLE);

http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring. The link gives you more example of styling using spannable string.

查看更多
▲ chillily
4楼-- · 2020-03-04 07:14

You can use the below code to set your text as bold

create a seperate file as style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="boldText">
        <item name="android:textStyle">bold|italic</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="normalText">
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#C0C0C0</item>
    </style>
</resources>

And in your java file if you want the text to be bold after can action means

Typeface tfArchitectsDaughter = Typeface.createFromAsset(getAssets(), "fonts/ArchitectsDaughter.ttf");
textview.setTypeface(tfArchitectsDaughter);

myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
查看更多
\"骚年 ilove
5楼-- · 2020-03-04 07:21

You can make your text bold as :

TextView.setTextAppearance(getApplicationContext(), R.style.boldText);
查看更多
手持菜刀,她持情操
6楼-- · 2020-03-04 07:29

You can achieve it with

textview.setTypeface(tfArchitectsDaughter, Typeface.BOLD);

Note: For sure you can use also ITALIC and BOLD_ITALIC

查看更多
登录 后发表回答