Detailed TextKit parsing and detailed TextKit Parsing
1. A rough understanding of TextKit
In iOS development, there are four View Controls for text processing: UILabel, UITextField, UITextView, and UIWebView. UILabel and UITextField are relatively simple. UITextView is a fully functional text layout display class. It can be used for complex Rich Text la S. UIWebView is mainly used to load webpages or PDF files, which can be HTML, parse CSS and JS files.
TextKit is a development framework based on the upper layer. It is available in iOS7 and later versions. With TextKit, developers can conveniently and flexibly process complex text la s to meet various complex requirements of text la s in development. TextKit is actually an upper-layer framework based on CoreText. It Is Object-Oriented. If the APIS provided in TextKit cannot meet the requirements, you can use the APIs in CoreText for underlying development.
An image in the official document is very accurate and is often used to describe the position of the TextKit framework in iOS text rendering.
2. TextKit Framework Structure
The following conditions are required for text rendering on the interface:
1. the content to be rendered.
2. render the content on a view.
3. The size position and shape of the Content Rendering on The View.
In the TextKit framework, several classes are provided to correspond to the preceding conditions:
1. NSTextStorage corresponds to the content to be rendered.
2. UITextView corresponds to the view to be rendered.
3. NSTextContainer corresponds to the rendering size position and shape information.
In addition to the preceding three classes, the NSLayoutManager class in the TextKit framework acts as the coordinator to perform layout operations.
Shows the preceding relationship:
3. Use TextKit for text Layout
TextKit is mainly used to process text layout more precisely and perform complex text-and-text layout. It is very cumbersome to use TextKit to display text layout. First, you need to define the display content as an NSTextStorage object, then add a layout manager object NSLayoutManager. In NSLayoutManager, You need to define NSTextContainer. if the definition is more than the NSTextContainer object, the text will be paginated. Finally, bind the NSTextContainer to the specific UITextView. In other words, we can use TextKit to completely control the position and style of each glyph.
Is the relationship between them:
The following figure shows my understanding: the NSTextStorage object stores the content, text, and images to be displayed. In my opinion, the most important class is NSLayoutManager, which manages the display of each character. NSTextContainer manages the layout of each line of font and then displays the text by binding a line of TextView. Of course, the underlying Rendering uses coreText.
In this way, can we modify the NSTextContainer display area to change the text display collision, which is similar to a triangle or circle. The answer is yes, of course. We need to rewrite the content in NSTextContainer.
Override func lineFragmentRect (forProposedRect proposedRect: CGRect, at characterIndex: Int, writingDirection baseWritingDirection: NSWritingDirection, remaining remainingRect: UnsafeMutablePointer )-> CGRect
What does this method mean? The explanation in the official document: return the rect drawn by each line of text, so that we can change the rect point to Implement Circular text.
Override func lineFragmentRect (forProposedRect proposedRect: CGRect, at characterIndex: Int, writingDirection baseWritingDirection: NSWritingDirection, remaining remainingRect: UnsafeMutablePointer )-> CGRect {
Let size = self. size
Let rect = super. lineFragmentRect (forProposedRect: proposedRect, at: characterIndex, writingDirection: baseWritingDirection, remaining: remainingRect)
Let radius = fmin (size. width, size. height)/2
Let ypos = fabs (rect. origin. y + rect. size. height/2)-radius)
Let width = (ypos <radius) 2 * sqrt (radius * radius-ypos * ypos): 0.0
Let finalRect = CGRect. init (x: rect. origin. x + radius-width/2.0, y: rect. origin. y, width: width, height: rect. size. height)
Return finalRect
}
In this way, we can replace the textiner of textView with our custom one to implement a circular text.
Of course, we can also modify NSLayoutManager. Now each line of text is neatly arranged, so we can modify the position of each character and arrange it with a slash.
Override func draw (_ rect: CGRect ){
// Obtain the position of the corresponding font
Var range = nsange. init (location: 0, length: 0)
_ = LayoutManager. lineFragmentRect (forGlyphAt: 0, interval tiverange: & range)
For glyIndex in range. location... (range. location + range. length-1 ){
Let point = layoutManager. location (forGlyphAt: glyIndex)
Print (point)
// Draw text
Self. layoutManager. drawGlyphs (forGlyphRange: nsange. init (location: glyIndex, length: 1), at: CGPoint. init (x: 10, y: 20 * glyIndex ))
}
}
TextKit is probably used in this way. There are many methods in NSLayoutManager, which can be used flexibly to make cool results.