gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:57:50 27 4
gpt4 key购买 nike

我很确定这实际上是一个 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;
}

然而,这就是它的样子

image

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

最佳答案

在你的属性字典中添加

 [attrDic setObject:@0 forKey:NSBaselineOffsetAttributeName];

关于ios - 对于具有 lineSpacing 和多种颜色的单行文本,UILabel 大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23575958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com