How to set “NSBackgroundColorAttributeName” on TTTAttributedLabel, OHAttributedLabel or SETextView in Swift

心不动则不痛 提交于 2019-12-23 03:07:25

问题


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
        })
    }
}

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


回答1:


Read this blog NSBackgroundcolorattributename and Apple Documentation of NSAttributedString_Class

Use this code..

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.
    }


}



回答2:


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



来源:https://stackoverflow.com/questions/29915988/how-to-set-nsbackgroundcolorattributename-on-tttattributedlabel-ohattributedl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!