更改属性UILabel的文本而不会丢失格式? [英] Change the text of an attributed UILabel without losing the formatting?

查看:95
本文介绍了更改属性UILabel的文本而不会丢失格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在故事板中,我布置了一组带有各种格式选项的标签。

In the storyboard I layout a set of labels with various formatting options.

然后我这样做:

label.text = @"Set programmatically";

所有格式都丢失了!这在iOS5中运行良好。

And all formatting is lost! This works fine in iOS5.

必须有一种方法只更新文本字符串而不重新编码所有格式?!

There must be a way of just updating the text string without recoding all the formatting?!

label.attributedText.string 

是只读的。

提前致谢。

推荐答案

一个attributionString包含所有它的格式化数据。标签根本不知道格式。

An attributedString contains all of its formatting data. The label doesn't know anything about the formats at all.

您可以将属性存储为单独的字典,然后在更改attributesString时可以使用:

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.

这篇关于更改属性UILabel的文本而不会丢失格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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