Ios 调整UI中的文本大小堆栈视图中的按钮
我正在根据Ios 调整UI中的文本大小堆栈视图中的按钮,ios,swift,uikit,Ios,Swift,Uikit,我正在根据viewDidLoad上的设备宽度调整4个相同类型按钮中的文本大小。它们位于水平堆栈视图中。有什么方法可以更优雅地做到这一点 // resize main buttons let screenSize = UIScreen.main.bounds let screenWidth = screenSize.width if screenWidth <= 320 { mainLockButton.titleLa
viewDidLoad
上的设备宽度调整4个相同类型按钮中的文本大小。它们位于水平堆栈视图中。有什么方法可以更优雅地做到这一点
// resize main buttons
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
if screenWidth <= 320 {
mainLockButton.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
mainUnlockButton.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
mainSplitButton.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
mainRoundButton.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
} else if screenWidth <= 375 {
mainLockButton.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
mainUnlockButton.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
mainSplitButton.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
mainRoundButton.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
} else {
mainLockButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
mainUnlockButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
mainSplitButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
mainRoundButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
}
//调整主按钮的大小
让screenSize=UIScreen.main.bounds
设screenWidth=screenSize.width
如果屏幕宽度不完美,但看起来更好
let screenWidth = UIScreen.main.bounds.width
let buttons = [mainLockButton, mainUnlockButton, mainSplitButton, mainRoundButton]
buttons.forEach {
if screenWidth <= 320 {
$0.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
} else if screenWidth <= 375 {
$0.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
} else {
$0.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
}
}
let screenWidth=UIScreen.main.bounds.width
let按钮=[mainLockButton、mainUnlockButton、mainSplitButton、mainRoundButton]
钮扣{
如果屏幕宽度var fontSize:CGFloat{
让screenWidth=UIScreen.main.bounds.width
开关屏幕宽度{
案例uu其中屏幕宽度
var fontSize: CGFloat {
let screenWidth = UIScreen.main.bounds.width
switch screenWidth {
case _ where screenWidth <= 320: return 12
case _ where screenWidth <= 375: return 13
default: return 14
}
}
stackView.arrangedSubviews.forEach {
if let button = $0 as? UIButton {
button.titleLabel?.font = UIFont.systemFont(ofSize: fontSize, weight: .medium)
}
}