查找 UIButton 调整后的字体大小值?

Posted

技术标签:

【中文标题】查找 UIButton 调整后的字体大小值?【英文标题】:Find UIButton adjusted font size value? 【发布时间】:2017-02-11 12:51:57 【问题描述】:

我有方形按钮,无论是显示在 iPad 还是 iPhone 上,它们的大小都会有所不同。 我希望按钮标题的字体调整为按钮的大小,即。这样它们在更大的 iPad 屏幕上看起来不会太小,或者在较小的 iPhone 屏幕上看起来不会太大。

我想出了以下解决方案:

// Buttons is an outlet collection
for button in Buttons 
            button.titleLabel?.adjustsFontSizeToFitWidth = true
            button.titleEdgeInsets = UIEdgeInsetsMake(button.frame.height/3, button.frame.width/3, button.frame.height/3, button.frame.width/3)
            button.titleLabel!.numberOfLines = 1
            button.titleLabel!.minimumScaleFactor = 0.1
            button.clipsToBounds = true
            button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.alignCenters

            print(button.titleLabel!.font.pointSize)


        

这可以根据标题的宽度调整字体大小。因此,标题较短的按钮将比标题较长的按钮具有更大的字体。

我希望所有按钮的字体大小相同,因此我想访问其中一个(假设是最小的)调整后的大小以将其设置为所有按钮。我怎么能这样做?

另外,我正在考虑将字体调整为按钮 height 而不是 width 但找不到有效的解决方案。

【问题讨论】:

请查看答案并回复 查看更新后的答案,现在适用于任何价值 我在 ios 开发之旅的早期就遇到了这个问题,但我现在意识到这不是设计应用程序的好方法。这样做不容易的原因是因为您的内容实际上不应该在更大的屏幕上“增长”,而是您更改应用程序以显示更多内容。只是一个想法,但我发现它是开发应用程序的更好方法。 如果有帮助应该接受答案 【参考方案1】:

这是我在 swift 4 中的解决方案:

private func adjustedFontSizeOf(label: UILabel) -> CGFloat 
    guard let textSize = label.text?.size(withAttributes: [.font: label.font]), textSize.width > label.bounds.width else 
        return label.font.pointSize
    

    let scale = label.bounds.width / textSize.width
    let actualFontSize = scale * label.font.pointSize

    return actualFontSize

希望对某人有所帮助。

【讨论】:

【参考方案2】:

这对我来说适用于 Swift 5,我希望它有所帮助。

func adjustedFontSizeOf(label: UILabel, sizeBounds: CGSize) -> CGFloat 
    
    // return the real pointSize for a label with adjustsFontSizeToFitWidth activated
    // sizeBounds is the frame size where the label is enclosed (UIButton frame or similar)
    // because label.frame or label.bounds is always CGRect.zero
    
    let textSize = label.intrinsicContentSize
    
    var actualFontSize = label.font.pointSize
    if textSize.width > sizeBounds.width  // otherwise is not adjusted
        let scale = sizeBounds.width / textSize.width
        actualFontSize = scale * label.font.pointSize
    

    return actualFontSize


func setButtonsToTheSameFontSize(buttons: [UIButton], maxPointSize: CGFloat)

    // set a list of buttons whose titleLabels have adjustFontSizeToFitWidth activated
    // to the same font pointSize using the minimum size among them
    
    var fsize: CGFloat = maxPointSize
    for b in buttons 
        let f = adjustedFontSizeOf(label: b.titleLabel!, sizeBounds: b.frame.size)
        if f < fsize  fsize = f 
    
    for b in buttons  b.titleLabel?.font = b.titleLabel?.font.withSize(fsize) 

【讨论】:

【参考方案3】:

这是一种将所有按钮的字体大小设置为最小大小(其中)的解决方案。

第 1 步。我已经初始化了一些新按钮,用于测试:

let button = UIButton()
button.titleLabel!.font =  UIFont(name: "Helvetica", size: 20)
let button2 = UIButton()
button2.titleLabel!.font = UIFont(name: "Helvetica", size: 16)
let button3 = UIButton()
button3.titleLabel!.font = UIFont(name: "Helvetica", size: 19)
let Buttons = [button, button2, button3]

第 2 步。 然后,我添加了一个名为 min 的变量,并将其初始化为大于几乎任何可能的按钮字体大小 100 的值,如下所示:

var min = CGFloat(Int.max)

第 3 步。 之后,我在您的循环中添加了更多代码:

for btn in Buttons
  // here goes your code for your buttons(the code from the question)
  // then my code:
    if (btn.titleLabel?.font.pointSize)! < min
        min = (btn.titleLabel?.font.pointSize)! // to get the minimum font size of any of the buttons 
    


print(min) // prints 16, which is correct amongst the value [20,19,16] 

所以您的代码将如下所示:

for btn in Buttons
    btn.titleLabel?.adjustsFontSizeToFitWidth = true
    btn.titleEdgeInsets = UIEdgeInsetsMake(btn.frame.height/3, btn.frame.width/3, btn.frame.height/3, btn.frame.width/3)
    btn.titleLabel!.numberOfLines = 1
    btn.titleLabel!.minimumScaleFactor = 0.1
    btn.clipsToBounds = true
    btn.titleLabel?.baselineAdjustment = UIBaselineAdjustment.alignCenters
    print(btn.titleLabel!.font.pointSize)
    if (btn.titleLabel?.font.pointSize)! < min
          min = (btn.titleLabel?.font.pointSize)! // to get the minimum font size of any of the buttons 
         

第四步 将所有按钮的字体大小设置为min

Buttons.map  $0.titleLabel?.font = UIFont(name:($0.titleLabel?.font.fontName)! , size: min) 

for btn in Buttons
    btn.titleLabel?.font = UIFont(name: (btn.titleLabel?.font.fontName)!, size: min)

如果您的最小字体大小是...15,那么所有按钮的字体大小将变为 15


就是这样。希望对您有所帮助!

【讨论】:

不幸的是我不知道按钮的字体大小。假设我为所有按钮设置了 100 的字体大小,然后在显示时使用adjustsFontSizeToFitWidth 缩小它。 button.titleLabel!.font.pointSize即使字体已经缩小,仍然会返回 100。 然后将其设置为 var min = CGFloat(Int.max)

以上是关于查找 UIButton 调整后的字体大小值?的主要内容,如果未能解决你的问题,请参考以下文章

将 UIButton 字体大小调整为宽度

如何在 Swift 中以编程方式调整 UIButton 中文本的字体大小以适应宽度?

获取调整后的标签字体大小

iPhone——获取 UITextField 调整后的字体大小

UIButton 框架不会随着使用 Swift 5 的可访问性大字体而增加

pycharm怎么调字体大小?