2014-05-10 13 views
5

Sono abbastanza sicuro che questo sia in realtà un bug UIKit ma voglio ottenere un input per vedere se mi manca qualcosa di stupido qui.Dimensioni UILabel errate per singola riga di testo con lineSpacing e più colori

Ecco il codice che ho:

// single line with modified line spacing and 2 colors - broken, line spacing is added to the bottom! 
UILabel *brokenLabel = [[UILabel alloc] init]; 
brokenLabel.backgroundColor = [UIColor greenColor]; 

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"]; 

[attributedString addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:@"text"]]; 

attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter); 

brokenLabel.attributedText = attributedString; 
[brokenLabel sizeToFit]; 
brokenLabel.frame = CGRectOffset(brokenLabel.frame, 50, 100); 
[self.view addSubview:brokenLabel]; 
// end 

// single line with modified line spacing and 1 color - correct 
UILabel *workingLabel = [[UILabel alloc] init]; 
workingLabel.backgroundColor = [UIColor greenColor]; 

attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"]; 

attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter); 

workingLabel.attributedText = attributedString; 
[workingLabel sizeToFit]; 
workingLabel.frame = CGRectOffset(workingLabel.frame, 200, 100); 
[self.view addSubview:workingLabel]; 
//end 

// multiple lines with modified line spacing and 1 color - correct 
UILabel *workingLabel2 = [[UILabel alloc] init]; 
workingLabel2.frame = CGRectMake(0, 0, 100, 0); 
workingLabel2.numberOfLines = 0; 
workingLabel2.backgroundColor = [UIColor greenColor]; 

attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"]; 

attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter); 

workingLabel2.attributedText = attributedString; 
[workingLabel2 sizeToFit]; 
workingLabel2.frame = CGRectOffset(workingLabel2.frame, 50, 300); 
[self.view addSubview:workingLabel2]; 
//end 

// multiple lines with modified line spacing and 2 color - correct 
UILabel *workingLabel3 = [[UILabel alloc] init]; 
workingLabel3.frame = CGRectMake(0, 0, 100, 0); 
workingLabel3.numberOfLines = 0; 
workingLabel3.backgroundColor = [UIColor greenColor]; 

attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"]; 

[attributedString addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:@"text"]]; 

attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter); 

workingLabel3.attributedText = attributedString; 
[workingLabel3 sizeToFit]; 
workingLabel3.frame = CGRectOffset(workingLabel3.frame, 200, 300); 
[self.view addSubview:workingLabel3]; 

insieme ad una semplice funzione di convenienza per cambiare il lineSpacing di una stringa attribuito:

NSMutableAttributedString *attributedStringFromAttributedStringWithLineSpacing(NSAttributedString *string, CGFloat lineSpacing, NSTextAlignment textAlignment) 
{ 
    NSMutableAttributedString *mutable = string.mutableCopy; 

    NSMutableParagraphStyle *par = [NSMutableParagraphStyle new]; 
    par.alignment = textAlignment; 
    par.lineSpacing = lineSpacing; 
    [mutable addAttributes:@{NSParagraphStyleAttributeName : par} range:NSMakeRange(0, mutable.length)]; 
    return mutable; 
} 

Tuttavia, questo è quello che sembra

image

Come puoi vedere, l'altezza della prima etichetta è troppo grande (o l'altezza che dovrebbe essere + la mia spaziatura personalizzata, per essere precisi). Quando si aggiunge semplicemente un altro colore alla prima stringa attribuita, si aumenta la dimensione sizeToFit aggiungendo lo lineSpacing sotto di esso. Ho anche provato a utilizzare i metodi boundingRectWithSize: sulle stringhe direttamente e lo stesso problema è visibile. Quindi questo non è specifico per il codice di dimensionamento delle etichette ma è un problema con le stringhe stesse. Non vedo alcuna ragione possibile per cui questo dovrebbe accadere. Qualcuno ha qualche idea?

+0

E se non si aggiunge interlinea personalizzato nel primo campione - quale sarebbe il telaio? – sha

+0

Funziona bene senza l'interlinea personalizzata, o almeno sembra che faccia. Sospetto che cerchi ancora di aggiungerlo in fondo, ma poiché il valore è 0, non avrebbe alcun effetto reale e apparirà bene. – Dima

+0

hai risolto questo? FWIW UITextView non ha questo problema – bogardon

risposta

0

Nei tuoi Attributi a Dizionario aggiungere

[attrDic setObject:@0 forKey:NSBaselineOffsetAttributeName]; 
+0

Ho provato questo in alcuni posti diversi e sfortunatamente non fa esattamente niente. Puoi elaborare e dove o come dovrei impostare questo attributo in quanto si riferisce esattamente al mio codice? Hai provato con il codice che ho fornito sopra? – Dima

Problemi correlati