2014-11-21 24 views
8

Ich versuche, meine UILabel Text zu rechtfertigen, aber es funktioniert nicht.NSTextAlignment.Justified für UILabel funktioniert nicht

Erklärung meines UIView:

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

Erklärung meines 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 

Und es sieht wie folgt aus:

enter image description here

Ich weiß nicht, was sonst um daszu verwendenum meinen Text zu rechtfertigen. Sollte ich stattdessen eine UITextView verwenden?

Antwort

29

Sie müssen einen NSMutableParagraphStyle in Kombination mit einem NSAttributedString erstellen, um Text als ausgerichtet anzuzeigen. Der wichtige Teil besteht darin, NSBaselineOffsetAttributedName auf 0.0 festzulegen.

Hier ist ein Beispiel, wie man alles zusammen:

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) 

Credits NSBaselineOffsetAttributedName: https://stackoverflow.com/a/19445666/2494219

+0

Danke! Ich ziehe es vor, ein UILabel zu behalten, damit ich mich nicht mit "Textauswahl", "Scrollen" usw. befassen muss. Ihre Lösung ist also großartig, danke. – magohamoth

+0

FYI: beginnend mit iOS 10 ist das nicht mehr erforderlich, scheint, dass 'UILabel' wurde für die Verwendung' NSTextAlignment.Justified' behoben. – kambala

-1

Sie können UITextView für Ihr Problem verwenden.

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

Vielen Dank, ich habe versucht und es funktioniert einige, aber ich bevorzuge, @Devran Lösung zu verwenden, damit ich kann behalte ein UILabel, was ich gerne für diese Implementierung hätte (zumindest für den Moment, aber ich behalte deine Antwort im Hinterkopf!) – magohamoth

+0

Du solltest kein 'UITextView' anstelle eines' UILabel' mit begründetem Text verwenden. – Zorayr

-1

Objective-C, die perfekte Lösung Update NSMutableParagraphStyle Test auf xCode 7 und 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 
} 

Anruf justifyLabel() Funktion wie diese verwendet ...

myLabel.attributedText = justifyLabel(myLabel.text!)