如何在Swift3中为我的标签加粗html文本

时间:2017-05-10 11:02:06

标签: swift swift3 uilabel nsattributedstring nsmutableattributedstring

我在一个视图的中心只有一个标签。我想大胆一些标签。我可以做到,但是当我这样做时,我会失去我的标签尺寸。 我发现了一些线程,但没有人可以帮我解决这个问题。

以下是我使用的代码:

override func viewDidAppear(_ animated: Bool) {
    let attrStr = try! NSAttributedString(
        data: "I'm a normal text and <b>this is my bold part . </b>And I'm again in the normal text".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil)
    myLabel.attributedText = attrStr
}

1 个答案:

答案 0 :(得分:0)

使用boundingRectWithSize:option:context

粗体化文本re-calculate the size之后的属性标签