简体   繁体   English

将用户输入的UITextVIew内容限制为自己的框架

[英]Keeping User-Input UITextVIew Content Constrained to Its Own Frame

Trying to create a large textbox of fixed size. 试图创建一个固定大小的大文本框。

This problem is very similar to the 140 character constraint problem, but instead of stopping typing at 140 characters, I want to stop typing when the edge of the textView's frame is reached, instead of extending below into the abyss. 这个问题与140个字符的约束问题非常相似,但是我不想在输入140个字符时停止键入,而是想在到达textView框架的边缘时停止键入,而不是在下方扩展到深渊。 Here is what I've got for the delegate method. 这是我对委托方法的了解。 Seems to always be off by a little bit. 似乎总是会有点偏离。 Any thoughts? 有什么想法吗?

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    BOOL edgeBump = NO;
    CGSize constraint = textView.frame.size;

    CGSize size = [[textView.text stringByAppendingString:text] sizeWithFont:textView.font 
                                                           constrainedToSize:constraint 
                                                               lineBreakMode:UILineBreakModeWordWrap];
    CGFloat height = size.height;

    if (height > textView.frame.size.height) {
        edgeBump = YES;
    }

    if([text isEqualToString:@"\b"]){
        return YES;
    } else if(edgeBump){
        NSLog(@"EDGEBUMP!");
        return NO;
    }   

    return YES;
}

EDIT: As per Max's suggestion below, here is the code that works (note, however, that autocorrect and cut do not work here): 编辑:根据下面的麦克斯的建议,这是有效的代码(但是请注意,自动更正和剪切在这里不起作用):

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    CGSize constraint = textView.frame.size;

    NSString *whatWasThereBefore = textView.text;

    textView.text = [textView.text stringByReplacingCharactersInRange:range withString:text];

    if (textView.contentSize.height >= constraint.height) {
        textView.text = whatWasThereBefore;
    }

    return NO;
}

I had similar problem and noticed that sizeWithFont returns the wrong size. 我有类似的问题,并注意到sizeWithFont返回错误的大小。 I used yourTextView.contentSize instead. 我改用yourTextView.contentSize So you can add characters, obtain the content size and then delete newly added characters if content size is bigger then the maximum one. 因此,您可以添加字符,获取内容大小,然后在内容大小大于最大值时删除新添加的字符。

Edit2: fixed Edit2:固定

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    UITextView* clone_tv = [[[UITextView alloc] initWithFrame: textView.frame] autorelease];
    clone_tv.text = [textView.text stringByReplacingCharactersInRange:range withString:text];

    return (clone_tv.contentSize.height <= textView.frame.size.height*2);
}

Notice the clone_tv. 注意clone_tv。 We are actually changing the text in clone text view, and not in the real one. 我们实际上是在克隆文本视图中更改文本,而不是在实际视图中。 In this case autocorrect and cut works. 在这种情况下,自动更正和剪切将起作用。 However I recommend you to create some ivar named cloneTextView in your view controller and use it every time. 但是,我建议您在视图控制器中创建一些名为cloneTextView的ivar,并每次使用它。 Doing like this you will avoid unnecessary allocations. 这样做可以避免不必要的分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM