如何避免将一个单词拆分为两个 UITextView
我想这是一个有点尴尬的问题,所以让我用一个例子来重新表述一下:
我有一个 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,让我们从简单的开始。我创建了以下正则表达式,它将匹配字符串中的最后一个单词以及是否存在空格或标点符号等非单词字符。
我认为使用正则表达式并确定它匹配的位置来操作字符串更容易。不过,我不确定大文本中的性能如何,但我认为可以通过使用一些假设来解决,例如:textview 不会在前 1000 个字符内跳过,因此仅使用最后 50 个字符可能是安全的正则表达式。
如果匹配,您可以轻松地迭代结果,从而获得最后一个单词和一些尾随非单词字符,但最重要的是您可以获得每个匹配的范围,以便您可以确定是否超过阈值。
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.
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.
很少有东西
你在这里得到一个
C
字符,所以你应该使用%c
代替。同样,您在这里所做的是向
C
字符发送一条消息,这是不正确的。您可以将其更改为[aTextView.text characterAtIndex:i] == " "
以正确执行此操作。text
被定义为不可变字符串。你不能这样改变它。您必须声明一个可变字符串并复制text
的内容。修改可变字符串后,您可以将其设置为text
。这也将给你最后的发言权。
编辑
Few things
You get a
C
character here so you should be using%c
instead.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.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 oftext
over. After modifications to the mutable string, you can set it totext
.This will also give you the last word.
EDIT