gpt4 book ai didi

swift - 如何在 Swift 中的 TTTAttributedLabel、OHAttributedLabel 或 SETextView 上设置 "NSBackgroundColorAttributeName"

转载 作者:行者123 更新时间:2023-11-28 09:09:04 27 4
gpt4 key购买 nike

我试图在 TTTAtributedLabelOHAttributedLabelSETextView 上设置 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”。

最佳答案

阅读此博客 NSBackgroundcolorattributenameNSAttributedString_ClassApple 文档

使用此代码..

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


}

关于swift - 如何在 Swift 中的 TTTAttributedLabel、OHAttributedLabel 或 SETextView 上设置 "NSBackgroundColorAttributeName",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29915988/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com