更改UILabel文本,但保留其余的属性

我有一个UILabel在我的ViewController创build使用故事板。 标签文本的字体,大小和颜色,alignment方式 – 全部在故事板中设置。 故事板中的标签连接到名为p_patientNameLbl的文件中的sockets。

现在我正在尝试以编程方式更改标签的文本,如下所示:

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

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

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

我可以看到新的标签,但其余的字体属性和alignment仍然是错误的。

有没有办法改变标签的文本,而没有以编程方式设置所有其他的属性? 我会想象一定有办法,因为我不想在代码中格式化我的接口!

发生这种情况的原因是,您要让“界面”构build器采用在其文本上设置了预定义属性的标签,并用纯文本replace此属性文本。 除了setTextsetTextColor还必须使用setAttributedText并指定要传递给属性string的属性。

以下是如何以编程方式将属性string应用于标签的示例。 我不知道有什么更好的方法,但是使用这个,你可以对文本或文本颜色进行更改,只要你设置了其他属性,所有的改变都将被正确应用。

 [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; } 

这适用于我:

 - (void)setText:(NSString *)text withExistingAttributesInLabel:(UILabel *)label { // Check label has existing text if ([label.attributedText length]) { // Extract attributes NSDictionary *attributes = [(NSAttributedString *)label.attributedText attributesAtIndex:0 effectiveRange:NULL]; // Set new text with extracted attributes label.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes]; } } ... [self setText:@"Some text" withExistingAttributesInLabel:self.aLabel]; 

Swift中的一行实现:

 label.attributedText = NSAttributedString(string: "text", attributes: label.attributedText!.attributesAtIndex(0, effectiveRange: nil)) 

0x7fffffff,感谢您的提示,为我工作。 在iOS6中的一些其他build议没有工作,例如。 提取属性并重新应用它们。

在我的情况下,我失去了不less有用的属性,包括字体,所以这是我的解决scheme:

 - (NSMutableAttributedString *)SetLabelAttributes:(NSString *)input col:(UIColor *)col size:(Size)size { NSMutableAttributedString *labelAttributes = [[NSMutableAttributedString alloc] initWithString:input]; UIFont *font=[UIFont fontWithName:@"Helvetica Neue" size:size]; NSMutableParagraphStyle* style = [NSMutableParagraphStyle new]; style.alignment = NSTextAlignmentCenter; [labelAttributes addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, labelAttributes.length)]; [labelAttributes addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, labelAttributes.length)]; [labelAttributes addAttribute:NSForegroundColorAttributeName value:col range:NSMakeRange(0, labelAttributes.length)]; return labelAttributes; } 

然后像下面这样将它应用到UILabel:

 [self.myLabel setAttributedText:[self CurrentSetLabelAttributes:[NSString stringWithFormat:@"%d", myClass.text] col:[UIColor redColor] size:50.0f]]; 

我为多个应用程序做了这么多次。 我总是在Storyboard中制作标签和文本,在编辑器中更改它们的字体大小,颜色显示,Alpha值等。 但事后如果我需要更改文字,我已经多次做了,我所做的只是这个。

 p_patientNameLbl.textColor = [UIColor redColor]; p_patientNameLbl.text = @"My Sample Text"; 

而已。 所有其他的默认属性保持不变。 除非我错过了你的问题?

而不是存储旧的属性,并创build一个新的NSAttributedString与改变的文本和存储的属性,相对较简单的方法将是:

 NSMutableAttributedString *attStr = [label.attributedText mutableCopy]; NSMutableString *str = attStr.mutableString; /* change str with new text */ label.attributedText = attStr;