2014-11-21 11 views
8

Sto tentando di giustificare il mio testo UILabel ma non funziona.NSTextAlignment.Justified per UILabel non funziona

Dichiarazione del mio UIView:

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

Dichiarazione del mio 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 

E sembra che questo:

enter image description here

non so che altro per usare quello NSTextAlignment.Justified per giustificare il mio testo. Dovrei invece usare un UITextView?

risposta

29

È necessario creare un NSMutableParagraphStyle in combinazione con una NSAribributoStringa per visualizzare il testo come giustificato. La parte importante è impostare NSBaselineOffsetAttributedName su 0.0.

Ecco un esempio di come mettere tutto insieme:

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) 

Crediti per NSBaselineOffsetAttributedName: https://stackoverflow.com/a/19445666/2494219

+0

Grazie! Preferisco mantenere un UILabel quindi non devo occuparmi di "selezione del testo", "scorrimento", ecc. Quindi la tua soluzione è fantastica, grazie. – magohamoth

+0

FYI: a partire da iOS 10 questo non è più necessario, sembra che 'UILabel' è stato corretto per l'uso di' NSTextAlignment.Justified' – kambala

-1

È possibile utilizzare UITextView per il vostro problema.

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

Grazie, ho provato e funziona come ma preferisco usare la soluzione @Devran così posso mantenere un UILabel che è quello che vorrei per questa implementazione (almeno per ora, ma tengo a mente la risposta!) – magohamoth

+0

Non dovresti usare un 'UITextView' al posto di un' UILabel' con testo giustificato. – Zorayr

-1

Objective-C, la soluzione di aggiornamento ideale è quello utilizzato NSMutableParagraphStyle test xCode 7 e 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 
} 

chiamata justifyLabel() funzione come questa ...

myLabel.attributedText = justifyLabel(myLabel.text!)