如何避免将一个单词拆分为两个 UITextView

发布于 2024-11-08 08:31:57 字数 2014 浏览 0 评论 0原文

我想这是一个有点尴尬的问题,所以让我用一个例子来重新表述一下: 在此处输入图像描述

我有一个 UITextView,它限制为一定数量的字符。定期使用 - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText 检查。一旦文本大于阈值(在插图中为 8 个字符),应用程序将更改到下一个 textView 并将输入的下一个字符(在本例中为 p)复制到新的 UITextView。然后,用户可以输入单词的其余部分(在本例中为 py)。

我不想将单词拆分为两个 UITextView,而是希望在下一个 UITextView 中包含整个单词“happy”。换句话说,我需要将字符串 1 中的“hap”复制到字符串 2 的开头,然后从字符串 1 中删除“hap”。

我想出了这段代码,它检查字符串 1 中的空格,并将空格后的所有内容复制到string 2。但它不起作用:

NSLog(@"text of current string: %@", aTextView.text);           // e.g. "I am hap"
         NSLog(@"chars in aTextView.text: %i", aTextView.text.length);  // e.g. 8 chars
         NSLog(@"text for next string: %@ ", aText);                    // e.g. "py"

         NSString *aTextModified = aText;

         for (int i = aTextView.text.length; i > 0; i--) {

             NSLog(@"I currently check: '@%'", [aTextView.text characterAtIndex:i]);

             if ([[aTextView.text characterAtIndex:i] isEqualToString:@" "]) {
                 // job done
             } else {
                 NSLog(@"I add %@ infront of %@", [aTextView.text characterAtIndex:i], aText);
                 aTextModified = [NSString stringWithFormat:@"%@%@", [aTextView.text characterAtIndex:i], aText];
                 NSLog(@"I delete %@ as I have put it into the next string...", [aTextView.text characterAtIndex:i]);
                 [aTextView.text characterAtIndex:i] = @"";
             }
         }

XCode 向我发出警告 'Invalid receive type 'unichar'' for the line if ([[aTextView.text characterAtIndex:i] isEqualToString:@" "])。我想我没有使用characterAtIndex,对吗?我是否不允许将其应用于 UITextView 中的文本(应该是 UIString)?

另外,我在最后一行收到错误“LValue required as left opperand of allocateement” [aTextView.text characterAtIndex:i] = @""; 再次,我想我不太明白如何访问 UITextView 中的字符并修改它。

如果有任何有关如何正确执行此操作的建议,以及是否有更简单的方法来实现我想要做的事情,我将非常感激。

I guess this is a slightly awkward question, so let me rephrase it with an illustration:
enter image description here

I have an UITextView which is limited to a certain amount of chars. This is regularly checked with - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText. Once the text is larger than the thresh hold (in the illustration it would be 8 chars), the app changes to the next textView and copies the next char entered (in this case p) to the new UITextView. The user can then enters the rest of the word (in this case py).

Instead of splitting the word over two UITextViews, I would like to have the entire word 'happy' in the next UITextView. In other words, I need to copy the 'hap' from string 1 to the beginning of string 2 and then delete 'hap' from string 1.

I came up with this code which checks string 1 for spaces and copies everything after a space into string 2. But it won't work:

NSLog(@"text of current string: %@", aTextView.text);           // e.g. "I am hap"
         NSLog(@"chars in aTextView.text: %i", aTextView.text.length);  // e.g. 8 chars
         NSLog(@"text for next string: %@ ", aText);                    // e.g. "py"

         NSString *aTextModified = aText;

         for (int i = aTextView.text.length; i > 0; i--) {

             NSLog(@"I currently check: '@%'", [aTextView.text characterAtIndex:i]);

             if ([[aTextView.text characterAtIndex:i] isEqualToString:@" "]) {
                 // job done
             } else {
                 NSLog(@"I add %@ infront of %@", [aTextView.text characterAtIndex:i], aText);
                 aTextModified = [NSString stringWithFormat:@"%@%@", [aTextView.text characterAtIndex:i], aText];
                 NSLog(@"I delete %@ as I have put it into the next string...", [aTextView.text characterAtIndex:i]);
                 [aTextView.text characterAtIndex:i] = @"";
             }
         }

XCode gives me the warning 'Invalid receiver type 'unichar'' for the line if ([[aTextView.text characterAtIndex:i] isEqualToString:@" "]). I guess I'm not using the characterAtIndex right? Am I not allowed to apply this to a text (which should be an UIString) in an UITextView?

Also, I get the error 'LValue required as left opperand of assignement' for the last line [aTextView.text characterAtIndex:i] = @""; Again, I guess I don't really understand how to access a char in an UITextView and modify it.

I'd be very grateful for any suggestions of how to do this right - and if there are easier ways to achieve what I'm trying to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-11-15 08:36:08

好吧,让我们从简单的开始。我创建了以下正则表达式,它将匹配字符串中的最后一个单词以及是否存在空格或标点符号等非单词字符。

我认为使用正则表达式并确定它匹配的位置来操作字符串更容易。不过,我不确定大文本中的性能如何,但我认为可以通过使用一些假设来解决,例如:textview 不会在前 1000 个字符内跳过,因此仅使用最后 50 个字符可能是安全的正则表达式。

NSString *aString = @"I am happy however I want the last word and it's positions !!!!";

NSString * const regularExpression = @"\\b(\\w+)\\b(\\W*)$";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regularExpression
                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionUseUnicodeWordBoundaries
                                                                         error:&error];

NSArray *matches = [regex matchesInString:aString
                                  options:0
                                    range:NSMakeRange(0, [aString length])];

for (NSTextCheckingResult *match in matches) {
    for (int i = 0; i < [match numberOfRanges]; i++) {
        NSLog(@"substring %d: %@", i, [aString substringWithRange:[match rangeAtIndex:i]]);
    }
}

如果匹配,您可以轻松地迭代结果,从而获得最后一个单词和一些尾随非单词字符,但最重要的是您可以获得每个匹配的范围,以便您可以确定是否超过阈值。

Ok let's start out simple. I've created the following regular expression which will match the last word in a string and if there is non word characters like white spaces or punctuation marks.

I think it's easier to work with a regex and determine on where it matches to manipulate the string. However I am not sure how performance will be in large texts, but I think that can be solved by using some assumptions like: textview one won't skip within the first 1000 characters so it may be safe to use only the last 50 characters for the regex.

NSString *aString = @"I am happy however I want the last word and it's positions !!!!";

NSString * const regularExpression = @"\\b(\\w+)\\b(\\W*)$";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regularExpression
                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionUseUnicodeWordBoundaries
                                                                         error:&error];

NSArray *matches = [regex matchesInString:aString
                                  options:0
                                    range:NSMakeRange(0, [aString length])];

for (NSTextCheckingResult *match in matches) {
    for (int i = 0; i < [match numberOfRanges]; i++) {
        NSLog(@"substring %d: %@", i, [aString substringWithRange:[match rangeAtIndex:i]]);
    }
}

If it matches you can easily iterate through the result which gives you the last word and some trailing non word characters, but most important is that you get the range of each match so you can determine if a threshold is crossed.

給妳壹絲溫柔 2024-11-15 08:35:04

很少有东西

NSLog(@"I currently check: '@%'", [aTextView.text characterAtIndex:i]);

你在这里得到一个 C 字符,所以你应该使用 %c 代替。

[[aTextView.text characterAtIndex:i] isEqualToString:@" "]

同样,您在这里所做的是向 C 字符发送一条消息,这是不正确的。您可以将其更改为 [aTextView.text characterAtIndex:i] == " " 以正确执行此操作。

[aTextView.text characterAtIndex:i] = @"";

text 被定义为不可变字符串。你不能这样改变它。您必须声明一个可变字符串并复制 text 的内容。修改可变字符串后,您可以将其设置为text

这也将给你最后的发言权。

NSScanner *wordsScanner = [NSScanner scannerWithString:@"I am happy however I want the last word and it's positions !!!!"];
NSString *theLastWord;
while (![wordsScanner isAtEnd]) {
    [wordsScanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] intoString:&theLastWord];
}

// theLastWord has the value.

编辑

NSString *text = aTextView.text;
int index = text.length - 1;
while ([text characterAtIndex:index] != " ") {
    index--;
}

NSString stringToBeMoved = [text substringFromIndex:index];
aTextView.text = [text substringToIndex:(index+1)];

Few things

NSLog(@"I currently check: '@%'", [aTextView.text characterAtIndex:i]);

You get a C character here so you should be using %c instead.

[[aTextView.text characterAtIndex:i] isEqualToString:@" "]

Again, what you're doing here is sending a message to a C character which is incorrect. You can change it to [aTextView.text characterAtIndex:i] == " " to do it correctly.

[aTextView.text characterAtIndex:i] = @"";

text is defined as an immutable string. You can't change it like this. You will have to declare a mutable string and copy the contents of text over. After modifications to the mutable string, you can set it to text.

This will also give you the last word.

NSScanner *wordsScanner = [NSScanner scannerWithString:@"I am happy however I want the last word and it's positions !!!!"];
NSString *theLastWord;
while (![wordsScanner isAtEnd]) {
    [wordsScanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] intoString:&theLastWord];
}

// theLastWord has the value.

EDIT

NSString *text = aTextView.text;
int index = text.length - 1;
while ([text characterAtIndex:index] != " ") {
    index--;
}

NSString stringToBeMoved = [text substringFromIndex:index];
aTextView.text = [text substringToIndex:(index+1)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文