对于具有lineSpacing和多种颜色的单行文本,UILabel大小不正确

 手机用户2602897765 发布于 2023-01-15 18:57

我很确定这实际上是一个UIKit错误,但想得到一些输入,看看我是否在这里遗漏了一些愚蠢的东西.

这是我的代码:

// 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];

以及一个简单的便利功能来更改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;
}

但是,这就是它的样子

图片

如您所见,第一个标签的高度太大(或者它的高度应该是+我的自定义行距,确切地说).当简单地向第一个属性字符串添加另一种颜色时,它会sizeToFit通过添加lineSpacing下面的颜色来增加大小.我也尝试boundingRectWithSize:直接使用字符串上的方法,同样的问题是可见的.因此,这不是特定于标签大小调整代码,而是字符串本身的问题.我没有看到为什么会发生这种情况的任何可能原因.有没有人有任何见解?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有