Ich bin mir ziemlich sicher, dass dies tatsächlich ein UIKit-Fehler ist, aber ein paar Eingaben erhalten möchten, um zu sehen, ob ich etwas Dummes hier vermisse.UILabel Größe falsch für einzelne Zeile des Textes mit lineSpacing und mehreren Farben
Hier ist der Code ich habe:
// 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];
zusammen mit einer einfachen Komfortfunktion der lineSpacing
ein zugeschrieben Zeichenfolge zu ändern:
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;
}
Dies ist jedoch, wie es
aussiehtWie Sie sehen können, ist die Höhe des ersten Etiketts viel zu groß (oder die Höhe, die es sein sollte, + mein eigener Zeilenabstand, um genau zu sein). Wenn Sie der ersten attributierten Zeichenfolge einfach eine andere Farbe hinzufügen, erhöht sich die Größe sizeToFit
, indem Sie die darunter liegende lineSpacing
hinzufügen. Ich habe auch versucht, die boundingRectWithSize:
Methoden auf den Strings direkt und das gleiche Problem ist sichtbar. Dies ist also nicht spezifisch für den Etikettengrößencode, sondern ein Problem mit den Zeichenfolgen selbst. Ich sehe keinen möglichen Grund, warum das passieren sollte. Hat jemand einen Einblick?
hinzufügen Und wenn Sie keine benutzerdefinierten Zeilenabstand in der ersten Probe hinzufügen - was der Rahmen wäre? – sha
Es funktioniert ohne den benutzerdefinierten Zeilenabstand, oder zumindest sieht es so aus. Ich vermute, dass es immer noch versucht, es am Ende hinzuzufügen, aber da der Wert 0 ist, würde es keinen wirklichen Effekt haben und gut aussehen. – Dima
hast du das gelöst? FWIW UITextView hat dieses Problem überhaupt nicht – bogardon