iphone - 如何设置字体大小以填充 UILabel 高度?

我见过很多改变 UILabel 大小的例子。

这是我想做的事情: 更改字体大小,使文本在新高度内尽可能大。

有什么线索吗?

最佳答案

我遇到了同样的问题,多亏了这个线程和 Joel 的算法,我可以解决它。 :-)

以下是我在 Swift 中的代码。我在 iOS 8 + 自动布局中。

Problem:

  1. 用户输入费用:

  1. 当用户点击“检查”按钮时,会从底部出现一个菜单,将所有内容推到屏幕顶部(缩小内容,包括标签):

After the fix:

这正是设计师的想法...... :)

我继承了 UILabel 并覆盖了 layoutSubviews。然后每次 UILabel 改变其大小时,都会重新计算字体大小:

//
//  LabelWithAdaptiveTextHeight.swift
//  123
//
//  Created by https://github.com/backslash-f on 12/19/14.
//

/*
 Designed with single-line UILabels in mind, this subclass 'resizes' the label's text (it changes the label's font size)
 everytime its size (frame) is changed. This 'fits' the text to the new height, avoiding undesired text cropping.
 Kudos to this Stack Overflow thread: bit.ly/setFontSizeToFillUILabelHeight
*/

import Foundation
import UIKit

class LabelWithAdaptiveTextHeight: UILabel {

    override func layoutSubviews() {
        super.layoutSubviews()
        font = fontToFitHeight()
    }

    // Returns an UIFont that fits the new label's height.
    private func fontToFitHeight() -> UIFont {

        var minFontSize: CGFloat = DISPLAY_FONT_MINIMUM // CGFloat 18
        var maxFontSize: CGFloat = DISPLAY_FONT_BIG     // CGFloat 67
        var fontSizeAverage: CGFloat = 0
        var textAndLabelHeightDiff: CGFloat = 0

        while (minFontSize <= maxFontSize) {

            fontSizeAverage = minFontSize + (maxFontSize - minFontSize) / 2

            // Abort if text happens to be nil
            guard text?.characters.count > 0 else {
              break
            }

            if let labelText: NSString = text {
                let labelHeight = frame.size.height

                let testStringHeight = labelText.sizeWithAttributes(
                    [NSFontAttributeName: font.fontWithSize(fontSizeAverage)]
                ).height

                textAndLabelHeightDiff = labelHeight - testStringHeight

                if (fontSizeAverage == minFontSize || fontSizeAverage == maxFontSize) {
                    if (textAndLabelHeightDiff < 0) {
                        return font.fontWithSize(fontSizeAverage - 1)
                    }
                    return font.fontWithSize(fontSizeAverage)
                }

                if (textAndLabelHeightDiff < 0) {
                    maxFontSize = fontSizeAverage - 1

                } else if (textAndLabelHeightDiff > 0) {
                    minFontSize = fontSizeAverage + 1

                } else {
                    return font.fontWithSize(fontSizeAverage)
                }
            }
        }
        return font.fontWithSize(fontSizeAverage)
    }
}

  • Refer to this Pastebin for execution logs (println() of each iteration).

https://stackoverflow.com/questions/8812192/

相关文章:

objective-c - Xcode 在 iOS 8 的 Main() 中抛出异常,断点为 'al

iphone - 如何阻止 NSNotification 中的观察者调用两次?

ios - 如何调整 UITableView 中原型(prototype)单元格的左边距?

objective-c - 从 NSMutableString 中删除最后一个字符

objective-c - 对 NSTimer 目标的弱引用以防止保留循环

objective-c - Objective-C 中的简单 http post 示例?

objective-c - iOS 8 Mapkit Objc 无法获取用户位置

objective-c - 可变集合有文字语法吗?

ios - 缩放 UIView 及其所有子项

objective-c - 如何在 Objective-C 中使用自定义委托(delegate)