gpt4 book ai didi

ios - 让 Button 标题标签大小根据 swift 中的设备(iPad 或 iPhone)

转载 作者:行者123 更新时间:2023-11-30 11:44:29 25 4
gpt4 key购买 nike

我对按钮标题标签尺寸有疑问。
我想放大iPad中按钮标题标签的字体大小。
我尝试使用以下代码,但似乎无法使标签放大。
如果我想根据设备改变字体大小,我该怎么办?

以图像为例。
标签尺寸如下图。
image

此代码在 iPhone 中工作时大小合适,但在 iPad 中似乎很小。

func CustomButton() -> UIButton {
let ui = UIButton()
ui.translatesAutoresizingMaskIntoConstraints = false
ui.contentMode = UIViewContentMode.scaleAspectFit
return ui
}

class FullWidthButton: UIView {

var button:UIButton = { () -> UIButton in
let ui:UIButton = CustomButton()
ui.titleLabel?.numberOfLines = 1
ui.titleLabel?.textAlignment = .center
ui.titleLabel!.minimumScaleFactor = 0.5
ui.titleLabel?.textColor = UIColor.white
ui.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
ui.titleLabel?.adjustsFontSizeToFitWidth = true
ui.titleLabel?.lineBreakMode = .byClipping
ui.backgroundColor = defaultButtonOrangeColor
ui.layer.cornerRadius = defaultButtonRadius
ui.layer.masksToBounds = true
return ui
}()
override init(frame: CGRect) {
super.init(frame:frame)
translatesAutoresizingMaskIntoConstraints = false
loadContent()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func layoutSubviews() {
super.layoutSubviews()

loadVFL()
}

func loadContent() {

addSubview(button)
}

func loadVFL() {

button.snp.makeConstraints { (make) in
make.width.height.leading.trailing.top.bottom.equalToSuperview()
}
}
}

最佳答案

switch UIDevice.current.userInterfaceIdiom { 
case .phone:
case .pad:
case .unspecified:
}

关于ios - 让 Button 标题标签大小根据 swift 中的设备(iPad 或 iPhone),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48983090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com