NSTextAlignmentJustified到UILable textAligment在iOS6

2019-07-05 19:55发布

我用Xcode4.5和iOS6的现在,在iOS6的修改了UILable的textAlignment,

label.textAlignment = NSTextAlignmentJustified;

该代码会崩溃的iPhone 6.0模拟器与iOS6的SDK。 但不会崩溃的iPhone 5.0模拟器与iOS6的SDK。

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

Answer 1:

编辑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];

弃用UITextAlignmentJustify ,不再出售的iOS 6下。

以合理的文本的所有可能的解决方案,在此给出SO链接也看看这个页面和iOS6的抢鲜文档 。



Answer 2:

我有种找到了一种方法,使标签Justifiy在iOS的7:我简单地设置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;

希望帮助;)



文章来源: NSTextAlignmentJustified to UILable textAligment on iOS6 will crash