1

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.

Yuyutsu
  • 2,509
  • 22
  • 38
yoshinbo
  • 741
  • 1
  • 6
  • 12
  • Please give the exact error message that occurs on the line `ohLabel.attributedText = text`. – Eric Aya Apr 28 '15 at 10:45
  • Error does not occur, background color is not adopted on text. I can see background colored text on only UILabel, others are not changed, 'This is Test', no background color. – yoshinbo Apr 28 '15 at 15:00
  • So `seTextView` and `ohLabel` are not of type `UILabel`? What are their type? – Eric Aya Apr 28 '15 at 15:01
  • SETextView is sub class of UITextView, and OHAttributedLabel is sub class of UILabel. Both classes are famous OSS. – yoshinbo Apr 29 '15 at 08:53
  • For attributed strings and UITextView, there's explanations [here](http://stackoverflow.com/a/19340206/2227743). And if you look at the [OHLabel repo](https://github.com/AliSoftware/OHAttributedLabel) they say it's deprecated for iOS > 6. – Eric Aya Apr 29 '15 at 09:00
  • I've already known that UILabel and UITextView can adopt NSBackgroundColorAttributeName. I want to set NSBackgroundColorAttributeName on TTTAttributedLabel, OHAttributedLabel or SETextView:( Do you know what I said? – yoshinbo Apr 29 '15 at 13:06
  • No need to be rude. Instead, read the explanation in the first link I gave you (since you said that SeTextView is a subclass of UITextView, it applies to your case and answers your questions). And read what *the author of the repo himself* says in the second link I gave you. Nobody can help you if you're using deprecated stuff. – Eric Aya Apr 29 '15 at 13:11
  • No No I read both of them you gave me, and try then not solve the problem.. I really don't blame you, if you said like this, I'm so sorry, my English is poor. I just wanted to confirm whether you understand my exact question or not.. – yoshinbo Apr 30 '15 at 00:49
  • Ok, my apologies. But I was serious about the deprecated third party library, you shouldn't use it, it will be the source of many problems for sure. – Eric Aya Apr 30 '15 at 05:50
  • 1
    Thanks ericd:D and finally solve the problem by modified the code of SETextView library for making it able to set NSBackgroundColorAttributeName. I'm going to pull request for the library. – yoshinbo May 01 '15 at 00:29

2 Answers2

0

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


}
Yuyutsu
  • 2,509
  • 22
  • 38
  • Thanks for your answer but It's not my point:( I can off cause set "NSBackgroundColorAttributeName" to UILabel, however can not set it to TTTAttributedLabel, OHAttributedLabel and SETextView. – yoshinbo Apr 28 '15 at 10:35
0

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

Jonathan Hersh
  • 1,073
  • 1
  • 8
  • 15