【问题标题】:iOS put HTML text in UILabeliOS 将 HTML 文本放入 UILabel
【发布时间】:2015-08-03 15:57:49
【问题描述】:

我需要将带有粗体、斜体和链接的 HTML 文本放在 UILabel 中。 如果我使用NSAttributedString,则链接不可点击。如果我使用TTTAttributedLabel 链接是可点击的,但格式(粗体和斜体)不可见。

这个问题把我逼疯了!想法?

【问题讨论】:

  • 您可以检查RTLabel是否有帮助?
  • 你能用 UITextView 代替 UILable 吗?我不确定,但它可能会有所帮助。

标签: ios uilabel nsattributedstring tttattributedlabel


【解决方案1】:

您可以使用 TTTAttributedLabel 使链接格式正常工作。例如,这会将链接着色为蓝色并在它们下划线(在 Swift 中):

let bodyLabel: TTTAttributedLabel

let content = "TTTAttributedLabel will detect http://example.com"
let attributedContent = NSMutableAttributedString(string: content)

// add some attributes as you need to the attributed string, then...

let blueLinks = [NSForegroundColorAttributeName: UIColor.blueColor()]

bodyLabel.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue
bodyLabel.linkAttributes = blueLinks

bodyLabel.setText(attributedContent)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2011-08-10
    • 2014-07-06
    相关资源
    最近更新 更多