如何在UITextView文本中添加自定义语言

3

我希望能够制作一个泰米尔语键盘

功能:当我按下A按钮时,我希望显示 அ,当我按下AA时,我想显示 ஆ。

这是我目前为止尝试过的:

NSMutableString *updatedText = [[NSMutableString alloc] initWithString:textView.text];
[updatedText insertString:text atIndex:range.location];
NSLog(@"%i",range.location);
NSRange replaceRange = range , endRange = range;

if (text.length > 0) 
{
    NSLog(@" text length %i",text.length);

    replaceRange.length= text.length;
} 
else 
{
    replaceRange.length = 2;  // length of "hi" is two characters
    replaceRange.location -= 1; // look back one characters (length of "hi" minus one)
}

   int replaceCount = [updatedText replaceOccurrencesOfString:@"a" withString:@" அ" options:NSCaseInsensitiveSearch range:replaceRange];

   //   replaceCount = [updatedText replaceOccurrencesOfString:@"aa" withString:@" ஆ" options:NSCaseInsensitiveSearch range:replaceRange];

if (replaceCount >=1) {
    // update the textView's text
    textView.text = updatedText;

   // leave cursor at end of inserted text
    endRange.location = text.length+ replaceCount*100000000; // length diff of "hello" and "hi" is 3 characters
//  endRange.location;
    NSRange h= endRange;

   textView.selectedRange= h; 

    [updatedText release];
    return NO;
}    
[updatedText release];

return YES;     
}

如果我使用这种方法,它只会读取一个字符并用空格替换该字符。
1个回答

1
如果你想将所有的"a"替换为"அ",并将所有的"aa"替换为"ஆ" 请按照以下步骤操作: 首先测试"aa"。
//this will repleace all of "aa"
int replaceCount = [updatedText replaceOccurrencesOfString:@"aa" withString:@"ஆ" options:NSCaseInsensitiveSearch range:[NSMakeRange(0, updatedText.length)];

//this will repleace all of "a"
int replaceCount = [updatedText replaceOccurrencesOfString:@"a" withString:@"அ" options:NSCaseInsensitiveSearch range:[NSMakeRange(0, updatedText.length)];

非常感谢,它工作得很好...你太棒了...但是我有另一个问题,当我按下一个按钮时,它显示“அ”,但是当我按下aa时,它不显示“ஆ”。它显示“அஅ”,那么该怎么办呢? - dhaya
请按照我最初编写的顺序添加这些行,先替换@"aa"再替换"a",同时如果我的回答有帮助,请不要忘记点赞或接受答案 :) - Omar Abdelhafith
我使用类似的代码,但是没有得到答案: int replaceCount = [updatedText replaceOccurrencesOfString:@"aa" withString:@"ஆ" options:NSCaseInsensitiveSearch range:NSMakeRange(0, updatedText.length)]||[updatedText replaceOccurrencesOfString:@"a" withString:@"அ" options:NSCaseInsensitiveSearch range:NSMakeRange(0, updatedText)]; - dhaya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接