2014-11-21 167 views
8

我想证明我的UILabel文本,但它不起作用。我UIViewNSTextAlignment.Josetified for UILabel does not work

声明:

descriptionUIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height) 

UILabel宣言:

bottleDescriptionLabel = UILabel(frame: CGRect(x: widthMargin, y: bottleDescriptionTitleLabel.frame.maxY + heightMargin, width: self.view.frame.width - (2 * widthMargin), height: heightBottleDescription - (2 * heightMargin))) 
bottleDescriptionLabel.font = UIFont(name: "AvenirNext-Regular", size: 16) 
bottleDescriptionLabel.text = bottleDescriptionString 
bottleDescriptionLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping 
bottleDescriptionLabel.textAlignment = NSTextAlignment.Justified 
bottleDescriptionLabel.numberOfLines = 0 

它看起来是这样的:

enter image description here

,我不知道还有什么使用NSTextAlignment.Justified来证明我的文本。我应该使用UITextView吗?

回答

29

您必须创建一个与NSAttributedString结合使用的NSMutableParagraphStyle才能以正确的方式显示文本。 重要的部分是将NSBaselineOffsetAttributedName设置为0.0。

下面是一个例子,如何把一切融合在一起:

let sampleText = "Lorem ipsum dolor sit amet, consectetur adipisicing 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 paragraphStyle = NSMutableParagraphStyle() 
paragraphStyle.alignment = NSTextAlignment.Justified 

let attributedString = NSAttributedString(string: sampleText, 
    attributes: [ 
     NSParagraphStyleAttributeName: paragraphStyle, 
     NSBaselineOffsetAttributeName: NSNumber(float: 0) 
    ]) 

let label = UILabel() 
label.attributedText = attributedString 
label.numberOfLines = 0 
label.frame = CGRectMake(0, 0, 400, 400) 


let view = UIView() 
view.frame = CGRectMake(0, 0, 400, 400) 
view.addSubview(label) 

学分NSBaselineOffsetAttributedName:https://stackoverflow.com/a/19445666/2494219

+0

谢谢!我更喜欢保留一个UILabel,所以我不必处理“文本选择”,“滚动”等。因此,您的解决方案非常好,谢谢。 – magohamoth 2014-11-21 12:02:50

+0

仅供参考:从iOS 10开始,这不再是必需的,似乎'UILabel'已被固定为使用'NSTextAlignment.Justified' – kambala 2018-01-11 20:28:45

-1

可以使用的UITextView您的问题。

bottleDescriptionTextView = UITextView(frame: CGRect(x: widthMargin, y: bottleDescriptionTextView.frame.maxY + heightMargin, width: self.view.frame.width - (2 * widthMargin), height: heightBottleDescription - (2 * heightMargin))) 
bottleDescriptionTextView.font = UIFont(name: "AvenirNext-Regular", size: 16) 
bottleDescriptionTextView.text = bottleDescriptionString 
bottleDescriptionTextView.textAlignment = NSTextAlignment.Justified 
+0

谢谢,我试过了,它的工作原理如何,但我更喜欢使用@Devran解决方案,所以我可以保留一个UILabel,这是我想要的这个实现(至少现在,但我始终记住你的答案!) – magohamoth 2014-11-21 12:03:44

+0

你不应该使用'UITextView'来替代具有正确文本的'UILabel'。 – Zorayr 2015-03-29 05:56:39

-1

Objective-C的,完美的更新的解决方案是使用NSMutableParagraphStyle测试上XCODE 7和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; 
3
func justifyLabel(str: String) -> NSAttributedString 
{ 
    let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.alignment = NSTextAlignment.Justified 
    let attributedString = NSAttributedString(string: str, 
               attributes: [ 
               NSParagraphStyleAttributeName: paragraphStyle, 
               NSBaselineOffsetAttributeName: NSNumber(float: 0) 
     ]) 

    return attributedString 
} 

呼叫justifyLabel()这样的功能...

myLabel.attributedText = justifyLabel(myLabel.text!)