Created
June 30, 2013 00:58
-
-
Save fethica/5893329 to your computer and use it in GitHub Desktop.
Size-to-Fit Text in UITextView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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++; | |
} | |
} |
This is next level
how to set width of uitext view
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
Hi! I'm struggling with this right now. I've seen similar solutions on SO, but I don't know where to put the check and while loop. I tried in textViewShouldChange() and textViewDidChange() logging out the content size and the frame size. But they are always the same. Any suggestions? Thanks.