2014-05-10 26 views
5

我敢肯定,这实际上是一个UIKit错误,但想获得一些输入,看看我是否在这里丢失了一些愚蠢的东西。UILabel大小不正确的单行文本lineSpacing和多种颜色

这里是我的代码:

// 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:方法,并且可以看到相同的问题。所以这不是特定于标签大小代码,而是字符串本身的问题。我没有看到任何可能的原因,为什么会发生这种情况。有没有人有任何见解?

+0

如果你没有第一样品中添加自定义行间距 - 会是什么框架? – sha

+0

它工作正常,没有自定义行间距,或者至少它看起来像它。我怀疑它仍然试图将其添加到底部,但由于值为0,所以它不会有实际效果并显示正常。 – Dima

+0

你解决了这个问题吗? FWIW UITextView根本没有这个问题 – bogardon

回答

0

在你的属性字典添加

[attrDic setObject:@0 forKey:NSBaselineOffsetAttributeName]; 
+0

我在几个不同的地方尝试了这一点,不幸的是它什么也没做。你能详细说明,我应该在哪里或如何设置这个属性,因为它与我的代码完全相关?你用上面提供的代码来试试它吗? – Dima