[英]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.