iphone/ipad:究竟如何使用 NSAttributedString?
是的,很多人都在谈论 iPhone/iPad 中的富文本,并且很多人都知道 NSAttributedString
。
但是如何使用NSAttributedString
呢?我搜索了很多时间,没有提取任何线索。
我知道如何设置NSAttributedString
,那么我该怎么做才能在iPhone/iPad上以富文本形式显示文本呢?
官方文档说它应该与 CoreText.Framework 一起使用,这是什么意思?
有这样简单的方法吗?
NSAttributedString *str;
.....
UILabel *label;
label.attributedString = str;
Yes, many people are saying about Rich Text in iPhone/iPad and many knows about NSAttributedString
.
But how to use NSAttributedString
? I searched for much time, no extract clues for this.
I know how to set up a NSAttributedString
, then what should I do to display a text on iPhone/iPad with rich text?
The official docs says it should be used with CoreText.Framework
, what does that mean?
Is there any simple way like this?
NSAttributedString *str;
.....
UILabel *label;
label.attributedString = str;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从iOS 6.0开始你可以这样做:
Starting from the iOS 6.0 you can do it like that:
您应该查看 AliSoftware 的 OHAttributedLabel。它是 UILabel 的子类,它绘制 NSAttributedString 并且还提供了从 UIKit 类设置 NSAttributedString 属性的便捷方法。
从存储库中提供的示例中:
注意:在 iOS 6+ 中,您可以使用 attributedText UILabel 的属性。
You should take a look at AliSoftware's OHAttributedLabel. It is a subclass of UILabel that draws an NSAttributedString and also provides convenience methods for setting the attributes of an NSAttributedString from UIKit classes.
From the sample provided in the repo:
Note: In iOS 6+ you can render attributed strings using the attributedText property of UILabel.
您应该尝试 TTTAttributedLabel。它是 UILabel 的直接替代品,可与 NSAttributedString 配合使用,并且对于 UITableViewCells 来说具有足够的性能。
You should try TTTAttributedLabel. It's a drop-in replacement for UILabel that works with NSAttributedString and is performant enough for UITableViewCells.
几乎。只需使用 CATextLayer 即可。它有一个
string
属性,您可以将其设置为 NSAttributedString。编辑(2012 年 11 月):当然,这一切在 iOS 6 中都发生了变化。在 iOS 6 中,您可以完全按照 OP 的要求进行操作 - 将属性字符串直接分配给标签的
attributedText
。Almost. Just use a CATextLayer. It has a
string
property that you can set to an NSAttributedString.EDIT (November, 2012): Of course all this has changed in iOS 6. In iOS 6, you can do exactly what the OP asked for - assign an attributed string directly to a label's
attributedText
.iOS 6 上 UILabel 属性文本对齐的答案:
使用 NSMutableAttributedString 并将 NSMutableParagraphStyle 添加到属性中。
像这样的东西:
Answer for UILabel attributed text alignment on iOS 6:
Use NSMutableAttributedString and add NSMutableParagraphStyle to the attribute.
Something like this:
我认为给出一个解析(简化的)HTML 字符串以创建 NSAttributedString 的示例会很有用。
它并不完整 - 它只处理 和对于初学者来说,标签,并且不关心任何错误处理 - 但希望也是如何开始使用 NSXMLParserDelegate 的有用示例......
要使用,请执行以下操作:
I thought it would be useful to give an example of parsing a (simplified) HTML string, to create an NSAttributedString.
It isn't complete - it only handles <b> and <i> tags, for starters, and doesn't bother with any error handling - but is hopefully also a useful example of how to get started with NSXMLParserDelegate ...
To use, do something like this:
从 iOS 6.0 开始,您可以这样做:另一个示例代码。
Starting from the iOS 6.0 you can do it like that: another sample code.
对于Swift使用这个,
它将使Titl文本加粗,
For Swift use this,
It will make Titl texts bold,
我知道有点晚了,但是对其他人很有用,将
所需的属性添加到字典中并将其作为属性参数传递
I know it is little bit late, But it will be useful to other,
Add the desired attribute to the dictionary and pass it as a attributes parameter