NSTextAlignmentJustified to UILable textAligment on iOS6 will crash

大憨熊 提交于 2019-11-30 03:18:58

问题


I am use Xcode4.5 and iOS6 now, the iOS6 have change the UILable's textAlignment,

label.textAlignment = NSTextAlignmentJustified;

The code will crash on iPhone 6.0 Simulator with ios6 SDK. but not crash on iPhone 5.0 Simulator with iOS6 SDK.

The iOS6 support NSTextAlignmentJustified, but why will crash?


回答1:


Edit 1:

Using NSAttributedString we can justify text.

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

Deprecation of UITextAlignmentJustify, not available anymore under iOS 6.

All possible solutions to justified text are given at this SO link also have a look at this page and iOS6 prerelease Documentation.




回答2:


I kind of found a way to make the label Justifiy in iOS 7: i simply set NSBaselineOffsetAttributeName to 0.

No idea why you need to add this baseline setting on iOS 7, (it works fine on 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;

Hope that helps ;)



来源:https://stackoverflow.com/questions/13947650/nstextalignmentjustified-to-uilable-textaligment-on-ios6-will-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!