xcode 在不丢失格式的情况下更改属性 UILabel 的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12706188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the text of an attributed UILabel without losing the formatting?
提问by lewis
In the storyboard I layout a set of labels with various formatting options.
在故事板中,我布置了一组带有各种格式选项的标签。
Then I do:
然后我做:
label.text = @"Set programmatically";
And all formatting is lost! This works fine in iOS5.
并且所有格式都丢失了!这在 iOS5 中运行良好。
There must be a way of just updating the text string without recoding all the formatting?!
必须有一种方法可以在不重新编码所有格式的情况下更新文本字符串?!
label.attributedText.string
is read only.
是只读的。
Thanks in advance.
提前致谢。
采纳答案by Fogmeister
An attributedString contains all of its formatting data. The label doesn't know anything about the formats at all.
属性字符串包含其所有格式数据。标签根本不了解格式。
You could possibly store the attributes as a separate dictionary and then when you change the attributedString you can use:
您可以将属性存储为单独的字典,然后在更改属性字符串时可以使用:
[[NSAttributedString alloc] initWithString:@"" attributes:attributes range:range];
The only other option is to build the attributes back up again.
唯一的其他选择是重新构建属性。
回答by josef
You can extract the attributes as a dictionary with:
您可以使用以下命令将属性提取为字典:
NSDictionary *attributes = [(NSAttributedString *)label.attributedText attributesAtIndex:0 effectiveRange:NULL];
Then add them back with the new text:
然后将它们添加回新文本:
label.attributedText = [[NSAttributedString alloc] initWithString:@"Some text" attributes:attributes];
This assumes the label has text in it, otherwise you'll crash so you should probably perform a check on that first with:
这假设标签中有文本,否则你会崩溃,所以你应该先检查一下:
if ([self.label.attributedText length]) {...}
回答by Kostas Williams
Although new to iOS programming, I encountered the same problem very quickly. In iOS, my experience is that
虽然是 iOS 编程新手,但我很快就遇到了同样的问题。在 iOS 中,我的经验是
- Lewis42's problem occurs consistently
- josef's suggestion of extracting and reapplying the attributes does not work: a null attributes dictionary is returned.
- Lewis42 的问题一直发生
- josef 的提取和重新应用属性的建议不起作用:返回空属性字典。
Having looked around s/o, I came across This Postand followed that recommendation, I ended up using this:
环顾四周后,我遇到了这篇文章并遵循了该建议,我最终使用了这个:
- (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;

