This is what happens in the preview and on device:
TextView is nothing special, it just loads the custom font:
public class TestTextView extends App
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 method onDraw
method Canvas.clipRect
is called to perform this crop.
My solution to bypass cropping when use padding :
1) Сreate custom class inherited from Canvas
and override method clipRect
public class NonClippableCanvas extends Canvas {
public NonClippableCanvas(@NonNull Bitmap bitmap) {
super(bitmap);
}
@Override
public boolean clipRect(float left, float top, float right, float bottom) {
return true;
}
}
2) Create custom TextView
and override methods onSizeChanged
and onDraw
.
In the method onSizeChanged
create bitmap and canvas.
In the method onDraw
draw on bitmap by passing our custom Canvas
to method super.onDraw
. Next, draw this bitmap on the target canvas.
public class CustomTextView extends AppCompatTextView {
private Bitmap _bitmap;
private NonClippableCanvas _canvas;
@Override
protected void onSizeChanged(final int width, final int height,
final int oldwidth, final int oldheight) {
if (width != oldwidth || height != oldheight) {
_bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
_canvas = new NonClippableCanvas(_bitmap);
}
super.onSizeChanged(width, height, oldwidth, oldheight);
}
@Override
protected void onDraw(Canvas canvas) {
_canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
super.onDraw(_canvas);
canvas.drawBitmap(_bitmap, 0, 0, null);
}
}