Reputation: 13267
I would like to know when text exceeds the bounds of a UITextView and requires the user to scroll. Knowing how many lines a certain font will occupy is useless - how do I know how many lines a certain amount of text will take up?
In the image above, I would add a fade effect so that I can cut off the text view gracefully. However, it does not look correct to display the fade if there is not enought text to scroll.
Upvotes: 4
Views: 3179
Reputation: 16715
Updated for Swift 5.x
func textExceedBoundsOf(_ textView: UITextView) -> Bool {
textView.contentSize.height > self.textView.bounds.height
}
Upvotes: 3
Reputation: 47699
Get contentSize
after setting the text. It's a CGSize and the height
will be the height required for the UITextView to display the text without scrolling.
myTextView.text = someText;
CGSize textSize = myTextView.contentSize;
float verticalSpaceNeededByText = textSize.height;
Upvotes: 6
Reputation: 1134
I write a custom appDelegate method that I can call for several usages throughout the several locations within the application. Handy for determining the height to perfectly fit text within a views, custom cell height, table row heights with dynamic text data, and textViews, etc...
It is very flexible and you can easily determine the "perceived" height of what any text needs to fully show on screen, in a container, or in a view.
After you get the result, you can just compare it against the size of your textView Height, as in....
Keep in mind that there is a little buffer between the TextView's Height and the TextView's textContainer's height. Very minimal, but you can adjust for that by sending in a MinHeight slightly smaller than your (TextView.frame.size.height -3).
You can also use the optional padding to give yourself a little buffer on the other size if you need it as well which will return a larger container.
If you use a custom Font, compensate for that with a new variable and take that into account, but as-is, this works great for System Fonts.
Example Logic:
//Logic Example:
if([appDelegate calculateTextHeight:@"Your_String_Passed_In_Here"] > textView.frame.size.height) {
//Text will exceed Height of TextView and require scroll.
}else {
//Text is within the TextView without need to scroll.
}
The Method:
-(NSInteger)calculateTextHeight:(NSString *)text:(NSInteger)FontSize:(NSInteger)MaxWidth:(NSInteger)MinHeight:(NSInteger)optionalHeightPadding {
//Determine the Size based on the Inputs:
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FontSize] constrainedToSize:CGSizeMake(MaxWidth, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
//Add Optional Padding:
int calH = size.height + optionalHeightPadding;
//Determine if larger than Min:
if (calH > MinHeight) {
return size.height + optionalHeightPadding;
}
//Return Min
return MinHeight;
}
Heads Up Reminder: I used to use UILineBreakModeWordWrap but it was deprecated as of iOS 6, use NSLineBreakByWordWrapping instead.
Upvotes: 0
Reputation: 21966
Take the attributed text of the text view and compute the size using this method:
- (CGSize) size;
Then you can do the job knowing the text view's bounds and this UIFont's property:
@property(nonatomic,readonly) CGFloat lineHeight;
Note that the size of the attributed string is useful only for the length, because it considers the text to be on the same line.And it doesn't consider the space between lines.
Upvotes: 0