Core Text Objective-C Wrapper
https://github.com/akosma/CoreTextWrapper
Introduction(介绍)
One of the most promising and mysterious new frameworks introduced in iOS 3.2 is Core Text. Apple defines Core Text as a “text drawing engine”, which allows Mac (and now iPad) apps to render rich text on any graphics context. Strings drawn with Core Text feature lots of custom settings such as detailed font information, columns, variable line and paragraph heights and several different attributes, which designers and font aficionados surely understand much better than I do. Many new apps have been using this framework since the release of the iPad, particularly newspapers and ebook reader applications, rendering gorgeous text in custom fonts, many of them not available natively in iOS. This framework is also used in lifestyle and corporate applications, too, where using a custom font is sometimes required to match the specifications of brands and trademarks.
在 iOS 3.2 的时候,出现了一个功能强大但又神秘莫测的新框架 Core Text。苹果将 Core Text 定义为“绘制文本的引擎”,允许 Mac (如今也包括了 iPad)的应用程序在任何的绘图上下文中渲染富文本。用 Core Text 绘制的文本拥有非常多的定制特性,比如复杂的字体信息,列数,变化的行数以及段落的高度,还有许多其他的特性。那些开发者以及开发字体的人想必比我更了解 Core Text 的用武之地。许多新应用已经用上了这个框架,尤其是那些报纸或者电子书阅读器的应用。这个框架也同样用在一些生活类以及社团类的应用当中,因为这些应用有时候需要匹配一些特殊的字符与标记。
It is very important to understand that Core Text is really just a text drawing engine to be used on top of Quartz (Core Graphics), to render rich text on any graphics context. Core Text cannot be used to create a rich text editor, for example, so don’t expect to extend UITextView with it. But you can use it to draw any kind of rich text on screen, which can make you avoid using UIWebView instances for that.
Core Text 只是一个文本绘制的引擎,是在 Quartz 的上层封装的,他可以用来在任何的绘图上下文中渲染富文本,了解这一点非常重要。Core Text 是不能被用来当做富文本的编辑器来使用的。例如,不要指望靠 Core Text 来扩展 UITextView 。但是,你可以用它绘制任何形式的富文本,避免你使用 UIWebView 来做这些工作。
Features(特性)
One of the most interesting capabilities of Core Text is being able to render text in several columns. However, Core Text is a C-based framework, and I think that understanding and using the concepts and structures required to render text in columns can be particularly tricky. To make the my life and that of my fellow developers easier, this project contains a small set of Objective-C classes that encapsulate the creation of framesetters, attributed strings and other constructions, and takes care of the creation of several columns, as well as the division of the text in several pages if required.
Core Text 一个非常有意思的特性就是能够将文本分成几列来渲染。然而,Core Text 是基于 C 语言的框架,所以,我觉得,了解那些能够把文本渲染成几列的方法将会十分有难度。为了让我自己少走弯路以及服务于广大的开发者,这个项目包含了一系列的 OC 封装的类,他们封装了frame设置相关的内容,带有特殊属性的字符串以及一些结构,以及小心处理了分列的特性,同时也包括了如何把文本分成几页。
The API interface is very simple (on purpose) and I’m pretty sure you’ll be able to integrate it very easily in your own projects, particularly if you look at the sample project where the classes are used. I’ve also added a category for UIFont, that returns the associated CTFontRef pointer, in a similar fashion to UIImage, which is able to return a pointer to the underlying CGImageRef pointer. It also allows to create a CTFontRef from any font embedded in the application bundle. I am puzzled that the framework designers haven’t included this by default in UIKit.
这些 API 接口非常的简单,我十分确定,你能十分轻易的将它融入到你的项目当中,尤其是你研究了那些包含了这些类使用方法的 demo 。我给 UIFont 添加了一个类目,他是用来返回一个相关联的 CTFontRef 的指针,类似于 UIImage 那样子。它允许你从嵌入到文件当中的字体文件引出指针。我都有点奇怪,为何这个框架的设计者竟然没有把这个特性加入到 UIKit 中。
What’s next(接下来要做的)
A future extension I’d like to add would be a couple of categories to parse simple RTF or HTML strings (to start with, probably just with bold and italic text) and create the appropriate attributed string from it; there’s a couple of AppKit extensions to NSAttributedString that do exactly that, but for the moment they are only available in the Mac version of Cocoa, and I haven’t found anything similar for iOS yet.
下一个版本中,我将添加一些类目用来解析简单的 RTF 或者 HTML 字符串,并能将他们渲染成富文本。实际上有着很多的 AppKit 扩展了 NSAttributedString ,都可以完成那些功能了,但是,此刻,他们仅仅针对与 Mac ,我还没发现任何与 iOS 相关的呢。
附录:
本人修改的源码地址: http://pan.baidu.com/s/1mgsilS0