我有一个按钮
buttonSign
创建并测试了突出显示的颜色变化。 我还需要创建-0.5个字母间距:
let attributedString = NSMutableAttributedString(string: (buttonSign.titleLabel?.text!)!)
attributedString.addAttribute(NSAttributedString.Key.kern, value: -0.5, range: NSRange(location: 0, length: (buttonSign.titleLabel?.text!.count)!))
buttonSign.setAttributedTitle(attributedString, for: .normal)
按钮代码:
let buttonSign = UIButton(frame: CGRect(x: UIScreen.main.bounds.width/2 - 170, y: UIScreen.main.bounds.height-41-56-16-56, width: 340, height: 56))
buttonSign.setTitleColor(UIColor.white, for: UIControl.State.normal)
buttonSign.setTitleColor(UIColor.blue, for: UIControl.State.highlighted)
buttonSign.backgroundColor = UIColor(red: 95.0 / 255.0, green: 84.0 / 255.0, blue: 216.0 / 255.0, alpha: 1.0)
buttonSign.addTarget(self, action: #selector(signUpAction), for: .touchUpInside)
buttonSign.layer.cornerRadius = buttonSign.bounds.size.height / 2
buttonSign.clipsToBounds = true
buttonSign.setTitle("SIGN UP",for: .normal)
buttonSign.titleLabel?.font = UIFont(name: "SFProDisplay-Bold", size: 20)
buttonSign.titleLabel?.textColor = UIColor(red: 111.0 / 255.0, green: 107.0 / 255.0, blue: 244.0 / 255.0, alpha: 1.0)
view.addSubview(buttonSign)
这可行,但是当我申请
attributedString
突出显示时,它不会更改文本的颜色。 那怎么可能?