NSTextAlignmentJustified ios6 崩溃问题

今天遇到一个问题,设置UILabel 的textAlignment属性为NSTextAlignmentJustified ,在ios6.1.3上每次都崩溃,

代码的:

_questionContentLabel.textAlignment = NSTextAlignmentJustified;
            

崩溃日志:

Error log is : *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textAlignment does not accept NSTextAlignmentJustified'


我就莫名其妙了,然后马上看了看apple 的document发现如下

typedef NS_ENUM(NSInteger, NSTextAlignment) {
    NSTextAlignmentLeft      = 0,    // Visually left aligned
#if TARGET_OS_IPHONE
    NSTextAlignmentCenter    = 1,    // Visually centered
    NSTextAlignmentRight     = 2,    // Visually right aligned
#else /* !TARGET_OS_IPHONE */
    NSTextAlignmentRight     = 1,    // Visually right aligned
    NSTextAlignmentCenter    = 2,    // Visually centered
#endif
    NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
    NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script
} NS_ENUM_AVAILABLE_IOS(6_0);

NSTextAlignmentJustified在ios6以后都可以使用啊,但是为什么崩溃,很纳闷,就开始继续研究官方的文档。

功夫不负有心人,找到答案了。

UIKIt

NSString drawing additions and UILabel do not support NSTextAlignmentJustified orNSTextAlignmentNatural text alignments. Instead, use NSAttributedString drawing additions, which do support text alignments.
UILabel

If you are using styled text in iOS 6 or later, assigning a new value to this property causes the text alignment to be applied to the entirety of the string in the attributedText property. If you want to apply the alignment to only a portion of the text, create a new attributed string with the desired style information and associate it with the label. If you are not using styled text, this property applies to the entire text string in the text property.

通过官方文档说明我们用nsstring 是不行的,必须使用nsattributestring,这样才会避免这个问题。



你可能感兴趣的:(NSTextAlignmentJustified ios6 崩溃问题)