Skip to content

Instantly share code, notes, and snippets.

@fethica
Created June 30, 2013 00:58
Show Gist options
  • Save fethica/5893329 to your computer and use it in GitHub Desktop.
Save fethica/5893329 to your computer and use it in GitHub Desktop.
Size-to-Fit Text in UITextView
#define kDefaultFontSize 24.0
myTextView.text = @"Some long string that will be in the UITextView";
myTextView.font = [UIFont systemFontOfSize:kDefaultFontSize];
//setup text resizing check here
if (myTextView.contentSize.height > myTextView.frame.size.height) {
int fontIncrement = 1;
while (myTextView.contentSize.height > myTextView.frame.size.height) {
myTextView.font = [UIFont systemFontOfSize:kDefaultFontSize-fontIncrement];
fontIncrement++;
}
}
@TusharSharma651
Copy link

This is next level

@Mari-Rider09
Copy link

how to set width of uitext view

@Nevazhnovu
Copy link

Nevazhnovu commented Mar 4, 2022

Swift 5:

var fontSize = 24
let family = UIFont.systemFont(ofSize: fontSize).familyName
if (myTextView.contentSize.height > myTextView.frame.size.height) {
                var fontIncrement = 1.0;
                while (myTextView.contentSize.height > myTextView.frame.size.height) {
                    fontSize = CGFloat(fontSize - fontIncrement)
                    myTextView.font = UIFont(name: family, size: fontSize)
                    fontIncrement+=1;
                }
            }
            

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment