Create Rich Text Features in iOS with Text Kit
2014-12-25 09:47
337 查看
Built on top of the Core Text framework sits Text Kit, a high-level text layout and rendering API in iOS 7. Text Kit makes text much easier to work with than having to drop down to Core Text. It allows you to implement rich, text-based applications such
as magazine readers or document editors that often require custom text styles and layouts. Features such as these were possible prior to iOS 7, but required complex implementations. However, with Text Kit, they are a breeze to create:
Text Kit’s architecture separates text storage from layout and display as illustrated below:
NSTextContainer – Provides the coordinate system and geometry that is used to layout text.
NSLayoutManager – Lays out text by turning text into glyphs.
NSTextStorage – Holds the text data, as well as handles batch text property updates.
These three classes are applied to a view that renders text. The built-in text handling views, such as UITextView, UITextField, and UILabel already have them set, but you can create and apply them to any UIView instance as well.
The NSTextStorage instance communicates any changes to the text – such as changes to characters or their attributes – to the layout manager for display. NSTextStorage inherits from NSMutableAttributedString, allowing changes to text attributes
to be specified in batches between BeginEditing and EndEditing calls.
For example, the following code snippet specifies a change to the foreground and background colors, respectively, and targets particular ranges:
After EndEditing is called, the changes are sent to the layout manager, which in turn performs any necessary layout and rendering calculations for the text to be displayed in the view, as shown:
In addition to manipulating text attributes, Text Kit also makes it easy to control layout. This simplifies the once difficult process of creating custom text layouts, making it easier to implement.
For example, we can create multiple text containers for the layout manager to use for a multi-column layout by following a few easy steps:
Create a text storage instance
Create a layout manager
Add the layout manager to the text storage
Create a text container
Add the text container to the layout manager
Create a text view with the text container
Repeat steps 4 – 6 for an additional column
With the layout containers defining the area to draw the text for each text view, we get a nice 2 column text layout as shown below:
As you can see, having Text Kit now makes creating text-based features much more approachable than in the past. You can download the code from this post
here to give it a try and see what interesting text scenarios you can come up with.
as magazine readers or document editors that often require custom text styles and layouts. Features such as these were possible prior to iOS 7, but required complex implementations. However, with Text Kit, they are a breeze to create:
Text Kit’s architecture separates text storage from layout and display as illustrated below:
NSTextContainer – Provides the coordinate system and geometry that is used to layout text.
NSLayoutManager – Lays out text by turning text into glyphs.
NSTextStorage – Holds the text data, as well as handles batch text property updates.
These three classes are applied to a view that renders text. The built-in text handling views, such as UITextView, UITextField, and UILabel already have them set, but you can create and apply them to any UIView instance as well.
The NSTextStorage instance communicates any changes to the text – such as changes to characters or their attributes – to the layout manager for display. NSTextStorage inherits from NSMutableAttributedString, allowing changes to text attributes
to be specified in batches between BeginEditing and EndEditing calls.
For example, the following code snippet specifies a change to the foreground and background colors, respectively, and targets particular ranges:
In addition to manipulating text attributes, Text Kit also makes it easy to control layout. This simplifies the once difficult process of creating custom text layouts, making it easier to implement.
For example, we can create multiple text containers for the layout manager to use for a multi-column layout by following a few easy steps:
Create a text storage instance
As you can see, having Text Kit now makes creating text-based features much more approachable than in the past. You can download the code from this post
here to give it a try and see what interesting text scenarios you can come up with.
相关文章推荐
- MKNetworkkit uses features only available in iOS SDK 4.0 and later
- iOS - 裁剪图片方法CGImageCreateWithImageInRect
- xcode6添加MKNetworkKit的注意事项(mknetworkkit uses features only available in ios sdk 4.0 and later)
- MKNetworkKit uses features only available in iOS SDK 4.0 and later报错解决方案
- ios 下CGImageCreateWithImageInRect 内存泄露
- MKNetworkKit uses features only available in iOS SDK 4.0 and later报错解决方案 --- 有我的一点思想
- To create my first app in iOS with Xcode(在Xcode创建我的第一个iOS app )
- iOS- Please sign in with an app-specific password. You can create one at applied.apple.com.
- Problems with Eclipse buttons in Ubuntu 9.10(Cannot create/import project)
- Can not open file with "ios_base::in | ios_base::app"
- How to create a PictureMarkerSymbol (Picture Symbol)in ArcGIS server with C#
- How To Create a Simple Magazine App with Core Text
- ASP.NET MVC #01, Create a Movie Database Application in 15 Minutes with ASP.NET MVC
- Create XML with LoadXml(string) in ASP.NET
- MainWindow.xib absence in Xcode 4.2 beta 4 with iOS 5 SDK
- 【LR】The Vuser Generator is unable to create a text check in this manner .To create a text check,swich to the Server Response vie
- 【LR】The Vuser Generator is unable to create a text check in this manner .To create a text check,swich to the Server Response vie
- How to create a resizeable dialog box with scroll bars in Visual C++
- How to create your own api with ACL in Magento
- Create an avatar and chat with your friends in rooms you design