ios 为什么点击后按钮会发生变化?

lymgl2op  于 2023-05-30  发布在  iOS
关注(0)|答案(3)|浏览(176)

我想将自定义字体设置为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) {
       
    }
}

c2e8gylq

c2e8gylq1#

在iOS 15中,您需要使用按钮的配置。这应该可以工作:

loginButton.configuration?.attributedTitle?.font = UIFont(name: "Capitana-Bold", size: CGFloat(16))
bqf10yzr

bqf10yzr2#

这是一个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

qco9c6ql

qco9c6ql3#

将按钮样式设置为default和signInButton.titleLabel?.font = .switzerSemibold(size: CGFloat(K.styles.buttonFontSize))

extension UIFont {
    static func switzerSemibold(size: CGFloat) -> UIFont? {
        return UIFont(name: "Switzer-Semibold", size: size)
    }
    
    static func switzerRegular(size: CGFloat) -> UIFont? {
        return UIFont(name: "Switzer-Regular", size: CGFloat(K.styles.descriptionFontSize))
    }
    
    static func switzerMedium(size: CGFloat) -> UIFont? {
        return UIFont(name: "Switzer-Medium", size: CGFloat(K.styles.buttonFontSize))
    }
}

做的把戏

相关问题