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

2

7

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.

enter image description here

Riegel answered 9/11, 2011 at 14:41 Comment(2)
To narrow the issue down, you could try using only Western characters and see if you still have the same issue.Expectorant
Tried it with alphabet too .... The result is the same :(Riegel
V
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.

Victory answered 26/11, 2011 at 6:21 Comment(6)
Sorry about the block quote being formatted as code. The engine wouldn't allow me to leave it as a block quote because it included code.Victory
Thank you David. About the first, second, and third problem: method [textView setMarkedText:@"月" selectedRange:range will NOT search and then mark , Instead it is supposed to input as marketText or replace the current markedText if there is one.Riegel
About the fourth problem, that is just an typo, in my program I wrote is correctly. I just corrected it here.Riegel
Hmm, okay I see what you are saying, let me try it real quick in xcode and see what I can do.Victory
Indeed , this time it worked. Thank you so much for your answer. I guess I will have to report this as a bug. setMarkedText:selectedRange work only when the textview is not the first responder, making the API almost useless because it is intended to be used while editing the text. Thank David again.Riegel
I'm glad this worked for you. You must be right because this worked when I hadn't made a single edit to the textview. I just slammed together a new project in 5 minutes and it worked perfectly.Victory
I
0

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

Implication answered 20/11, 2011 at 13:47 Comment(2)
According to this topic: #3278038 you should force the textfield, to be the first responder.Implication
The text view is first responder at that moment. BTW, I answered that question :)Riegel

© 2022 - 2024 — McMap. All rights reserved.