1

I am trying to limit the amount of LINES a user can have in an UITextView. Let's consider I only want to allow 4 lines (i.e. in the following example, you can't go higher than a height of 84 pixels with the given font in a given UITextView with a width of 200).

I coded this to achieve the limiting and it works fine:

    - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText {

    NSString* newText = [aTextView.text stringByReplacingCharactersInRange:aRange withString:aText];


    CGSize strSize = [newText sizeWithFont:textView.font constrainedToSize:CGSizeMake(200, 10000) lineBreakMode:UILineBreakModeWordWrap];

if (strSize.height > 100)

    {

        return NO; // can't enter more text
    }
    else
        return YES; // let the textView know that it should handle the inserted text
    }

So far so good. The problem starts when I try to be sneaky and enter as many 'x's as I can before each return. In that case, I can get an extra of 4 lines... which I obviously do not want to allow. I assume that this has something to do with the lineBreakMode? Is there any case to alter this? I haven't found much in the docs (but then again, I usually have a hard time understanding them).

I attached a couple of screenshots to illustrate the problem. I'd be grateful for any suggestions. Thanks a lot!


Normal behaviour - stops after 4 lines Deviant behaviour - too many lines through additional x-es

n.evermind
  • 11,944
  • 19
  • 78
  • 122

2 Answers2

2

(I hope I understood your problem correctly... )

If you look closely, there's a margin in UITextView. If you use sizeWithFont: carelessly, it can return unexpected results in special cases (because you disrespect the text view's margin).

You should either 1) adjust the width you use in sizeWithFont: to respect the text view's margin or 2) use an auxiliary UITextView object to accurately compute the content size.

ryyst
  • 9,563
  • 18
  • 70
  • 97
  • Thanks a lot for your thoughts. Do you know how I find out what the exact margin is? Is it just something to experiment with? – n.evermind Apr 09 '11 at 18:22
  • ryyst: This actually WORKS! thanks so much, you were right. It's all down to reducing the 200 a bit. Still, I'm wondering if there is a way to find out the margins programatically and not by guessing. – n.evermind Apr 09 '11 at 18:29
  • 1
    @n.evermind: I don't know whether you can find out the margins programmatically - I don't think it is possible using public APIs. By the way, option 2 is described on Apple's dev forums [here](http://discussions.apple.com/thread.jspa?threadID=1923059). (Oh, and make sure you accept this answer if it solved your problem :p) – ryyst Apr 09 '11 at 18:44
  • sure! i didn't mark it first as I needed to wait for 15 minutes or so, but here we go. Thanks a lot again, this really helped! – n.evermind Apr 10 '11 at 10:23
1

Looks to me like word wrapping is on in UITextView and there's not much you can do to change it (please tell me I am wrong). If you want 4 lines only, maybe 4 UITextField's in a UIScrollView (sent the return key to Next). Or reduce the ContentSize when the length of the render rectangle of each line exceeds the height of one line.

See also limiting text in a uitextview

Community
  • 1
  • 1
Hiltmon
  • 1,265
  • 9
  • 6
  • Thanks, but I really need to use an UITextView. Don't really understand what you mean by reducing centensize? Also, the link you've provided (thanks for that!) suggestest that you could do a work around by reducing the width of the checked text, but then again, this isn't ideal. Thanks though for your time! – n.evermind Apr 09 '11 at 13:51