textkit

iOS文本布局探讨之一——文本布局框架TextKit浅析

北战南征 提交于 2019-12-10 13:38:11
iOS文本布局探讨之一——文本布局框架TextKit浅析 一、引言 在iOS开发中,处理文本的视图控件主要有4中,UILabel,UITextField,UITextView和UIWebView。其中UILabel与UITextField相对简单,UITextView是功能完备的文本布局展示类,通过它可以进行复杂的富文本布局,UIWebView主要用来加载网页或者pdf文件,其可以进行HTML,CSS和JS等文件的解析。 TextKit是一个偏上层的开发框架,在iOS7以上可用,使用它开发者可以方便灵活处理复杂的文本布局,满足开发中对文本布局的各种复杂需求。TextKit实际上是基于CoreText的一个上层框架,其是面向对象的,如果TextKit中提供的API无法满足需求,可以使用CoreText中的API进行更底层的开发。 官方文档中的一张图片很确切,经常会被用来描述TextKit框架在iOS系统文本渲染中所处的位置。 二、TextKit框架的结构 界面在进行文本的渲染时,有下面几个必要条件: 1.要渲染展示的内容。 2.将内容渲染在某个视图上。 3.内容渲染在视图上的尺寸位置和形状。 在TextKit框架中,提供了几个类分别对应处理上述的必要条件: 1.NSTextStorage对应要渲染展示的内容。 2.UITextView对应要渲染的视图。 3

iOS文本布局探讨之三——使用TextKit框架进行富文本布局

心不动则不痛 提交于 2019-12-10 13:33:21
iOS文本布局探讨之三——使用TextKit框架进行富文本布局 一、引言 关于图文混排,其实以前的博客已经讨论很多,在实际开发中,经常使用第三方的框架来完成排版的需求,其中RCLabel和RTLabel是两个比较好用的第三方库,他们的实现都是基于UIView的,通过更底层的CoreText相关API来进行图文处理。相关介绍博客地址如下: iOS中支持HTML标签渲染的MDHTMLLaebl: http://my.oschina.net/u/2340880/blog/703254 。 扩展于RCLabel的支持异步加载网络图片的富文本引擎的设计: http://my.oschina.net/u/2340880/blog/499311 。 iOS开发封装一个可以响应超链接的label——基于RCLabel的交互扩展: http://my.oschina.net/u/2340880/blog/550194 。 二、原生UILabel真的只能渲染文字么? CoreText是一个比较底层且十分强大的文本渲染框架,但是其使用起来并不是十分方便。在较低版本的iOS系统中,要进行富文本排版十分困难。在iOS6中,系统为UILabel,UITextView等这类文本渲染控件引入了NSAttributedString属性,有了NSAttributedString这个类

iOS 7 UITextView: Size of nstextattachment getting 2x after reopening the application

北慕城南 提交于 2019-12-10 04:22:30
问题 I am building a note editor using the Text Kit in ios7. Earlier I had trouble in rendering of custom size NSTextAttachment's as it was slowing down the rendering to a great extent.I solved the issue by scaling the images and then adding them to textview.You can find my answer in iOS 7.0 UITextView gettings terribly slow after adding images to it After scaling the images the textview rendering runs fine without any lag.The attributed text of textview is stored in core data.During a running

Confused by NSStringDrawingOptions item meaning

廉价感情. 提交于 2019-12-10 01:52:24
问题 iOS7 and later, we can use - (void)drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context to calculate the string size, but I'm confused with the comments on NSStringDrawingOptions enum. NSStringDrawingUsesLineFragmentOrigin It means specified origin is the line fragment origin, not the base line origin. But what mean of line fragment origin and baseline origin. Just like the WWDC 2013 Session 220

textkit custom text attribute draws big rect when it spans multiple lines

假如想象 提交于 2019-12-09 20:08:24
问题 I am basically creating a custom attribute to draw a rounded rectangle in my text subclassing NSLayoutManager with drawGlyphsForGlyphRange method below. Below works like a charm with ranges that spans one line. However, when the range of text spans in two lines, I am getting a big rectangle which draws the attribute along those two lines. I think I should be using a different approach here, I tried nsbackgroundattribute to draw the highlight but unfortunately I cannot make the highlight

TextKit: How is the editor placeholder feature implemented in Xcode?

时间秒杀一切 提交于 2019-12-09 07:03:34
问题 I took a deep dive into TextKit and wondered how the editor placeholders are implemented in the Xcode code editor: You can also try this yourself and type something along the lines of: <#Hello#> , which automatically turns into a placeholder. The Xcode editor is built with TextKit. After some research I came up with two possible strategies: Using NSTextAttachment : as soon as a string matching the placeholder pattern <#...#> is detected, that string is removed and replaced by a

Adding exclusion paths to multiple text views

对着背影说爱祢 提交于 2019-12-07 18:08:54
问题 I'm trying to add multiple exclusion paths to a series of UITextView s laid out successively in a UIScrollView , like so: while (lastRenderedGlyph < self.manager.numberOfGlyphs) { CGRect textViewFrame = CGRectMake(currentXOffset, 10, width / 2, height - 20); CGSize columnSize = CGSizeMake(CGRectGetWidth(textViewFrame) - 20, CGRectGetHeight(textViewFrame) - 10); NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize]; [self.manager addTextContainer:textContainer];

When creating a series of NSTextContainers, how do I specify container breaks based on the text content?

China☆狼群 提交于 2019-12-06 04:05:40
I'm creating a series of NSTextContainers to hold the text from an HTML resource. I am able to add the HTML to an attributed string, assign that to a NSTextStorage and NSLayoutManager, and create a series of NSTextContainers to hold all the text. My problem is, I want to add "page breaks" within the text, i.e. stop filling this text container and start another... In the documentation, I've found something call NSControlCharacterContainerBreakAction; but I'm unclear how to implement it or if thats even the best approach. Code snippet below is how I'm current building by text containers (in

Adding exclusion paths to multiple text views

左心房为你撑大大i 提交于 2019-12-06 01:55:31
I'm trying to add multiple exclusion paths to a series of UITextView s laid out successively in a UIScrollView , like so: while (lastRenderedGlyph < self.manager.numberOfGlyphs) { CGRect textViewFrame = CGRectMake(currentXOffset, 10, width / 2, height - 20); CGSize columnSize = CGSizeMake(CGRectGetWidth(textViewFrame) - 20, CGRectGetHeight(textViewFrame) - 10); NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize]; [self.manager addTextContainer:textContainer]; UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame textContainer:textContainer];

Calculate cell height for TextView with exclusion paths

纵然是瞬间 提交于 2019-12-05 23:36:00
问题 If I have a TextView with exclusion paths in a UITableViewCell , how can I calculate the cell's height for a given string? 回答1: I found a solution which I think might be of help to others. Since it does not require the creation of a new NSTextContainer, NSLayoutManager, and NSTextStorage object, which are already instantiated as part of the UITextView, I suspect it would be more efficient. To calculate the size of a UITextView that is using exclusions paths and NSAttributedString, one can do