I'm trying to align my UILabel/UITextView like in the picture below.
Anyone got a lead how to justify the text that way.

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];
Try to set the alignment of your UILabel
to NSTextAlignmentJustified
label.textAlignment = NSTextAlignmentJustified;
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).
来源:https://stackoverflow.com/questions/27547847/ios-justify-uilabel-like-in-the-newspaper