2011-08-22 16 views
14

Sto riscontrando un problema che in iOS sto usando UILabel per visualizzare il testo a 2,3 righe, voglio allineare il testo come giustificato ma non trovo alcuna opzione per farlo. Qualche suggerimento su come giustificare il testo in etichetta?Giustifica il testo in UILabel iOS

Ho messo queste linea per fare iniziare da cima

CGSize maximumSize = CGSizeMake(300, 9999); 
NSString *textString = someString; 
UIFont *textFont = [UIFont fontWithName:@"Futura" size:14]; 
CGSize textStringSize = [textString sizeWithFont:textFont 
           constrainedToSize:maximumSize 
            lineBreakMode:text.lineBreakMode]; 

CGRect textFrame = CGRectMake(10, 110, 300, textStringSize.height); 
text.frame = textFrame; 

così qualche trucco come questo per renderlo justfiy Grazie

risposta

31

Ora c'è un modo conveniente per giustificare il testo da iOS6. È possibile creare un'istanza di NSAttributedString, impostare le proprietà appropriate e assegnare questa stringa attribuito ad un testo che rappresenta vista come UILabel, UITextView, ecc E 'facile come questo:

  1. creare un'istanza di NSMutableParagraphStyle e impostarne le proprietà .

    NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init]; 
    paragraphStyles.alignment = NSTextAlignmentJustified;  //justified text 
    paragraphStyles.firstLineHeadIndent = 10.0;    //must have a value to make it work 
    
  2. Creare NSDictionary per gli attributi di testo e creare una stringa attribuita.

    NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles}; 
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes]; 
    
  3. Imposta la stringa attribuita a un'etichetta.

    existingLabel.attributedText = attributedString; 
    
+1

"paragraphStyles.firstLineHeadIndent = 10.0" era proprio quello che stavo cercando. Grazie! – Neru

+0

Funziona come un fascino !! Grazie! –

4

non può essere fatto ho paura - anche non con UILabel .

È possibile utilizzare il UIWebView o una libreria di terze parti come OHAttributedLabel

Felice Coding :)

Aggiornamento:

Questa risposta è stata obsoleta dal iOS6. Si prega di fare riferimento alla risposta di Tankista.

+3

questa soluzione è ormai obsoleto, controllare il mio risposta qui sotto –

0

Può essere fatto facilmente, ma è necessario utilizzare Core Text.

sottoclasse un UIView, aggiungere una proprietà NSString, creare un NSAttributedString e passare kCTJustifiedTextAlignment valore per la chiave kCTParagraphStyleSpecifierAlignment, quindi disegnare il NSAttributedString utilizzando quarzo o CoreText nel metodo drawRect.

edit: kCTParagraphStyleSpecifierAlignment chiave valore kCTJustifiedTextAlignment dovrebbe essere utilizzato per creare una struct CTParagraphStyleRef e passato come un valore per kCTParagraphStyleAttributeName chiave durante la creazione del NSAttributedString.

0

Come menzionato da @martin, la mia classe OHAttributedLabel può farlo molto facilmente. (Lo troverai sul mio github e troverai anche molti riferimenti ad esso su SO)

+1

Hai tutto per quella nel file di intestazione, nessun problema a fare tutto questo. C'è un '' 'UITextAlignmentJustify''' #define (che equivale a' '' kCTJustifiedTextAlignment''') che puoi usare per applicare l'allineamento del testo su NSAttributedString (usando '' 'setTextAlignment: lineBreakMode:' '' metodo di NSAttributedString + Attributi .h).E se non vuoi che i link siano cliccabili, c'è anche una proprietà @ per questo: '' 'automaticallyAddLinksForType = 0''' non aggiungerà alcun collegamento su nessuno degli URL, né sui telefoni, né su nulla. – AliSoftware

+0

Da iOS6, è preferibile utilizzare "UILabel" nativo con "NSAttributedString". –

+0

@ Cœur Totalmente d'accordo (ecco perché 'OHAttributedLabel' è deprecato da qualche tempo a questa parte, BTW). Per chi fosse interessato, ho estratto tutte le pratiche categorie 'NSAttributedString' nel mio nuovo [OHAttributedStringAdditions] (https://github.com/AliSoftware/OHAttributedStringAdditions) pod ora, che aiuta a manipolare gli oggetti' NS (Mutable) AttributedString' più facilmente (ed è compatibile con iOS6 + e TextKit). – AliSoftware

Problemi correlati