简体   繁体   中英

iOS Swift 5 Material Components button text shrink after button tap

I have a material button. When I tap on it, the text label in the button shrinks and stays small, never reverting to the original size after it is released. The cocoa pods I am using is pod 'MaterialComponents/Buttons', '~> 92.4.0'

Before Tap

点击前

After Tap

点击后

import UIKit
import MaterialComponents.MaterialButtons

class TestViewController: UIViewController {

private let testButton: MDCButton = {
    let button = MDCButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Test", for: UIControl.State.normal)
    button.titleLabel?.font = UIFont(name: button.titleLabel!.font.fontName, size: 22)
    button.titleLabel?.adjustsFontSizeToFitWidth = true
    button.titleLabel?.numberOfLines = 1
    button.titleLabel?.minimumScaleFactor = 0.5
    button.titleLabel?.lineBreakMode = NSLineBreakMode.byClipping;
    button.setTitleColor(.white, for: .normal)
    button.backgroundColor = UIColor.blue
    button.layer.cornerRadius = 10
    button.layer.shadowColor = UIColor.gray.cgColor
    button.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
    button.layer.shadowOpacity = 0.3
    button.layer.shadowRadius = 1.0
    button.layer.masksToBounds = false
    return button
}()

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
    self.view.backgroundColor = UIColor.white
    self.navigationItem.title = "The Page"
    self.view.addSubview(testButton)
    var testConstraintsArray: [NSLayoutConstraint] = []
    testConstraintsArray.append(testButton.centerXAnchor.constraint(equalTo: 
    self.view.centerXAnchor))
    testConstraintsArray.append(testButton.centerYAnchor.constraint(equalTo: 
    self.view.centerYAnchor))
    NSLayoutConstraint.activate(testConstraintsArray)
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

}

One of our developers found the solution. Pretty simple fix but since we couldn't find an answer to it anywhere I thought I'd share.

button.titleLabel?.font =  UIFont(name: "Roboto-Medium", size: 20)

should have been....

button.setTitleFont(UIFont(name: "Roboto-Medium", size: 20), for: .normal)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM