UITextInput setMarkedText:selectedRange不工作? (不能!) [英] UITextInput setMarkedText:selectedRange not working? (Can't be!)
问题描述
我想以编程方式设置标记文本,因为iOS5 UITextView和UITextField符合UITextInput这应该是可能的但是由于某种原因我总是得到 markedText
为零。 :(
我在这里缺少什么?
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:
(虽然textview是firstResponder)
(While the textview is firstResponder)
1.-当文本视图不包含文字时
text:,selectedRange:{0,0},markedText:nil。
text: "", selectedRange : {0,0}, markedText: nil.
[_textView setMarkedText:@"月" selectedRange:NSMakeRange(0, 1)];
结果:
text:,selectedRange:{0,0},markedText:nil。(没有更改)
Result: text : "", selectedRange: {0,0}, markedText: nil. (Nothing changed)
2.-当文本视图包含文本+一些标记文本时:
text:AAA,selectedRange = {0,3},最后标记文字:太阳
然后我这样做:
text : "AAA", selectedRange = {0,3}, marked text at the end : "太陽" then I do:
[_textView setMarkedText:@"地" selectedRange:NSMakeRange(0,3)];
结果:
text:AAA,selectedRange:{0,3},markedText:nil;(标记的文字变为零)
Result : text :"AAA", selectedRange: {0,3}, markedText: nil; (the marked text became nil)
在这两种情况下都像 setMarkedText:selectedRange:
将是将当前标记的文本(如果有的话)设置为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 :)
由于标记文字的概念似乎不清楚,这里有一个解释:
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.
这个截图是在iPad上使用蓝牙键盘时拍摄的,但是当你在写日文时使用软件键盘会得到类似的结果例如。
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.
在写つ
之前,textview是:
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 :)
这可能吗? - 根据UITextInput文档,它是。
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)当文本视图不包含文本时: text:,selectedRange:{0,0},markedText:nil
:
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.
(没有变化)
2)当文本视图包含文本+一些标记文本时: text:AAA
, selectedRange:{0,3}
, markedText
最后:太阳然后我做:
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;
(标记文字为零)
我刚刚解决了我在Xcode中运行的代码中的问题。当我点击三个不同的按钮时,它运行完美并更改了标记的文本。如果没有标记文本,则会附加并标记文本。
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
是<$的名称c $ c> UITextView :
-(IBAction)first
{
[text setMarkedText:@"test" selectedRange:NSMakeRange(0, 4)];
}
-(IBAction)second
{
[text setMarkedText:@"woo" selectedRange:NSMakeRange(0,4)];
}
这里我点击了First
Here I clicked "First"
在这里我点击了Second
< img src =https://i.stack.imgur.com/Yh7il.pngalt =当点击第二个>
Here I clicked "Second"
我希望这可以帮助你和值得赏金。
I hope this helps you and is deserving of the bounty.
这篇关于UITextInput setMarkedText:selectedRange不工作? (不能!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!