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

Community
  • 1
  • 1
nacho4d
  • 43,720
  • 45
  • 157
  • 240

2 Answers2

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.

Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
Liftoff
  • 24,717
  • 13
  • 66
  • 119
  • 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. – Liftoff Nov 26 '11 at 06:22
  • 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. – nacho4d Nov 26 '11 at 15:19
  • About the **fourth** problem, that is just an typo, in my program I wrote is correctly. I just corrected it here. – nacho4d Nov 26 '11 at 15:23
  • Hmm, okay I see what you are saying, let me try it real quick in xcode and see what I can do. – Liftoff Nov 27 '11 at 01:17
  • 1
    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. – nacho4d Nov 27 '11 at 08:48
  • 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. – Liftoff Nov 27 '11 at 15:08
0

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

Aloys
  • 682
  • 4
  • 11
  • According to this topic: http://stackoverflow.com/questions/3277538/can-i-select-a-specific-block-of-text-in-a-uitextfield you should force the textfield, to be the first responder. – Aloys Nov 20 '11 at 15:06
  • 2
    The text view is first responder at that moment. BTW, I answered that question :) – nacho4d Nov 21 '11 at 02:48