NSTextAlignment对齐UILabel文本iOS6上的对齐将崩溃

时间:2012-12-19 07:42:20

标签: ios6 uilabel text-alignment

我现在使用Xcode4.5和iOS6,iOS6已经改变了UILable的textAlignment,

label.textAlignment = NSTextAlignmentJustified;

使用ios6 SDK,代码将在iPhone 6.0 Simulator上崩溃。但是在使用iOS6 SDK的iPhone 5.0模拟器上没有崩溃。

iOS6支持NSTextAlignmentJustified,但为什么会崩溃?

2 个答案:

答案 0 :(得分:10)

修改1:

使用NSAttributedString我们可以证明文字的合理性。

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment                = NSTextAlignmentJustified;
paragraphStyles.firstLineHeadIndent      = 0.05;    // Very IMP

NSString *stringTojustify                = @"We are what our thoughts have made us; so take care about what you think. Words are secondary. Thoughts live; they travel far.";
NSDictionary *attributes                 = @{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString     = [[NSAttributedString alloc] initWithString:stringTojustify attributes:attributes];

self.lblQuote.attributedText             = attributedString;
self.lblQuote.numberOfLines              = 0;
[self.lblQuote sizeToFit];

enter image description here

UITextAlignmentJustify的弃用,在iOS 6下不再可用。

SO link也提供了所有可能的合理文本解决方案 请查看this pageiOS6 prerelease Documentation

答案 1 :(得分:5)

我找到了一种在iOS 7中制作标签Justifiy的方法:我只需将NSBaselineOffsetAttributeName设置为0。

不知道为什么你需要在iOS 7上添加这个基线设置(它在iOS 6上运行正常)。

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = NSTextAlignmentJustified;

NSDictionary *attributes = @{ NSParagraphStyleAttributeName : paragraph,
                                        NSFontAttributeName : self.textLabel.font,
                              NSBaselineOffsetAttributeName : [NSNumber numberWithFloat:0] };

NSAttributedString *str = [[NSAttributedString alloc] initWithString:content 
                                                          attributes:attributes];

self.textLabel.attributedText = str;

希望有所帮助;)