NSAttributedString is used to implement simple text-and-text mixing, and textkit implements text-and-text Mixing
After iOS7, because TextKit is powerful, NSAttributedString can be used to conveniently implement text-and-image mixing (mainly using NSTextAttachment ). About Textkit awesome, you can refer to the article on objcio (https://objccn.io/issue-5-1/) I used NSAttributedString to imitate a knowledge of the input box (low imitation, there is a function ), effect demo here :( fourth of the https://github.com/Phelthas/TEST_XMLCommon) There are several places to note, worth record: 1, textView has a typingAttributes attribute, can make the input text automatically into NSAttributedString, you do not need to set AttributedText yourself. 2. textView has a selectedRange attribute, which refers to the position of the current cursor and can be used to determine the position of the inserted image. 3. If you open a breakpoint to observe textView. text can be found that the inserted image is replaced with a special UTF8 character "\ U0000fffc", which is not displayed on iOS devices., Not a space, not a line break, but it exists !!! It has a location, length (that is, 1), and a location in the entire string (nsage), so we should treat it as a placeholder character! This placeholder character makes it easier to upload data to the server and obtain data from the server. If it does not exist, you must record the image location, at this time, if there is any modification, it is very troublesome to update the positions of all images... 4. Set the bounds attribute for NSTextAttachment. Otherwise, the image will be displayed according to its own size. If the image is too large, it will not be displayed completely. Here, you can cut the image and show it when uploading the image, you can upload the source image or cropped image as needed to make the UI smoother. You can use string enumerateAttribute: NSAttachmentAttributeNameinRange: NSMakeRange (0, string. length) options: 0 usingBlock: This method is used to determine the final attributedString image, which is more convenient than recording the position each time an image is inserted ~ 6. I know that I uploaded the image to the server when inserting the image, so I used the user's typing time, users do not need to upload images at the end of the release. Although some traffic may be wasted (when users delete images), it is worthwhile to improve the user experience ~ 7. If data is retrieved from the server, it cannot be displayed in the same way when it is displayed, because the image must be asynchronously loaded, so we should use the conventional method at this time, displays a pile of text and images. You can directly arrange and combine the label and imageView, or use a tableView to display 8. textView and textField have an inputAccessoryView attribute, it automatically follows the keyboard. If it is not an input box that needs to be displayed all the time, it is much easier to use this attribute than to listen to keyboard notifications ~ 9. The placeholder character "\ U0000fffc" of the image is actually in the Attributes style. Therefore, sometimes the character after the image is deleted, the new display may be the default Attributes style. Therefore, it is safer to add the Attributes style to the placeholder character and the body. I wonder if there is any better way ~ For the time being, I will try again later ~ If you have any questions, please feel free to discuss them ~