2014-11-21 17 views
8

Próbuję uzasadnić mój tekst UILabel, ale to nie działa.NSTextAlignment.Justified dla UILabel nie działa

Deklaracja mojego UIView:

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

Deklaracja mojego 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 

A wygląda to tak:

enter image description here

nie wiem co jeszcze używać tego NSTextAlignment.Justified, aby usprawiedliwić mój tekst. Czy powinienem zamiast tego użyć UITextView?

Odpowiedz

29

Musisz utworzyć NSMutableParagraphStyle w połączeniu z NSAttributedString, aby wyświetlać tekst jako uzasadniony. Ważne jest ustawienie wartości NSBaselineOffsetAttributedName na 0.0.

Oto przykład jak umieścić wszystko razem:

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) 

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

+0

Dziękujemy! Wolę zachować UILabel, więc nie mam do czynienia z "wyborem tekstu", "przewijaniem" itp. Więc twoje rozwiązanie jest wspaniałe, dziękuję. – magohamoth

+0

FYI: począwszy od iOS 10 nie jest to już wymagane, wydaje się, że 'UILabel' został naprawiony do używania' NSTextAlignment.Justified' – kambala

-1

Można użyć UITextView dla swojego problemu.

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

Dziękuję, próbowałem i działa jak, ale ja wolę używać rozwiązania @Devran, więc mogę trzymaj UILabel, co jest tym, czego bym chciał dla tej implementacji (przynajmniej na razie, ale trzymam twoją odpowiedź na uwadze!) – magohamoth

+0

Nie powinieneś używać 'UITextView' zamiast' UILabel' z uzasadnionym tekstem. – Zorayr

-1

Cel C, idealnym rozwiązaniem jest aktualizacja używane NSMutableParagraphStyle testu na Xcode 7 i 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 
} 

połączenia justifyLabel (funkcja) jak to ...

myLabel.attributedText = justifyLabel(myLabel.text!) 
Powiązane problemy