NSAttributedString在iOS 6之前是否可用?

我发现6之前版本的iOS版本的NSAttributedString的可用性信息是有冲突的。如果它稍微向后兼容的话,这是非常有用的。

这里说它从iOS 4开始可用: 为什么iPhone上没有NSAttributedString?

在苹果网站上,它说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中运行时。 我没有testing过早期的版本。

我只是试图使用不包含在旧版本中的function吗?

从iOS3.2开始就已经有了。 但是,在某些UI控件(如UILabel,UIButton,UITextView等)中使用它们的function仅在iOS6以后才可用。 您仍然可以使用CoreText <6.0来使用它们。

根据iOS参考文档, NSAttributeStringinitWithString:attributes:方法已经在iOS 3.2之后。 在iOS 4.0中添加了一些NSAttributedString方法。 所有添加为UIKit类别的方法都是在iOS 6.0中添加的。

这一行的问题:

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

NSForegroundColorAttributeName常量没有添加,直到iOS 6.0。

所以,虽然NSAttributedString已经有一段时间了,但所有有用的东西都是6.0以前才添加的。

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

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

这只是IOS6 afaik。 课程在IOS6之前是可用的,但function不可用。 所以你最后的假设是正确的:)