UITextInput setMarkedText:selectedRange not working? (Can't be!)

泄露秘密 提交于 2019-12-04 06:12:24
David

I have a conceptual problem with your code here. I may just be overlooking some small thing, but I think you just have a simple error in your programming.

1) When the text view contains no text: text: "", selectedRange : {0,0}, markedText: nil:

[_textView setMarkedText:@"月" selectedRange:NSMakeRange(0, 1)];

Result: text: "", selectedRange: {0,0}, markedText: nil. 

(Nothing changed)

2) When the text view contains text + some marked text: text: "AAA", selectedRange: {0,3}, markedText at the end: "太陽" then I do:

[_textView setMarkedText:@"地" selectedRangeNSMakeRange(0,3)];

Result: text: "AAA", selectedRange: {0,3}, markedText: nil; 

(the marked text became nil)

I just fixed your problem in the code that I ran in Xcode. It ran flawlessly and changed the marked text when I clicked three different buttons. When there was no marked text, the text was appended and marked.

text is the name of a UITextView:

-(IBAction)first
{
    [text setMarkedText:@"test" selectedRange:NSMakeRange(0, 4)];
}
-(IBAction)second
{
    [text setMarkedText:@"woo" selectedRange:NSMakeRange(0,4)];
}

Here I clicked "First"

Here I clicked "Second"

I hope this helps you and is deserving of the bounty.

Did you add the UITextField to the view before setting it's contents (and marking the text)?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!