更改UILabel文本但保留其余属性 [英] Changing UILabel text but keeping the rest of the attributes

查看:63
本文介绍了更改UILabel文本但保留其余属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用故事板创建的ViewController中有一个UILabel。标签文本的字体,大小和颜色,它的对齐方式 - 都在故事板中设置。故事板中的标签连接到我的文件中名为 p_patientNameLbl 的插座。

I have a UILabel in my ViewController created using storyboards. The font, size and color of the label's text, its alignment - all set in the storyboard. The label in the storyboard is connected to the outlet in my file called p_patientNameLbl.

现在我正在尝试以编程方式更改标签的文本,像这样:

Now I am trying to change the text of the label programatically, like this:

[self.p_patientNameLbl setText:@"Vasya"];

我看不到新文本,直到我意识到故事板中的原始标签是白色的黑色背景,但显然在我更改了上面的标签文字后,所有字体属性都已重置,现在它是黑​​色背景上的黑色文字,因此没有看到。一旦我以编程方式设置颜色:

I could not see the new text, until I realized that the original label in the storyboard was white on black background, but apparently after I changed the label's text as above, all the font attributes have been reset and now it was a black text on black background, and therefore not seen. Once I set the color programmatically:

[self.p_patientNameLbl setTextColor:[UIColor whiteColor]];

我可以看到新标签,但所有其他字体属性和对齐仍然是错误的。

I could see the new label, but all the rest of the font attributes and alignment were still wrong.

有没有办法只更改 标签文本,而无需以编程方式设置所有其余属性?我想必须有办法,因为我不想在代码中格式化我的界面!

Is there a way to change only the text of the label without having then programmatically to set all the rest of the attributes? I would imagine there must be a way, since I don't want to be formatting my interface in the code!

推荐答案

这个之所以发生,是因为您告诉界面构建器采用在其文本上设置了预定义属性的标签,并使用纯文本替换此属性文本。而不是 setText ,或 setTextColor ,你必须使用 setAttributedText 并指定要传递给属性字符串的属性。

This is happening because you're telling Interface builder to take a label that had pre-defined attributes set on its text and replace this attributed text with plain text. Instead of setText, or setTextColor you have to use setAttributedText and specify the attributes that you wish to pass to the attributed string.

以下是如何以编程方式将属性字符串应用于标签的示例。我不知道有什么更好的方法,但使用它,您可以更改文本或文本颜色,只要您设置其他属性,所有更改都将正确应用。

Here's an example of how to apply an attributed string to your label programmatically. I'm not aware of any better way, but using this, you can make changes to the text or text color and as long as you set your other attributes along with them, all changes will be applied correctly.

[myLabel setAttributedText:[self myLabelAttributes:@"Some awesome text!"]];

....................

- (NSMutableAttributedString *)myLabelAttributes:(NSString *)input
{
    NSMutableAttributedString *labelAttributes = [[NSMutableAttributedString alloc] initWithString:input];

    [labelAttributes addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-5.0] range:NSMakeRange(0, labelAttributes.length)];
    [labelAttributes addAttribute:NSStrokeColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, labelAttributes.length)];
    [labelAttributes addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, labelAttributes.length)];

    return labelAttributes;
}

这篇关于更改UILabel文本但保留其余属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆