0

I`m implementing a custom view which contains text, and the text will be drawn in onDraw.

My problem is, I wish my text will looks like those in the TextView objects, but I`m not quit familiar with setting up the style and typeface.

The text finally drawn with my fresh new Paint object, looks different from TextView: The lines looks thinner and seems bitten by some insects... - -b. I wish my text will be drawn just like those TextView objects.

Plz help!

====================
The problem in other words:

In a custom view extending View, I call mPaint.drawText in the onDraw method, with a new Paint object mPaint. But the text drawn looks different to the default TextView, the lines are thinner and not smooth(like were bitten by some insects). I just want it to be the same look as TextView.

Lyn
  • 699
  • 1
  • 7
  • 17

2 Answers2

1
 public class CustomText extends TextView { 
 public CustomText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    Typeface typeface = Typeface.createFromAsset(context.getAssets(),
    "fonts/your_font.ttf");
    setTypeface(typeface);

}

public CustomText(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    Typeface typeface = Typeface.createFromAsset(context.getAssets(),
    "fonts/fonts/your_font.ttf");
    setTypeface(typeface);

}

public CustomText(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/fonts/your_font.ttf");
setTypeface(typeface);
    }

}

Now use the CustomText in your class/xml.

Hope this will help you.

Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
  • I dont have my own ttf, I just wish to acquire the same look as TextView, how can I do that? Since I create an Paint object and use it to draw text whose lines looks thin and not smooth but like bitten by insects. – Lyn Apr 04 '11 at 12:54
  • `I just wish to acquire the same look as TextView` then why dont you use the default `TextView`?Can you clarify your requirements. – Tanmay Mandal Apr 04 '11 at 13:00
  • Sorry but the scene is: I`v created a custom widget extending View. It contains mixing text and small images, like "hello (smile) world", and the "smile" will be replace with a emoticon.I have completed all the tasks of parsing the raw text, cutting words, layouting, and drawing of text and the icons. But as I`m not quite familiar with the graphic part of Android, I merely new a Paint object and call drawText. The problem is here: I wish the text drawn will be the same look as TextView, but the look is different: lines of those characters thinner, and not smooth like were bitten by insects. – Lyn Apr 04 '11 at 13:21
0

You can create a custom view by extending TextView as a parent view. by doing this you will get all the default features that textView has and plus you can add other custom features as per your requirements.

mudit
  • 25,306
  • 32
  • 90
  • 132
  • I just wish to acquire the same look as TextView, how can I do that? Since I create an Paint object and use it to draw text whose lines looks thin and not smooth but like bitten by insects. – Lyn Apr 04 '11 at 12:54
  • No, I have other elements like images(icons), I think I should not just extends TextView. – Lyn Apr 04 '11 at 12:55
  • Sorry but the scene is: I`v created a custom widget extending View. It contains mixing text and small images, like "hello (smile) world", and the "smile" will be replace with a emoticon.I have completed all the tasks of parsing the raw text, cutting words, layouting, and drawing of text and the icons. But as I`m not quite familiar with the graphic part of Android, I merely new a Paint object and call drawText. The problem is here: I wish the text drawn will be the same look as TextView, but the look is different: lines of those characters thinner, and not smooth like were bitten by insects. – Lyn Apr 04 '11 at 13:27