3

当我将 NSForegroundColorAttributeName 与包含表情符号的字符串一起使用时,该字符串会垂直移位。

这是一个例子:

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 21)];
label1.text = @"@Hashtag";
//label1.backgroundColor = [UIColor redColor];
label1.numberOfLines = 0;
label1.shadowColor = [UIColor colorWithWhite:1 alpha:0.5];
label1.shadowOffset = CGSizeMake(1,1);
[label1 sizeToFit];
[self.view addSubview:label1];

NSString *comment = @"@Hashtag ";

NSMutableAttributedString *commentAttributedString = [[NSMutableAttributedString alloc]  initWithString:comment];
[commentAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor  darkGrayColor] range:NSMakeRange(0, comment.length)];
[commentAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 8)];

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 21)];
label2.attributedText = commentAttributedString;
label2.numberOfLines = 0;
label2.shadowColor = [UIColor colorWithWhite:1 alpha:0.5];
label2.shadowOffset = CGSizeMake(1,1);
[label2 sizeToFit];
[self.view addSubview:label2];

我得到以下结果:

绿色标签被替换。这仅在 iOS 7 上发生,在 iOS 6 上一切正常。

有人知道为什么会这样吗?

亲切的问候

阿诺

4

0 回答 0