views:

143

answers:

1

Hi all

how can I learn total number of characters a UITextView shows in its specific contentSize after UILineBreakModeWordWrap applied.

I will use different strings everytime as the text of UITextView. The strings are much longer than UITextView with its specified area can contain. The visible characters will be different for every different string because of the different length of spaces at the end of the lines.

And I want to learn the number of characters visible in my UITextView.

+1  A: 

Use this method from NSString:

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
  • You calculate first how long the height of the UITextView should be to fit the whole text.
  • Using proportions you find out how long your string should be to fit the actual size.
  • a) If that string fits the current size you can add characters one by one and check again the size until it doesn't fit anymore -> the limit is what you're looking for.
  • b) If that string doesn't fit the current size then you remove characters from it one by one until it fits -> again, this limit is what you're looking for in this case.

It is a very intensive method but I think it's the only one (You can optimize it by adding/removing half of the remaining string instead of adding/removing characters).

Alin
Thank you Alin, i think you are right I can't find any other "more useful" method too. I will do as you explain but i will add or remove not character by character but word by word.
chelenli