我的 View Controller 一加载,我就会看到一个显示文本“Sto 1”的按钮(灰色背景,白色字体)。这在 viewWillLayoutSubviews 中调用,标题使用 NSMutableAttributedString 设置。 “Sto”是商店的缩写。
对于我的应用程序,我希望用户能够选择 Sto 1 按钮并能够存储显示在 UILabel 上的数字。我能够获取当前显示的数字,但无法使用 NSMutableAttributedString 更新我的 Sto 1 按钮内的文本。换句话说,我想从显示“Sto 1”的按钮转到显示某个数字(例如 12)。
感谢大家对我的帮助。我对 Swift 还是比较陌生,过去一周我一直在努力解决这个问题。
import UIKit
class ViewController: UIViewController {
var fontConstant = CGFloat()
var someRandomNumberDisplayedOnAUILabel = String(12)
@IBOutlet weak var myButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillLayoutSubviews() {
fontConstant = 1
let myString = "Sto 1"
let myAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
let mutableAttributedString = NSMutableAttributedString(string: myString, attributes: myAttributes)
myButton.setAttributedTitle(mutableAttributedString, for: .normal)
myButton.backgroundColor = UIColor(red: 94/255.0, green: 94/255.0, blue: 94/255.0, alpha: 1.0)
}
@IBAction func myButtonAction(_ sender: Any) {
let myAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)
myButton.setAttributedTitle(mutableAttributedString, for: .normal)
myButton.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)
}}
最佳答案
通常,您会使用 setTitle(:for:) 更改 UIButton
上的文本。但是由于您正在使用 NSMutableAttributedString
,因此您将需要 setAttributedTitle( :for:)功能。我想这可能是您要找的:
myButton.setAttributedTitle(myNSMutableAttributedString, for: .normal)
不过,请注意。您可能需要为不同的控件状态调用此函数,而不仅仅是 .normal
,否则您可能会在突出显示按钮时立即看到不同的文本。 Here是控制状态的列表。
编辑:
我会尝试在 IBAction
中引用发件人,而不是 myButton
。这可能是一个快速修复:
@IBAction func myButtonAction(_ sender: Any) {
let myAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)
guard let button = sender as? UIButton else {
print("Error: sender was not a button")
return
}
button.setAttributedTitle(mutableAttributedString, for: .normal)
button.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)
}
编辑 #2:
如果您丢失了对 IBOutlet
的引用,您可以通过为按钮分配一个选择器来解决这个问题在你失去它之前。试试这个:
override func viewDidLoad() {
super.viewDidLoad()
// Add the action to the button rather than holding on to the IBOutlet
myButton.addTarget(self, action: #selector(RideInProgressViewController.myAction(sender:)), for: .touchUpInside)
}
@objc private func myAction(sender: Any) {
guard let button = sender as? UIButton else {
print("Error: sender was not a button")
return
}
let myAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10 * fontConstant)]
let mutableAttributedString = NSMutableAttributedString(string: someRandomNumberDisplayedOnAUILabel, attributes: myAttributes)
button.setAttributedTitle(mutableAttributedString, for: .normal)
button.backgroundColor = UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0)
}
关于swift - 在实例化 View Controller 中使用 NSMutableAttributedString 引用 UIButton 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53712259/