将文本从plist解析为NSAttributedString [英] Parsing text from plist to NSAttributedString
问题描述
我正在从plist加载文本并在我的应用程序中显示它。理想情况下,我希望能够指定更复杂的格式,如斜体,粗体和上标文本,并将其显示在自定义标签中,例如 TTTAttributedLabel 。
I'm loading in text from a plist and displaying it within my app. Ideally I want to be able to specify more complex formatting such as italics, bold and superscript text, and display it in a custom label such as TTTAttributedLabel.
是否有任何可用的库将给定格式的字符串(最好是简单的文本格式,如Markdown或Textile)解析为NSAttributedString?我知道解决方案可用于解析RTF和HTML,但这对我的需求来说太过分了 - 而且我希望手动编写文本。
Are there any available libraries to parse a string in a given format (preferably a simple text format such as Markdown or Textile) into an NSAttributedString? I am aware that solutions are available for parsing RTF and HTML, but this is overkill for my needs - plus I'd like the text to be easily written by hand.
编辑:这是针对iOS / UIKit
this is for iOS/UIKit
推荐答案
我刚刚添加了 NSString
到 NSAttributedString
MGBoxKit 的轻量级标记解析器。它不是Markdown或Textile,但它非常相似。到目前为止,它支持粗体,斜体,下划线,单圈和彩色文本。
I've just added an NSString
to NSAttributedString
lightweight markup parser to MGBoxKit. It's not Markdown or Textile but it's very similar. So far it supports bold, italics, underline, monospacing, and coloured text.
MGMushParser 类可以单独使用,并且相当容易扩展。
The MGMushParser class could be used standalone, and is fairly easy to extend.
NSString *markup = @"**bold**, //italics//, __underlining__, and `monospacing`, and {#0000FF|coloured text}";
UIFont *baseFont = [UIFont fontWithName:@"HelveticaNeue" size:18];
UIColor *textColor = UIColor.whiteColor;
myLabel.attributedString = [MGMushParser attributedStringFromMush:markup
font:baseFont color:textColor];
OHAttributedLabel 也有类似的标记解析器。
OHAttributedLabel also has a similar markup parser.
这篇关于将文本从plist解析为NSAttributedString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!