This is what happens in the preview and on device:
TextView is nothing special, it just loads the custom font:
public class TestTextView extends AppCompatTextView {
public TestTextView(Context context) {
super(context);
init(context);
}
public TestTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TestTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
void init(Context context) {
Typeface t = Typeface.createFromAsset(context.getAssets(), "fonts/daisy.ttf");
setTypeface(t);
}
}
Layout is also very basic, but just in case:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/material_red200"
android:orientation="vertical">
<*custompackage* .TestTextView
android:gravity="left"
android:padding="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="just some text for testing"
android:textColor="@color/material_black"
android:textSize="100dp" />
</LinearLayout>
As you can see, the left parts, like ‘j’ and ‘f’ are cut off.
Setting the padding or margin did not work.
This font fits into it’s frame when using from other programs.
Thanks in advance.
Edit:
What @play_err_ mentioned is not a solution in my case.
- I am using in the final version a textview that resizes automatically, so adding spaces would be terribly difficult.
- I need an explanation why other programs (eg photoshop, after effects…) can calculate a proper bounding box and android cannot
-
I am also loading different fonts dynamically and I do not want to create an
if(badfont) addSpaces()
7
Answers
This answer has led me to the right path: https://stackoverflow.com/a/28625166/4420543
So, the solution is to create a custom Textview and override the onDraw method:
What if you wrap it in another layout and add padding to that? For example something like this:
Not having your font and other themes etc I’ve just tried it with the cursive font for example and on my machine it would look like this.
screenshot
Update:
Looks like you’re not the only one to have had this issue and the other answers here and here both unfortunately relate to adding extra spaces.
I’ve created a bug ticket here since it looks like a bug to me.
I encountered the same problem when I used some fonts in
EditText
.My first attempt was to use padding. Size of view increased but text is still cropped.
Then I looked at the source code
TextView
. In methodonDraw
methodCanvas.clipRect
is called to perform this crop.My solution to bypass cropping when use padding :
1) Сreate custom class inherited from
Canvas
and override methodclipRect
2) Create custom
TextView
and override methodsonSizeChanged
andonDraw
.In the method
onSizeChanged
create bitmap and canvas.In the method
onDraw
draw on bitmap by passing our customCanvas
to methodsuper.onDraw
. Next, draw this bitmap on the target canvas.A workaround is to add a space before typing. It will save you a lot of coding but will result in a “padding” to the left.
android:text=" text after a space"
Reworked @Dmitry Kopytov solution:
Code:
I have encountered the same problem and i found a one liner solution for thouse who are not using the
TextView.shadowLayer
.this is based on the source code that [Dmitry Kopytov] brought here:
that’s it, now the
canvas.clipRect
inTextView.onDraw()
won’t cut off the curly font sides.replace
TextView.BufferType.SPANNABLE
withTextView.BufferType.NORMAL