【问题标题】:Why button changes after the click?为什么点击后按钮会发生变化?
【发布时间】:2021-12-16 04:02:54
【问题描述】:

我想将自定义字体设置为UIButton。我需要以编程方式设置它,因为字体不会以其他方式应用。问题是单击后它会变回内置字体。我做错了什么?

import UIKit

class LoginViewController: UIViewController {
    
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var eyeButton: UIButton!
    @IBOutlet weak var loginButton: UIButton!
    
    var iconClick = true
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loginButton.titleLabel?.font = UIFont(name: "Capitana-Bold", size: CGFloat(16))
    }
    
    @IBAction func iconAction(sender: AnyObject) {
        if (iconClick == true) {
            passwordTextField.isSecureTextEntry = false
            eyeButton.setImage(UIImage(named: "eye-closed"), for: .normal)
        } else {
            passwordTextField.isSecureTextEntry = true
            eyeButton.setImage(UIImage(named:  "eye-open"), for: .normal)
        }
        iconClick = !iconClick
    }
    
    
    @IBAction func onLoginClicked(_ sender: Any) {
       
    }
}

【问题讨论】:

    标签: ios fonts uibutton


    【解决方案1】:

    这是一个 iOS 15 填充按钮。你不能说配置它

    loginButton.titleLabel?.font = ...
    

    这在 iOS 14 及之前是可能的(但错误),但使用 iOS 15 按钮则不可能。要以编程方式配置填充按钮,您必须设置其配置对象。

    https://developer.apple.com/documentation/uikit/uibutton/configuration

    特别是您要设置按钮配置属性标题。

    https://developer.apple.com/documentation/uikit/uibutton/configuration/3750779-attributedtitle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多