I want to set the marked text programmatically and since iOS5 UITextView and UITextField conform to UITextInput this should be possible but for some reason I always get the markedText
to be nil. :(
What am I missing here?
This is what I've tried without success:
(While the textview is firstResponder)
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}, marked text at the end : "太陽" then I do:
[_textView setMarkedText:@"地" selectedRange:NSMakeRange(0,3)];
Result : text :"AAA", selectedRange: {0,3}, markedText: nil; (the marked text became nil)
In both cases is like setMarkedText:selectedRange:
would be setting the current marked text (if some) to nil.
Any help would be highly appreciated :)
UPDATE
Since the concept of marked text seems to be unclear, here is an explanation:
In multi stage input languages (e.g. Japanese) you can input regular text (like english) or input marked text (like japanese).
Here I wrote regular text : regular text
then I wrote marked text つ
and then I wrote marked text き
so it was appended becoming つき
.
Marked text (text in the blue box) represents a transitional or intermediate input stage because it can become into other characters/words called candidates. Candidates are shown in the big box.
This screenshot was taken when using a bluetooth keyboard in the iPad but you will get similar results when using the software keyboard when writing japanese for example.
Before writing つ
the textview was:
textView.text : "regular text"
textView.selectedRange : {12,0}
textView.markedText:nil
Then I wrote つ
and the text view became:
textView.text : "regular textつ"
textView.selectedRange : {13,0}
textView.markedText: "つ"
Then I wrote き
and the text view became:
textView.text : "regular textつき"
textView.selectedRange : {14,0}
textView.markedText: "つき"
Which is the current state of the text view.
I want to be able to set the marked text programatically :)
Is this possible? -- According to UITextInput docs it is.
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)?
来源:https://stackoverflow.com/questions/8066628/uitextinput-setmarkedtextselectedrange-not-working-cant-be