如何设置"NSBackgroundColorAttributeName";在Swift中的TTTAttributedLabel,OHAttributedLabel或SETextView上 [英] How to set "NSBackgroundColorAttributeName" on TTTAttributedLabel, OHAttributedLabel or SETextView in Swift

查看:220
本文介绍了如何设置"NSBackgroundColorAttributeName";在Swift中的TTTAttributedLabel,OHAttributedLabel或SETextView上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在TTTAttributedLabelOHAttributedLabelSETextView上设置NSBackgroundColorAttributeName属性,但是它不起作用.有人知道吗遵循示例代码.

I'm trying to set NSBackgroundColorAttributeName attribute on TTTAttributedLabel, OHAttributedLabel or SETextView, but it's not work. Anyone have any idea? Sample code is followed.

class ViewController: UIViewController {

    @IBOutlet weak var textLabel: UILabel!
    @IBOutlet weak var ttLabel: TTTAttributedLabel!
    @IBOutlet weak var ohLabel: OHAttributedLabel!
    @IBOutlet weak var seTextView: SETextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var content: String = "This is Test."
        var text = NSMutableAttributedString(string: content)
        text.addAttribute(NSBackgroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, text.length))
        textLabel.attributedText = text     // It's Work.
        ohLabel.attributedText = text       // It's not Work
        seTextView.attributedText = text    // It's not Work

        // It's not Work as well
        ttLabel.setText(content, afterInheritingLabelAttributesAndConfiguringWithBlock: {
            (attributedString) -> NSMutableAttributedString! in
            attributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attributedString.length))
            //attributedString.addAttribute(kTTTBackgroundFillColorAttributeName, value: UIColor.yellowColor(), range: NSMakeRange(1, 3))
            return attributedString
        })
    }
}

此外,我可以在每个label上正确设置"NSForegroundColorAttributeName".

In addition, I can set "NSForegroundColorAttributeName" on every label properly.

推荐答案

对于TTTAttributedLabel,您应该使用

For TTTAttributedLabel, you should use the custom attributes that are specific to the label, like kTTTBackgroundFillColorAttributeName.

这篇关于如何设置"NSBackgroundColorAttributeName";在Swift中的TTTAttributedLabel,OHAttributedLabel或SETextView上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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