Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android's equivalent to CoreText

We have an App for iOS that renders an enormous amount of text: http://itunes.apple.com/br/app/biblia-sagrada/id370178518?mt=8

We use CoreText to render text and give the user the ability to change formatting, font-size and font face.

We are trying to port it to Android but I'm not sure if there's a substitute for CoreText in Android.

like image 834
ppaulojr Avatar asked Apr 22 '12 18:04

ppaulojr


1 Answers

The equivalent to iOS' CoreText in Android is the drawText APIs, part of the Canvas class, Canvas.drawText(), Canvas.drawPosText(), etc. see javadoc for Canvas for more detail. These graphics APIs use Skia underneath.

The functionalities that these graphics APIs provide are not the same as those in iOS, say there's no CTFramesetter equivalent that helps you layout text and handle line breaks for you. With the drawText APIs, you can only draw one line at a time, you will have to handle line breaks yourself, It is like using CTTypesetter in iOS. For laying out text, see Paint.breakText and Paint.measureText.

For changing font size & font face, you can set related attributes in the Paint object, which is passed as the last parameter to the drawText APIs.

Snippet:

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(14);
String text = "Hello world!";
canvas.drawText(text, 0, 100, paint);

Note: The coordinate system used in the Android drawText APIs is from left-top corner.

like image 96
neevek Avatar answered Oct 20 '22 01:10

neevek