简体   繁体   中英

Text editing best practices (with tags, etc.)

I want to create a text field with some text, that is styled with tags. Think of MS Word, some ext is bold, some is a headline, etc. The tags that style the text are not visible to the user, he just sees the text in bold.

How do I preserve these tags? I mean when I backspace over the closing bold-tag, for example, how do I preserve that and prevent that the rest of the text is now bold until the end?

I guess this is a pretty basic question for experiences programmers. Are there any algorithms or best practices? This problem surely has been solved before, any pointers?

(If this is of any concern, I develop for the iPhone OS and the text field will be a UITextView, but my question is more general I guess.)

Thanks in advance!

与其将样式视为嵌入在文本中的打开/关闭标签,不如将其视为NSRanges并相应地进行调整。

As of 3.2 you can use NSAttributesString. Even if you roll your own, it is a good reference.

An attribute is applied to a range of characters. When characters are added or removed, the ranges are adjusted accordingly.

Tags are for a flattened format like HTML or RTF. In memory during editing you need complex data structures like DOM or NSAttributedString.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM