7
4

2 回答 2

6

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" When First is Clicked

Here I clicked "Second" When Second is Clicked

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

于 2011-11-26T06:21:40.057 回答
0

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

于 2011-11-20T13:47:27.160 回答