Background: A few days ago suddenly want to do a notebook function, a start, think quite simple ah, a uitextview, network cache also do not dry, direct local nsuserdefault storage, and then finished, art, make a few nice pictures, add a few animation, and so on. Then, by convention, look at the existing memo software, its App store's well-known software: Weiconote, etc. I found it was not that simple. Here first not to consider the problem of picture and text mixed, the first thing to face is: After the keyboard hidden, the position of the word moved the problem. After careful experience found: The system memo, already some good software, will do some processing, for example: 1. When I edit, the cursor should always be hidden from the height of the keyboard. 2. I edit in the middle of the text, the editor to complete the hidden keyboard, Uitextview text location This time should be the part I just edited. Thereupon began to write: 1. Look at the exposed API, attributes, not by the keyboard height mask, frame,contentinset,textcontainerinset, can do. According to the notification  of the keyboard itself, or Uitextview delegate, can be easily done. Part of the code:  2. The workaround is cumbersome, looking at all the APIs, only the parent class of this API is useful:-(void) scrollRectToVisible: (cgrect) Rect animated: (bool) animate; The next question comes, based on the current position height of the cursor, manually calculate the property, then, get the position to move and jump. Because the default is: As long as the keyboard is hidden, it will automatically jump to the front of the Uitextview. As for the position of the cursor and the calculation of the position of the movement, the data can be found quickly. Here, the elder brother, also encountered the same problem, the solution is the same. The http://petersteinberger.com/blog/2014/fixing-uitextview-on-ios-7/ has also been shared with GitHub source. (Do not repeat things) in fact, problem 2 the best way to solve is: their own according to the Textkit package a uitextview... above the background is this, solve the problem, found that why not by the way the picture and text mixed problems also solved once. All along, rich text editing is not in-depth, but also directly with the open source code, or UIWebView, always in the mind is not practical, the situation occasionally occurs, very unhappy. So, next, is the main thing to share today: Rich text problem. &nbSP; keywords: coretext textkit third-party library analysis: Dtcoretext m80attributedlabel fasttext egotext official Website reference: UITextKit: https://developer.apple.com/library/ios/documentation/stringstextfonts/conceptual/textandwebiphoneos/ introduction/introduction.htmlcoretext: https://developer.apple.com/library/ios/documentation/ stringstextfonts/conceptual/coretext_programming/introduction/introduction.html#//apple_ref/doc/uid/tp40005533 textkit allows us to manually manage the font color, style (text layout), touch events, typography, graphics and the mix of rules. Textkit is based on Coretext, and WebKit is a two different branch. My main view is also Textkit, with coretext too troublesome. The logic of the relationship between them is as follows: specifically to the display, the rendering process is to first get the text from the stored nstextstorage and its attributes, and then through the Nslayoutmanager Layout display container including shape, size, etc. The Nstextcontainer layout is then displayed in a concrete display, which is finally shown on the UI. The process of rendering and implementation: Note here Description: By using multiple text containers, each with a associated text view, more complex layout arrangemen TS is possible. here, there is a spectrum of basic psychology. This logic is generally clear. Next, Subdivide: a. Here, first of all, to the text of the style, first on an official illustration: We can set the text of the font of various details, paragraph layout, on its way, indentation rules and so on. About style: Comes with six kinds, direct settings via Uifont: Uifonttextstyleheadline, uifoNttextstylesubheadline, uifonttextstylebody, uifonttextstylefootnot, uifonttextstylecaption1, uifonttextstylecaption2 b. Then began to write the text mixed with the reference code: Nsmutableattributedstring *string; An Image object is set by inserting nstextattachment, where nstextattachment can inherit the overridden method implementation to modify the size of the inserted content. Here is actually finding a nsrange position and inserting a picture, a common insert expression. Then the expression is converted into a string output. The string is then converted into a picture output. We can iterate through the input string, set the font of the string by nstextstorage, make custom rule processing, for example, # #进行标题大写, highlight, etc. The number of lines of input text is set by Nslayoutmanager, indentation rules of text segments, line spacing, and so on. Through the Nstextcontainer to set the layout of the text, such as the way the text wrapping, and so on. Basic text Kit functions like this, basically can meet all the requirements of rich text. Compose yourself and do something according to the business logic of your project. Finally add some of the libraries mentioned at the beginning, read good source analysis is better than anything: M80attributedlabel: Basic Textkit Package, code understandable, examples are very easy to use, tried the effect is very good, need to do some performance optimization processing. Dtcoretext:coretext package, parsing the regular HTML source code, display it into rich text, very powerful, if Android and iOS use the same set of interfaces, it is very useful. compatibility issues. Section Reference Links: http://esoftmobile.com/2013/10/17/text-kit%E8%BF%9B%E9%98%B6/#morehttps://github.com/ Xiangwangfeng/m80attributedlabelhttps://github.com/cocoanetics/dtcoretext?:dtcoretext run with hints that need to be done first: git Submodule init update
iOS Rich Text