从位置外推TextView的文字
问题描述:
我有一个TextView,我需要从位置插入一个字符串。使用此方法派生的位置从位置外推TextView的文字
NSInteger location = textView.selectedRange.location;
但我不知道如何把它放入一个字符串。
非常感谢
答
如果你想更换一个词的选择,最简单的将是这样的:
NSRange range = textView.selectedRange;
if (range.location != NSNotFound) {
NSString *replacement = @"some word";
textView.text = [textView.text stringByReplacingCharactersInRange:range
withString:replacement];
}
对于一个空的选择,这将插在光标位置的文本,如果文本被选中,它将被替换。在iOS 5上,使用UITextInput
协议中的方法可能会更高效,但使用起来也更麻烦。
答
是你在找什么?
NSInteger location = textView.selectedRange.location;
UITextView yourTxtView;
yourTxtView.text = [NSString stringWithFormat:@"%d",location];