I'm trying to determine the amount of attributed text that can fit in the constant size textview.I have tried with the CTFrameSetter but I think that's only helpful when we already know the text that we want to add. So far I have tried this
func numberOfCharactersThatFitTextView() -> Int {
let fontRef = CTFontCreateWithName(font!.fontName as CFString, font!.pointSize, nil)
let attributes = [kCTFontAttributeName : fontRef]
let attributedString = NSAttributedString(string: text!, attributes: attributes as [NSAttributedString.Key : Any])
let frameSetterRef = CTFramesetterCreateWithAttributedString(attributedString as CFAttributedString)
var characterFitRange: CFRange = CFRange()
CTFramesetterSuggestFrameSizeWithConstraints(frameSetterRef, CFRangeMake(0, 0), nil, CGSize(width: bounds.size.width, height: bounds.size.height), &characterFitRange)
return Int(characterFitRange.length)
}