Multiline UILabel with adjustsFontSizeToFitWidth

前端 未结 8 1506
北海茫月
北海茫月 2020-12-04 12:16

I have a multiline UILabel whose font size I\'d like to adjust depending on the text length. The whole text should fit into the label\'s frame without truncating it.

8条回答
  •  爱一瞬间的悲伤
    2020-12-04 13:01

    Swift 4.2:

    //
    //  String+Utility.swift
    //
    //  Created by Philip Engberg on 29/11/2018.
    //  Original code from http://stackoverflow.com/a/4383281/463892 & http://stackoverflow.com/a/18951386
    //
    
    import Foundation
    import UIKit
    
    extension String {
        func fontSize(with font: UIFont, constrainedTo size: CGSize, minimumScaleFactor: CGFloat) -> CGFloat {
            var fontSize = font.pointSize
            let minimumFontSize = fontSize * minimumScaleFactor
    
            var attributedText = NSAttributedString(string: self, attributes: [.font: font])
            var height = attributedText.boundingRect(with: CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin], context: nil).size.height
    
            var newFont = font
            //Reduce font size while too large, break if no height (empty string)
            while height > size.height && height != 0 && fontSize > minimumFontSize {
                fontSize -= 1
                newFont = UIFont(name: font.fontName, size: fontSize)!
    
                attributedText = NSAttributedString(string: self, attributes: [.font: newFont])
                height = attributedText.boundingRect(with: CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin], context: nil).size.height
            }
    
            // Loop through words in string and resize to fit
            for word in self.components(separatedBy: NSCharacterSet.whitespacesAndNewlines) {
                var width = word.size(withAttributes: [.font: newFont]).width
                while width > size.width && width != 0 && fontSize > minimumFontSize {
                    fontSize -= 1
                    newFont = UIFont(name: font.fontName, size: fontSize)!
                    width = word.size(withAttributes: [.font: newFont]).width
                }
            }
            return fontSize
        }
    }
    

提交回复
热议问题