Use this method from NSString:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
- First, you calculate how long the height of the UITextView should fit the entire text.
- Using proportions, you will find out how long your string should match the actual size.
- a) If this line matches the current size, you can add characters one by one and check the size again until it no longer fits → the limit is what you are looking for.
- b) If this line does not match the current size, you delete characters from it one by one until it is set again →, this limit is what you are looking for in this case.
This is a very intensive method, but I think that it is the only one (you can optimize it by adding / removing half of the remaining line instead of adding / removing characters).
source share