Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置;NSBackgroundColorAttributeName“;在Swift中的TTTAttributedLabel、OHAttributedLabel或SETextView上_Swift_Xcode6_Tttattributedlabel_Ohattributedlabel - Fatal编程技术网

如何设置;NSBackgroundColorAttributeName“;在Swift中的TTTAttributedLabel、OHAttributedLabel或SETextView上

如何设置;NSBackgroundColorAttributeName“;在Swift中的TTTAttributedLabel、OHAttributedLabel或SETextView上,swift,xcode6,tttattributedlabel,ohattributedlabel,Swift,Xcode6,Tttattributedlabel,Ohattributedlabel,我正在尝试在TTATAttributedLabel、OHAttributedLabel或SETextView上设置NSBackgroundColorAttributeName属性,但它不起作用。有人知道吗?下面是示例代码 class ViewController: UIViewController { @IBOutlet weak var textLabel: UILabel! @IBOutlet weak var ttLabel: TTTAttributedLabel!

我正在尝试在
TTATAttributedLabel
OHAttributedLabel
SETextView
上设置
NSBackgroundColorAttributeName
属性,但它不起作用。有人知道吗?下面是示例代码

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
        })
    }
}
此外,我可以在每个
标签上正确设置“
NSForegroundColorAttributeName
”。

阅读此博客 以及

使用此代码

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var myLabel: UILabel!
    var myString:NSString = "I Love Stackoverflow!"
    var myMutableString = NSMutableAttributedString()
    @IBAction func myFontButton(sender: UIButton) {
        myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: sender.titleLabel!.text!, size: 24.0)!, range: NSRange(location: 7,length: 5))
        myLabel.attributedText = myMutableString
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //Initialize the mutable string
        myMutableString = NSMutableAttributedString(string: myString, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])

        //Add more attributes here:
        myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Chalkduster", size: 24.0)!, range: NSRange(location: 7,length: 5))
        myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "AmericanTypewriter-Bold", size: 18.0)!, range: NSRange(location:2,length:4))
        myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))

        myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Georgia", size: 36.0)!, range: NSRange(location: 0, length: 1))
        myMutableString.addAttribute(NSStrokeColorAttributeName, value: UIColor.blueColor(), range:  NSRange(location: 0, length: 1))
        myMutableString.addAttribute(NSStrokeWidthAttributeName, value: 2, range: NSRange(location: 0, length: 1))

        myMutableString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location: 0, length: myString.length))
        myLabel.backgroundColor = UIColor.grayColor()

        //Apply to the label
        myLabel.attributedText = myMutableString    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

对于
TTTAttributedLabel
,您应该使用特定于标签的,如
kTTTBackgroundFillColorAttributeName

谢谢您的回答,但这不是我的重点:(我可以设置“NSBackgroundColorAttributeName”对于UILabel,但无法将其设置为TTtatAttributedLabel、OHAttributedLabel和SETextView。请给出行
OHalabel.attributedText=text
上出现的确切错误消息。不会发生错误,文本上不采用背景色。我只能在UILabel上看到背景色文本,其他文本不会更改,“这是测试”,没有背景色。因此
seTextView
ohLabel
不是类型
UILabel
?它们的类型是什么?seTextView是uiextview的子类,而ohattributedblabel是UILabel的子类。这两个类都是著名的操作系统。感谢ericd:D,最后通过修改seTextView库的代码使其成为ab解决了这个问题le设置NSBackgroundColorAttributeName。我将拉取对库的请求。谢谢Jonathan,我稍后将尝试:D