I am using a custom typeface, and it displays perfectly on newer android versions(tested on API 17(Asus tab), 18(Dell tab), 19(Nex4) - devices). However, the same typeface looks faded (possibly distorted?) on older versions - API 8(SE X10i), 10(LG P500H).
Here's a comparison, in case my explanation doesn't make sense:
On nex4:
On x10i:
I am using a custom typeface with Typeface.BOLD:
tvTitle.setTypeface(titleAndBodyFont, Typeface.BOLD);
And the body ("Looks *" part):
tvBody.setTypeface(titleAndBodyFont);
XML declaration for title:
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_title_side"
android:layout_marginRight="@dimen/margin_title_side"
android:layout_marginTop="@dimen/margin_title_top"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/constant_color"
android:textSize="@dimen/text_size_title" />
tvBody
is declared in a similar fashion.
Is this a known bug? If so, could someone help me find the bug report? It would help knowing what version this was fixed in. If not, I would be grateful for a solution.
Thanks everyone.
I always use 2 fonts, 1 normal and 1 bold. So when you need to go bold, just change your font. Never had problems, you can also create your custom textviewplus.
Something like this:
/res/values/attrs.xml
<resources>
<attr name="fontFamily" format="enum">
<enum name="helvetica" value="0"/>
<enum name="helvetica_bold" value="1"/>
</attr>
your TextViewPlus:
public class TextViewPlus extends TextView{
private static final String TAG = "TextViewPlus";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
int customFont = a.getInt(R.styleable.TextViewPlus_fontFamily, -1);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, int font) {
Typeface tf = null;
try {
tf = Typefaces.get(ctx, font);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typefaces.get(ctx, asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
How to use:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.myapp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.myapp.viewhelpers.TextViewPlus
app:fontFamily="helvetica"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fontFamily="helvetica"
android:text="helo world"/>
I really can't help you much, definitely looks like a bug, but to check the bug extension maybe you find this test useful:
public class TestBoldText extends View {
private TextPaint mPaint;
public TestBoldText(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new TextPaint();
mPaint.setAntiAlias(true);
mPaint.setFakeBoldText(true);
mPaint.setFlags(mPaint.getFlags()|Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("i am a bold text!", 0, 0, mPaint)
}
}
It looks like Android uses a "fake bold" approach to draw bold text, rather than a specific bold version of the typeface. I suppose they draw the same text with a pixel offset. This test draws acceptable bold text in my devices with API >= ICS (samsung galaxy / amazon kindle) but I'd be curious to know if it fails in your device as well, as I am doing kind of a wysiwig editor.
Also, mPaint.setStrokeWidth
should change the text line width. I suppose playing with setStrokeWidth / setAntiAlias / setFakeBoldText you can get a decent bold text in the older API, but I also wonder why the result is not the same...
good luck in your investigation !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With