iPhone: Limiting text input in an UITextView
Asked Answered
E

2

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

Einstein answered 9/4, 2011 at 13:14 Comment(0)
G
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.

Goree answered 9/4, 2011 at 14:45 Comment(4)
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?Einstein
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.Einstein
@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. (Oh, and make sure you accept this answer if it solved your problem :p)Goree
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!Einstein
T
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

Teliospore answered 9/4, 2011 at 13:37 Comment(1)
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!Einstein

© 2022 - 2024 — McMap. All rights reserved.