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

时间:2014-05-10 00:34:16

标签: ios cocoa-touch uikit uilabel nsattributedstring

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

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

1 个答案:

答案 0 :(得分:0)

在属性词典中添加

 [attrDic setObject:@0 forKey:NSBaselineOffsetAttributeName];
相关问题