Guest User

Untitled

a guest
Jan 20th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import android.graphics.Paint;
  2. import android.graphics.Typeface;
  3. import android.text.TextPaint;
  4. import android.text.style.TypefaceSpan;
  5.  
  6. public class CustomTypefaceSpan extends TypefaceSpan {
  7. private final Typeface newType;
  8.  
  9. public CustomTypefaceSpan(String family, Typeface type) {
  10. super(family);
  11. newType = type;
  12. }
  13.  
  14. @Override
  15. public void updateDrawState(TextPaint ds) {
  16. applyCustomTypeFace(ds, newType);
  17. }
  18.  
  19. @Override
  20. public void updateMeasureState(TextPaint paint) {
  21. applyCustomTypeFace(paint, newType);
  22. }
  23.  
  24. private static void applyCustomTypeFace(Paint paint, Typeface tf) {
  25. int oldStyle;
  26. Typeface old = paint.getTypeface();
  27. if (old == null) {
  28. oldStyle = 0;
  29. } else {
  30. oldStyle = old.getStyle();
  31. }
  32.  
  33. int fake = oldStyle & ~tf.getStyle();
  34. if ((fake & Typeface.BOLD) != 0) {
  35. paint.setFakeBoldText(true);
  36. }
  37.  
  38. if ((fake & Typeface.ITALIC) != 0) {
  39. paint.setTextSkewX(-0.25f);
  40. }
  41.  
  42. paint.setTypeface(tf);
  43. }
  44. }
  45.  
  46. String firstWord = "first ";
  47. String secondWord = "second";
  48.  
  49. // Create a new spannable with the two strings
  50. Spannable spannable = new SpannableString(firstWord+secondWord);
  51.  
  52. // Set the custom typeface to span over a section of the spannable object
  53. spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  54. spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  55.  
  56. // Set the text of a textView with the spannable object
  57. textView.setText( spannable );
  58.  
  59. // Styled label
  60. String styledText = "<big><b><font color='#333333'>title</font></b></big> <small><b><font color='#CC5490'>subtitle</font></b></small>";
  61.  
  62. // Apply the styled label on the TextView
  63. textView.setText(Html.fromHtml(styledText));
  64.  
  65. import android.text.Html;
  66.  
  67. public class CustomTextView extends TextView {
  68.  
  69. public CustomTextView(Context context, AttributeSet attrs) {
  70. super(context, attrs);
  71. }
  72.  
  73. @Override
  74. public void setTypeface(Typeface tf, int style) {
  75. if (style == 1){
  76. //replace "HelveticaBOLD.otf" with the name of your bold font
  77. tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "HelveticaBOLD.otf");
  78. }else{
  79. //replace "HelveticaNORMAL.otf" with the name of your normal font
  80. tf = Typeface.createFromAsset(getContext().getApplicationContext().getAssets(), "HelveticaNORMAL.otf");
  81. }
  82. super.setTypeface(tf, 0);
  83. }
  84. }
  85.  
  86. int index1 = 0; //wherever bold should begin
  87. int index2 = 5; //wherever bold should end
  88.  
  89. Spannable span = new SpannableString("some string");
  90. span.setSpan(new StyleSpan(Typeface.BOLD),index1, index2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  91.  
  92. ((CustomTextView)findViewById(R.id.yourTextView)).setText(span);
  93.  
  94. class FooClass {
  95. void Foo() {
  96. textViewSetText(R.id.photo_title, "Title: ", photo.getTitle());
  97. textViewSetText(R.id.photo_tags, "Tags: ", photo.getTags());
  98. textViewSetText(R.id.photo_author, "Author: ", photo.getAuthor());
  99. }
  100.  
  101. private void textViewSetText(int resourceId, String prefix, String text) {
  102. TextView textView = (TextView) findViewById(resourceId);
  103. SpannableString styledText = new SpannableString(prefix);
  104. styledText.setSpan(new StyleSpan(Typeface.BOLD), 0, prefix.length(), 0);
  105. textView.setText(null);
  106. textView.append(styledText);
  107. textView.append(text);
  108. }
  109. }
  110.  
  111. Typeface face = Typeface.createFromAsset(ctx.getAssets(), "fonts/boost.ttf")
  112. TextView tv = (TextView) ctx.findViewById(id);
  113. tv.setTypeface(face);
Add Comment
Please, Sign In to add comment