简体   繁体   English

2 UIButton 动态文本和字体大小

[英]2 UIButton with dynamic text and font size

I am using 2 UIButton in same y position. The one on the left is shorter in text than the one on the right:我在同一个 y position 中使用 2 UIButton左边的文本比右边的短:

example image示例图像

I am adding我正在添加

button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.minimumScaleFactor = 0.5

for each button.对于每个按钮。 The problem is that one button font size get smaller than the other . The problem is that one button font size get smaller than the other For example, they are both with font size 18, one might get 12 and the other will stay 18. I want them both to be 15 (the larger possible keeping both texts visible)例如,它们的字体大小都是 18,一个可能是 12,另一个将保持 18。我希望它们都是 15(更大的可能使两个文本都可见)

The text inserted to the buttons changes so i can't use aspect ratio.插入按钮的文本发生变化,因此我无法使用纵横比。
Also, i don't want to use the font of the button with the smaller text because the difference is sometimes to high.另外,我不想将按钮的字体与较小的文本一起使用,因为有时差异很大。

You can do it programmatically with stack view, declare your buttons under your controller class:您可以使用堆栈视图以编程方式执行此操作,在您的 controller class 下声明您的按钮:

let myButton: UIButton = {
    let b = UIButton()
    b.backgroundColor = .white
    b.setTitle("Short button", for: .normal)
    b.setTitleColor(.black, for: .normal)
    b.titleLabel?.font = .systemFont(ofSize: 17, weight: .regular)
    b.layer.cornerRadius = 12
    b.clipsToBounds = true
    b.titleLabel?.adjustsFontSizeToFitWidth = true
    b.titleLabel?.minimumScaleFactor = 0.5
    b.translatesAutoresizingMaskIntoConstraints = false

    return b
}()

let myButton2: UIButton = {
    let b = UIButton()
    b.backgroundColor = .white
    b.setTitle("Much Much Much longer button", for: .normal)
    b.setTitleColor(.black, for: .normal)
    b.titleLabel?.font = .systemFont(ofSize: 17, weight: .regular)
    b.layer.cornerRadius = 12
    b.clipsToBounds = true
    b.titleLabel?.adjustsFontSizeToFitWidth = true
    b.titleLabel?.minimumScaleFactor = 0.5
    b.translatesAutoresizingMaskIntoConstraints = false

    return b
}()

Now in viewDidLoad set stackView and constraints (I set 6 for space, but you can set your prefer space, look at the comment to do it):现在在 viewDidLoad 中设置 stackView 和约束(我为空间设置了 6,但你可以设置你喜欢的空间,看评论来做):

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = .darkGray
    
    let stackView = UIStackView(arrangedSubviews: [myButton, myButton2])
    stackView.distribution = .fillProportionally
    stackView.spacing = 6 // change space betwenn buttons
    stackView.translatesAutoresizingMaskIntoConstraints = false
    
    view.addSubview(stackView)
    stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
    stackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
    stackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
    stackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
}

This is the result shortButton/shortButton and shortButton/longerButton, the width of the buttons changes dynamically according to the text:这是 shortButton/shortButton 和 shortButton/longerButton 的结果,按钮的宽度根据文本动态变化:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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