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,这样才会避免这个问题。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值