NSTextAlignmentJustified not working in iOS7

感情迁移 提交于 2019-12-10 02:11:47

问题


I have an app which uses NSTextAlignmentJustified for an NSAttributedString. In iOS 6 everything is working great. But the same App running in iOS 7 (simulator or device makes no difference) is showing no Justify at all. Also the linespacing seems to have changed dramatically from iOS 6 to 7.

Anyone else encountered this problem? Is there any way to make a justified Textblock in iOS 7 (which works in iOS 6 too?)

Regards, Markus


回答1:


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

No idea why it works, but it works.

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentJustified;

NSAttributedString *string = [[NSAttributedString alloc] initWithString:rawString 
          attributes:[NSDictionary dictionaryWithObjectsAndKeys:
          paragraphStyle, NSParagraphStyleAttributeName , 
          [NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName, 
          nil]];



回答2:


Setting firstLineHeadIndent on NSMutableParagraphStyle will also work too.

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment                = NSTextAlignmentJustified;    // To justified text
paragraphStyles.firstLineHeadIndent      = 0.05;    // IMP: must have a value to make it work

NSString *stringTojustify                = @"No one wakes up excited to see more advertising, no one goes to sleep thinking about the ads they’ll see tomorrow.";
NSDictionary *attributes                 = @{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString     = [[NSAttributedString alloc] initWithString:stringTojustify attributes:attributes];

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



回答3:


Just for the record, you could also append a '\n' as first character for a plain UILabel.

        self.text = [NSString stringWithFormat:@"\n%@",TextString];
        {
            CurFrame.origin.y -= FontSize;
            self.frame = CurFrame;
        }
        self.textAlignment = NSTextAlignmentJustified;


来源:https://stackoverflow.com/questions/19252395/nstextalignmentjustified-not-working-in-ios7

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