Autoshrink on a UILabel with multiple lines

前端 未结 17 2020
醉梦人生
醉梦人生 2020-11-28 21:31

Is it possible to use the autoshrink property in conjunction on multiple lines on a UILabel? for example, the large text size possible on 2 available lines.

17条回答
  •  温柔的废话
    2020-11-28 22:15

    extension UILabel{
    
    func adjustFont(minSize:Int, maxSize:Int){
        var newFont = self.font
        for index in stride(from: maxSize, to: minSize, by: -1) {
            newFont = UIFont.systemFont(ofSize: CGFloat(index))
            let size = CGSize(width: self.frame.width, height: CGFloat(Int.max))
            let size2 = (self.text! as NSString).boundingRect(with: size, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedStringKey.font:newFont!], context: nil).size
            if size2.height < self.frame.size.height{
                break
            }
        }
        self.font = newFont
    }
    

    }

    you need to assign value to the numberOfLines property of UILabel as well.

提交回复
热议问题