如何在iPhone上显示NSAttributedString? [重复]

时间:2022-10-30 11:47:40

This question already has an answer here:

这个问题在这里已有答案:

I believe I understand how to set NSAttributedString values but how on earth do you actually display them in the interface?

我相信我理解如何设置NSAttributedString值,但实际上你如何在界面中显示它们?

examples: UILabel, UITextView, etc.

示例:UILabel,UITextView等

Specific instructions would be greatly appreciated/

非常感谢具体说明/

  • Thanks!
  • 谢谢!

4 个解决方案

#1


2  

you could also create a CATextLayer, which works fine with attributed strings... Just create the layer, and set it's string property...

你也可以创建一个CATextLayer,它可以很好地处理属性字符串...只需创建图层,并设置它的字符串属性...

Have fun... :D

玩得开心......:D

#2


2  

If you don't want to bother with CATextLayer, you should check out TTTAttributedLabel. It's just like UILabel, except that supports NSAttributedStrings (using the same setText: method as UILabel, so you can use it as a drop-in replacement)

如果您不想打扰CATextLayer,您应该查看TTTAttributedLabel。它就像UILabel,除了支持NSAttributedStrings(使用与UILabel相同的setText:方法,因此您可以将其用作替代品)

#3


2  

In iOS 6 and later you can use attributed strings to display formatted text in text views, text fields, and some other controls. Both AppKit and UIKit also define extensions to the basic attributed string interface that allows you to draw their contents in the current graphic context.

在iOS 6及更高版本中,您可以使用属性字符串在文本视图,文本字段和一些其他控件中显示格式化文本。 AppKit和UIKit都定义了基本属性字符串接口的扩展,允许您在当前图形上下文中绘制其内容。

NSAttributedString Reference

NSAttributedString参考

#4


1  

The controls you mentioned only work with strings, not attributed strings. The only way to display them is to do a lot of work with Core Text. File a complaint with bugreporter.apple.com to request they flesh this part of UIKit out.

您提到的控件只能使用字符串,而不是属于字符串。显示它们的唯一方法是使用Core Text进行大量工作。向bugreporter.apple.com提出投诉,要求他们将UIKit的这部分内容输出。

#1


2  

you could also create a CATextLayer, which works fine with attributed strings... Just create the layer, and set it's string property...

你也可以创建一个CATextLayer,它可以很好地处理属性字符串...只需创建图层,并设置它的字符串属性...

Have fun... :D

玩得开心......:D

#2


2  

If you don't want to bother with CATextLayer, you should check out TTTAttributedLabel. It's just like UILabel, except that supports NSAttributedStrings (using the same setText: method as UILabel, so you can use it as a drop-in replacement)

如果您不想打扰CATextLayer,您应该查看TTTAttributedLabel。它就像UILabel,除了支持NSAttributedStrings(使用与UILabel相同的setText:方法,因此您可以将其用作替代品)

#3


2  

In iOS 6 and later you can use attributed strings to display formatted text in text views, text fields, and some other controls. Both AppKit and UIKit also define extensions to the basic attributed string interface that allows you to draw their contents in the current graphic context.

在iOS 6及更高版本中,您可以使用属性字符串在文本视图,文本字段和一些其他控件中显示格式化文本。 AppKit和UIKit都定义了基本属性字符串接口的扩展,允许您在当前图形上下文中绘制其内容。

NSAttributedString Reference

NSAttributedString参考

#4


1  

The controls you mentioned only work with strings, not attributed strings. The only way to display them is to do a lot of work with Core Text. File a complaint with bugreporter.apple.com to request they flesh this part of UIKit out.

您提到的控件只能使用字符串,而不是属于字符串。显示它们的唯一方法是使用Core Text进行大量工作。向bugreporter.apple.com提出投诉,要求他们将UIKit的这部分内容输出。