'Rich text editor implementation with UITextView
I need to implement a text editor using UITextView that supports:
- Bold/Italic/Underline
- Color,Font,font size changes
- Paragraph alignment
- List format (bullets, numbers, etc.)
- Custom selection of text anywhere in the text view and change the properties
- Insert weblinks in the text
So far I have managed to do it without NSTextStorage but it seems I am hitting limits.For instance, if the user selects part of the text, it's possible that some range of the text has a different text font/color/Underline that the other parts of the text. If one were to modify attributes of NSMutableString under selection, there is a need to:
Get all the subranges of NSAttributedString having common attributes [How do I do this?],
Fetch those common attributes,
Apply the new attribute to selection after "resolving conflicts". [There may be some conflicts here to resolve. For instance, if all the text under selection is underlined, tapping underline would remove the underline. However if part of the text under selection is underlined while the remaining is not, tapping underline may either remove underline from all the selected text OR apply underline to all the selected text (this depends on the text editor as I see, TextEdit on Mac seems to prefer the latter approach). Similarly if part of the text is default color of text editor and part is green, applying Red color to selected text may make either the whole selection Red OR it may only make part of the text that had default color Red while leaving green text as it is. TextEdit on Mac again favours the former approach.]
In any case, the key here is to find attributes of all the ranges and merging them carefully as per custom choices. Keeping the 6 requirements mentioned in the beginning, what is the right approach to solve this problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
