Jestem prawie pewny, że to jest błąd UIKit, ale chcę uzyskać trochę informacji, aby zobaczyć, czy brakuje tu czegoś głupiego.Niepoprawny rozmiar UILabel dla pojedynczego wiersza tekstu z lineSpacing i wieloma kolorami
Oto kod mam:
// 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];
wraz z prostą funkcją wygody zmienić lineSpacing
danego nadana wyrażenie:
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;
}
Jednak jest to na co wygląda
Jak widać, wysokość pierwszej etykiety jest zbyt duża (lub wysokość, która powinna być + mój niestandardowy odstęp między wierszami, aby być precyzyjnym). Po prostu dodanie innego koloru do pierwszego przypisanego ciągu powoduje zwiększenie rozmiaru o sizeToFit
, dodając poniżej lineSpacing
. Próbowałem też bezpośrednio używać metod na ciągach i ten sam problem jest widoczny. To nie jest specyficzne dla kodu rozmiaru etykiety, ale jest problemem z samymi ciągami znaków. Nie widzę żadnego możliwego powodu, aby tak się stało. Czy ktoś ma jakiś wgląd?
A jeśli nie dodawać niestandardowe odstępy między wierszami w pierwszej próbce - jaka byłaby rama? – sha
Działa dobrze bez niestandardowego odstępu między wierszami lub przynajmniej wygląda tak. Podejrzewam, że nadal próbuje dodać go na dole, ale ponieważ wartość wynosi 0, nie przyniosłoby to rzeczywistego efektu i wyglądałoby dobrze. – Dima
rozwiązałeś to? FWIW UITextView nie ma w ogóle tego problemu – bogardon