Resize font size to fill UITextView?

后端 未结 15 978
春和景丽
春和景丽 2020-12-01 07:03

How would I set the font size of text in a UITextView such that it fills the entire UITextView? I\'d like the user to type in their text, then have the text fill the entire

15条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 07:36

    In viewDidLoad:

    textView.textContainer.lineFragmentPadding = 0;
    textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
    

    You need to add UITextViewDelegate:

    - (void)updateTextFont:(UITextView *)textView {
    
        CGSize textViewSize = textView.frame.size;
        CGSize sizeOfText = [textView.text sizeWithAttributes:@{NSFontAttributeName: textView.font}];
    
        CGFloat fontOfWidth = floorf(textView.font.pointSize / sizeOfText.height * textViewSize.height);
        CGFloat fontOfHeight = floorf(textView.font.pointSize / sizeOfText.width * textViewSize.width);
    
        textView.font = [textView.font fontWithSize:MIN(fontOfHeight, fontOfWidth)];
    }
    

提交回复
热议问题