[英]How to replace text in UITextView with selected range?
我想在选定范围内替换 UITextView 文本中的文本。 这是我的问题。 我在这里提到我做了什么? 我想做什么? 我有一个 UITextView 并在 textview 中输入了以下文本。
Ask question here
,我已经在 textview 中保存了文本的范围。 文本范围是{17, 0}
。 我在-(void) textViewDidChange:(UITextView *)textView
textViewDidChange -(void) textViewDidChange:(UITextView *)textView
textView 委托中使用 NSRange。
现在我想用answer
替换文本question
,我想用them. The UITextView.text look like this,
替换here
的文本them. The UITextView.text look like this,
them. The UITextView.text look like this,
在替换文本后询问答案。
如何用选定的范围替换文本? 你能帮我么? 提前致谢。
从 iOS 7 开始,textView 中的 textStorage 继承自NSMutableAttributedString
因此您可以使用这些方法:
目标-C
[self.textView.textStorage replaceCharactersInRange:withString:];
要么
[self.textView.textStorage replaceCharactersInRange:withAttributedString:];
迅速
textView.textStorage.replaceCharacters(in: range, with: string)
嗯...我不确定您要做什么,但如果您的目标是更改选定范围内的某些字符,您可以按照以下步骤操作:
获取您的 UITextView 内容并将其放入 NSString 中:
NSString *textViewContent = textView.text;
更改您想要的范围内的字符:
NSString *newContent = [textViewContent stringByReplacingCharactersInRange:range withString:replacement];
用新内容替换旧内容:
textView.text = newContent;
无论如何,如果您只是想用Ask answer them替换这里的 Ask question ,那么最快的解决方案就是:
textView.text = @"Ask answer them";
来自我头顶的解决方案..
NSArray *Dividedstring = [[self.TextView.text] componentsSeparatedByString:@" question here "]; // this will divide the string into two parts ..one before question here and second after question here.
NSString * firstpart = [Dividedstring objectAtIndex:0];
NSString * secondpart = [Dividedstring objectAtIndex:0];
self.TextView.text = [NSString stringwithFormat:@"%@ answer here %@",firstpart,secondpart];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.