我试图调整我的UILabel / UITextView,如下图所示。
任何人都有一个领导如何以这种方式证明文本。
答案 0 :(得分:8)
我也遇到了NSTextAlignementJustified
的问题。
你可以通过使用NSAttributedString来实现这一点,通过设置对齐方式并将firstLineHeadIndent设置为接近零(为什么?嗯......我不知道):
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];
答案 1 :(得分:3)
尝试设置UILabel
与NSTextAlignmentJustified
label.textAlignment = NSTextAlignmentJustified;
答案 2 :(得分:2)
ÿ。 Bonafons谢谢你!作为Swift的新手,我花了很长时间才使用你的代码,所以我在这里为Swift 2打字:
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)
}
}
我希望这有帮助!
答案 3 :(得分:1)
完美的更新解决方案是在xCode 7和iOS 9上使用NSMutableParagraphStyle
测试
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;
答案 4 :(得分:0)
作为Y. Bonafons的优秀答案的一点点补充 - 您可以通过设置' First Line Indent'来实现相同的结果。通过Interface Builder(在xCode 7.3.1上检查)。