IOS – 在报纸上certificateUILabel

我正在尝试调整我的UILabel / UITextView,如下图所示。

任何人都有一个领导如何certificate这种方式的文本。

在此处输入图像描述

我也遇到了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]; 

尝试将UILabel的对齐方式设置为NSTextAlignmentJustified

 label.textAlignment = NSTextAlignmentJustified; 

Y. 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) } } 

我希望这有帮助!

完美的更新解决方案是在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; 

作为Y. Bonafons的优秀答案的一点点补充 – 您可以通过Interface Builder设置’ First Line Indent ‘来获得相同的结果(在xCode 7.3.1上检查)。