【问题标题】:How to set "NSBackgroundColorAttributeName" on TTTAttributedLabel, OHAttributedLabel or SETextView in Swift如何在 Swift 中的 TTTAttributedLabel、OHAttributedLabel 或 SETextView 上设置“NSBackgroundColorAttributeName”
【发布时间】:2015-04-28 09:49:38
【问题描述】:

我正在尝试在TTTAttributedLabelOHAttributedLabelSETextView 上设置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
        })
    }
}

另外,我可以在每个label上正确设置“NSForegroundColorAttributeName”。

【问题讨论】:

  • 请给出出现在ohLabel.attributedText = text这一行的确切错误信息。
  • 没有出现错误,文本没有采用背景色。我只能在 UILabel 上看到背景颜色的文字,其他没有改变,“这是测试”,没有背景颜色。
  • 所以seTextViewohLabel 不是UILabel 类型?他们是什么类型的?
  • SETextView 是 UITextView 的子类,OHAttributedLabel 是 UILabel 的子类。这两个类都是著名的 OSS。
  • 感谢 ericd:D 最后通过修改 SETextView 库的代码使其能够设置 NSBackgroundColorAttributeName 来解决问题。我要拉取图书馆的请求。

标签: swift xcode6 tttattributedlabel ohattributedlabel


【解决方案1】:

阅读此博客 NSAttributedString_Class 中的NSBackgroundcolorattributenameApple Documentation

使用此代码..

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


}

【讨论】:

  • 感谢您的回答,但这不是我的意思:(我可以将“NSBackgroundColorAttributeName”设置为 UILabel,但不能将其设置为 TTTAttributedLabel、OHAttributedLabel 和 SETextView。
【解决方案2】:

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

【讨论】:

  • 谢谢乔纳森,我稍后试试:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多