IOS - Justify UILabel like in the newspaper

自作多情 提交于 2019-12-08 14:36:24

I have some trouble with NSTextAlignementJustified too.

You can achieve this with the use of NSAttributedString, by setting the alignment and by setting firstLineHeadIndent to something close to zero (why? hmmmm... well I don't know):

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment                = NSTextAlignmentJustified;
paragraphStyles.firstLineHeadIndent      = 0.001f;
NSString *stringTojustify                = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
NSDictionary *attributes                 = @{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString     = [[NSAttributedString alloc] initWithString:stringTojustify attributes:attributes];

CGFloat labelWidth = self.view.frame.size.width * 0.7f;
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.5f - labelWidth * 0.5f,
                                                             100,
                                                             labelWidth,
                                                             800)];
myLabel.backgroundColor = [UIColor orangeColor];
myLabel.textColor = [UIColor whiteColor];
myLabel.numberOfLines = 0;
myLabel.attributedText = attributedString;
[myLabel sizeToFit];

[self.view addSubview:myLabel];
madhu

Try to set the alignment of your UILabel to NSTextAlignmentJustified

label.textAlignment = NSTextAlignmentJustified;
Stelios Serghiou

Y. Bonafons thank you! Being new to Swift, it took me quite a while to use your code, so I'm typing that for Swift 2 here:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        let labelWidth = CGFloat(self.view.frame.size.width * 0.7)

        let explanationLabel = UILabel(frame: CGRectMake(self.view.frame.size.width * 0.5 - labelWidth * 0.5, 100, labelWidth, 800))

        let paragraphStyle = NSMutableParagraphStyle()

        paragraphStyle.alignment = NSTextAlignment.Justified

        paragraphStyle.firstLineHeadIndent = 15

        paragraphStyle.paragraphSpacingBefore = 10.0

        explanationLabel.attributedText = NSAttributedString(string: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum")

        let mutableAttrStr = NSMutableAttributedString(attributedString: explanationLabel.attributedText!)

        mutableAttrStr.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, mutableAttrStr.length))

        explanationLabel.backgroundColor = UIColor.orangeColor()

        explanationLabel.textColor = UIColor.whiteColor()

        explanationLabel.numberOfLines = 0

        explanationLabel.attributedText = mutableAttrStr

        explanationLabel.sizeToFit()

        self.view.addSubview(explanationLabel)

    }

}

I hope this helps!

The perfect update solution is to used NSMutableParagraphStyle test on xCode 7 and iOS 9

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
        paragraphStyles.alignment = NSTextAlignmentJustified;      //justified text
        paragraphStyles.firstLineHeadIndent = 1.0;
        NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: YourString attributes: attributes];
        YourLabel.attributedText = attributedString;

As a little addition to the great answer of Y. Bonafons - you can achieve the same result by setting 'First Line Indent' via Interface Builder (checked on xCode 7.3.1).

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