iOS 7 - UITextView size font to fit all text into view (no scroll)

前端 未结 5 1661
Happy的楠姐
Happy的楠姐 2021-02-12 22:58

I am trying to find a non-deprecated method to size the font of a textview down so that all text fits in the textview without requiring scrolling.

The method \'sizeWithF

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-12 23:13

    Swift 5+

        var fontSize = Font.Size.section.rawValue
        let minSize = Font.Size.large.rawValue
    
        while fontSize > minSize {
            let textViewSize = CGSize(width: textView.frame.size.width, height: textView.frame.size.height)
            let size = textView.sizeThatFits(textViewSize)
    
            if size.height <= textViewSize.height {
                break
            }
    
            fontSize -= 1.0
        }
    
        textView.font = Font.primary.of(size: fontSize)
    

提交回复
热议问题