Android's equivalent to CoreText
Asked Answered
M

2

7

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.

Masterpiece answered 22/4, 2012 at 18:33 Comment(0)
L
7

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.

Leggat answered 8/5, 2012 at 1:41 Comment(2)
Has anything changed in the last 6 years? Has Android now got a real text system?Rabin
@Lothar, I haven't worked on text rendering for a long time. But I guess the answer to your question is still no, I didn't see any significant changes regarding text rendering introduced in the past years. Since the performance of WebView as well as that of the hardware has been improved a lot, HTML for complex text laying-out/rendering may be the way to go nowadays.Leggat
G
1

TextView can handle "the ability to change formatting, font-size and font face", via spannable strings. See the android.text.style package for the various effects that you can apply.

In terms of "enormous amounts of text", wrap the TextView in a ScrollView.

You can always fall back to using WebView, which gives you full WebKit capabilities to render HTML, if needed.

Germaun answered 22/4, 2012 at 18:46 Comment(3)
On iOS, CoreText tells the exact amount of text that would be visible on a certain frame. This feature is very, as the application can place several frames placed on a ScrollView, paginating the text. Could TextView do the same on Android?Careerism
@javsmo: Not that I am aware of. The only paginated text I have seen on Android is with ebook readers, which will use HTML and WebView.Germaun
@CommonsWare, not really, most ebook readers use canvas and the drawText APIs, I once implemented an ebook reader, things would be much more controllable with the drawText APIs.Leggat

© 2022 - 2024 — McMap. All rights reserved.