对于lineSpacing和多种颜色的单行文本,UILabel大小不正确
我很确定这实际上是一个UIKit的错误,但想得到一些input,看看我是否在这里丢失了一些愚蠢的东西。
这里是我有的代码:
// 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];
以及一个简单的方便function来改变一个属性string的lineSpacing
:
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; }
但是,这就是它的样子
正如你所看到的,第一个标签的高度太大了(或者它的高度应该是+我自定义的行间距,准确的说)。 当简单地向第一个属性string添加另一种颜色时,会通过在lineSpacing
下面添加lineSpacing
来增加sizeToFit
大小。 我也尝试直接使用string上的boundingRectWithSize:
方法,同样的问题是可见的。 所以这不是特定于标签大小代码,而是string本身的问题。 我没有看到任何可能的原因,为什么会发生这种情况。 有没有人有任何见解?
在你的属性字典中添加
[attrDic setObject:@0 forKey:NSBaselineOffsetAttributeName];