限制UITextView中的文本 [英] Limiting text in a UITextView

查看:93
本文介绍了限制UITextView中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制文本输入到UITextView可可触摸。我真的想限制行数,而不是字符数。到目前为止我有这个计数的行数:

   - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange :( NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@\\\
]){
rows ++;
}
NSLog(@Rows:%i,rows);
return YES;
}

但是,如果自动换行而不是用户按返回键。有没有办法检查文本是否被包装类似于检查\ n?



谢谢。



注意我的奶油在这(从宽度减去15)。这可能与我的意见(我有一个在另一个),所以你可能不需要它。

   - BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText 
{
NSString * newText = [aTextView.text stringByReplacingCharactersInRange:aRange withString:aText];

// TODO - 找出为什么字符串的大小小于实际的宽度,所以你得到额外的包装字符,除非你把东西关闭
CGSize tallerSize = CGSizeMake(aTextView .frame.size.width-15,aTextView.frame.size.height * 2); //假装有更多的垂直空间来获取额外的行来检查
CGSize newSize = [newText sizeWithFont:aTextView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];

if(newSize.height> aTextView.frame.size.height)
{
[myAppDelegate beep];
return NO;
}
else
return YES;
}


I am trying to limit the text input into a UITextView in cocoa-touch. I really want to limit the amount of rows rather than the number of characters. So far I have this to count the amount of rows:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@"\n"]) {
        rows++;
    }
    NSLog(@"Rows: %i", rows);
    return YES;
}

However this doesn't work if the line is automatically wrapped rather than the user pressing the return key. Is there a way to check if the text was wrapped similar to checking for "\n"?

Thanks.

解决方案

Unfortunately, using NSString -stringWithFont:forWidth:lineBreakMode: doesn't work - which ever wrap mode you choose, the text wraps with a width that is less than the current width, and the height becomes 0 on any overflow lines. To get a real figure, fit the string into a frame that is taller than the one you need - then you'll get a height that is greater than your actual height.

Note my fudge in this (subtracting 15 from the width). This might be something to do with my views (I have one within another), so you might not need it.

- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText
{
        NSString* newText = [aTextView.text stringByReplacingCharactersInRange:aRange withString:aText];

        // TODO - find out why the size of the string is smaller than the actual width, so that you get extra, wrapped characters unless you take something off
        CGSize tallerSize = CGSizeMake(aTextView.frame.size.width-15,aTextView.frame.size.height*2); // pretend there's more vertical space to get that extra line to check on
        CGSize newSize = [newText sizeWithFont:aTextView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];

        if (newSize.height > aTextView.frame.size.height)
            {
            [myAppDelegate beep];
            return NO;
            }
        else
            return YES;
}

这篇关于限制UITextView中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆