2014-11-21 93 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!)