如何保留将接收HTML文本的Label属性

时间:2019-04-03 09:08:30

标签: ios

在Android中,当我将文本设置如下时

my_text.text = Html.fromHtml("<B>From</B> Someone", FROM_HTML_MODE_COMPACT)

我会得到

enter image description here

当我如下更改TextView中的属性时

        android:textColor="#F40"
        android:textSize="24sp"
        android:fontFamily="serif-monospace"

我会得到

enter image description here

这很酷,因为我可以从XML控制我的属性,而文本可以来自某些端点服务(以控制文本是否加粗)。

我可以使用以下代码在iOS上执行类似的任务

let htmlString = "<B>From</B> someone"
let data = htmlString.data(using: String.Encoding.unicode)! 
let attrStr = try? NSAttributedString( 
  data: data,
  options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
  documentAttributes: nil)
myLabel.attributedText = attrStr

总是这样

enter image description here

它不再支持从界面构建器设置的属性。有什么方法可以让Label保持界面构建器设置的属性(字体类型,大小,颜色),而设置给它的文本将决定使用<b></b>标签加粗还是不加粗?

0 个答案:

没有答案