Custom sized italic Font using Spannable

末鹿安然 提交于 2020-01-04 06:06:00

问题


There is the following code:

String s = message + "\n" + date;
Spannable f = Spannable.Factory.getInstance().newSpannable(s);
f.setSpan(new ForegroundColorSpan(Color.BLUE), 0, message.length(), 0);
f.setSpan(new ForegroundColorSpan(Color.RED), message.length() + 1, s.length(), 0);

textView.setText(f);

This code works fine for setting up diffent colors for textView. But I need another feature - I'd like that "date" has got smaller size of font and italic typeface. How can I achieve that?


回答1:


Check this

Different font size of strings in the same TextView

Try the below

String title="My Custom Text!";  
TextView tv = (TextView) findViewById(R.id.some_id);
SpannableString ss1=  new SpannableString(title);
ss1.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss1.length(), 0);
ss1.setSpan(new RelativeSizeSpan(2f), 0, ss1.length, 0); 
tv.setText(ss1);

For more styling

http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring




回答2:


You can make use of Html.fromHtml("your html string"), to do any style you want in HTML and then show it in the textview as shown below:

 Spanned htmlText = Html.fromHtml(getString(R.string.yourStringResource));
 yourTextView.setText(htmlText, TextView.BufferType.SPANNABLE);

And the XML string.xml, must have the string declared as shown below:

 <string name="yourStringResource"><![CDATA[BLA BLA BLA BLA <br/> BLABA <font color="blue" size="6">COLORED HTML</font>]]></string>

Hope this helps.

Regards!




回答3:


You can use HTML tags, like this:

String s = "<html>" + message + "<br /><i><small>" + date + "</i></small></html>";



回答4:


Yes, that is absolutely possible. You can simply add another Span:

Spannable.setSpan(new StyleSpan(Typeface.ITALIC), from, to, 0);
Spannable.setSpan(new RelativeSizeSpan(float size), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

In this case, its a StyleSpan to Italic and a RelativeSizeSpan.



来源:https://stackoverflow.com/questions/18621691/custom-sized-italic-font-using-spannable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!