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

I want the selected text to be programmatic, and since iOS5 UITextView and UITextField match UITextInput, this should be possible, but for some reason I always get markedText as zero. :( What am I missing here?

Here is what I tried without success:

(While textview is the first Responder)

1.- If the text view does not contain text:

text: "", selectedRange: {0,0}, markedText: no.

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

Result: text: "", selectedRange: {0,0}, markedText: nil. (Nothing changed)


2.- When the text view contains text + 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; (marked text has become null)


In both cases, it is like setMarkedText:selectedRange: will set the currently marked text (if any) to nil.

Any help would be greatly appreciated :)

UPDATE

Since the concept of tagged text seems unclear, here is an explanation:

In multi-stage input languages ​​(e.g., Japanese), you can enter plain text (e.g., English) or enter marked text (e.g., Japanese).

Here I wrote plain text: regular text , then I wrote selected text ぀ and then I wrote selected text き , so it was added to ぀き .

Marked text (text in a blue box) represents a transitional or intermediate input stage, as it can turn into other characters / words called candidates. Candidates are shown in a large box.

This screenshot was taken using the bluetooth keyboard on the iPad, but you will get similar results when using the soft keyboard when writing Japanese.

Before recording ぀ text representation 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 as follows:

 textView.text : "regular text぀き" textView.selectedRange : {14,0} textView.markedText: "぀き" 

What is the current state of the text view.

I want to be able to mark text programmatically :)

Is it possible? - According to UITextInput docs this.

enter image description here

+6
source share
2 answers

I have a conceptual problem with your code here. I can just forget about some small thing, but I think that you just have a programming error.

1) If the text view does not contain 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 + 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; 

(marked text has become null)

I just fixed your problem in the code that I ran in Xcode. It worked flawlessly and changed the selected text when I clicked three different buttons. When there was no selected text, the text was added 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

Hope this helps you and deserves generosity.

+6
source

Was UITextField added to the view before setting its contents (and labeling the text)?

0
source

Source: https://habr.com/ru/post/901122/


All Articles