在iOS 6之前NSAttributedString是否可用?

时间:2012-11-15 00:15:37

标签: objective-c ios ios5 ios6

我发现在6之前版本的iOS版本的NSAttributedString的可用性方面存在冲突信息。如果它在某种程度上是向后兼容的话,它将非常有用。

这里说它从iOS 4开始提供: Why No NSAttributedString on the iPhone?

在Apple网站上,它说iOS 6及更高版本:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html

我尝试在我的代码中实现它,如下所示:

NSAttributedString *commentString = [[NSAttributedString alloc] initWithString:@"I really love raisins" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];

它适用于iOS 6但不是我尝试在iOS 5或iOS 5.1中运行时。我没有测试过早期版本。

我是否只是尝试使用旧版本中未包含的功能?

3 个答案:

答案 0 :(得分:14)

自iOS3.2以来它一直存在。但是,在UILabel,UIButton,UITextView等特定UI控件中使用它们的能力仅在iOS6之后才可用。您仍然可以使用CoreText for< 6.0。

答案 1 :(得分:6)

根据iOS参考文档,自iOS 3.2以来,

NSAttributeStringinitWithString:attributes:方法已经存在。在iOS 4.0中添加了NSAttributedString的某些方法。在iOS 6.0中添加了作为UIKit类别添加的所有方法。

这一行的问题:

NSAttributedString *commentString = [[NSAttributedString alloc] initWithString:@"I really love raisins" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];

是在iOS 6.0之前没有添加NSForegroundColorAttributeName常量。

所以虽然NSAttributedString已经存在了一段时间,但直到6.0才会添加所有有用的东西。

答案 2 :(得分:1)

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html

  

在iOS 6及更高版本中,您可以使用属性字符串在文本视图,文本字段和其他一些控件中显示格式化文本

这只是IOS6 afaik。该课程在IOS6之前可用,但功能性不可用。所以你的最终假设是正确的:)