UIKit文本输入组件,例如UITextView并UITextField具有inputView添加自定义键盘的属性.我有两个问题与此有关.
如果键盘当前可见且属性设置为新的输入视图,则不会发生任何事情.重新启动并重新获得第一响应者状态会刷新输入并显示新视图.这是最好的方法吗?如果是这样,它可以回答我更大的问题:
是否可以为两个输入视图之间的过渡设置动画?
有没有办法(疯狂的黑客欢迎)将当前行作为UITextView的字符串?这将包括自动换行等.例如,在这种情况下:

该方法将返回"堆栈溢出.是不是很好?我",因为这是基于游标的当前行.
它也可以根据光标的位置返回"这是我为之做的测试"或"这么做".我尝试过使用UITextView方法和UITextInput协议.
编辑:
这是我试图使用的代码.我需要找到字符串的原因是为了得到它的长度,所以这就是为什么你会看到基于UI的代码.
NSRange location = self.textView.selectedRange;
NSString *searchString = [self.textView.text substringWithRange:NSMakeRange(0, location)];
CGSize currentStringDimensions = [searchString sizeWithFont:self.textView.font constrainedToSize:CGSizeMake(self.textView.frame.size.width, self.textView.frame.size.height) lineBreakMode:NSLineBreakByWordWrapping];
float numberOfRows = (currentStringDimensions.width/(self.textView.frame.size.width));
float left = (float)(numberOfRows - (int)numberOfRows) * (self.textView.frame.size.width);
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.我认为它可能与包装的单词或不同大小的字符有关,但左边的值不一致或在第一行后关闭.
好的,所以我知道Apple的UITextInput协议需要两个UITextRange属性selectedTextRange,markedTextRange并且文档说用户yatta yatta是一个未确认的文本范围selectedTextRange的子markedTextRange范围.对于我应该如何以不同方式实现两个文本范围,这仍然没有让我明白一些事情.有人能直观地向我解释selectedTextRange和之间的区别markedTextRange吗?我知道,当length的selectedTextRange是0它表明在一个闪烁的插入符selectedTextRange的location.但到底是什么"标记文字"?我在iOS中只看到以下文本视图:
selectedTextRange指示"标记文本"中的子范围markedTextRange,即readwrite有关?
这似乎与iOS的8,9,7 Xcode的特性beginningOfDocument和endOfDocument的UItextField总是nil你做whathever.更糟糕的是,它们不是UITextPosition?Swift 2 中的可选类型(),而是它们的类型UITextPosition- 并且仍然具有nil价值.Debuger称之为<uninitialized>代替,nil但它的行为与它相同.要重现,请将以下代码添加到任何UIViewController:
override func viewDidAppear(animated: Bool) {
let textField = UITextField()
textField.text = "Hello"
view.addSubview(textField)
let position: UITextPosition? = textField.beginningOfDocument //beginningOfDocument is of type UITextPosition, not optional
//following line should always succeed
let positionUnwrapped = position! //fatal error: unexpectedly found nil while unwrapping an Optional value
}
Run Code Online (Sandbox Code Playgroud)
这真的是一个(巨大的)错误还是我错过了什么?是否有解决方法,也许有一些步骤来解决问题?
编辑:请注意,这里没有回答这个问题.建议的修复程序不适用于我的示例代码:
我想要更改丰富的UITextView(iOS 6)的部分或全部属性文本,并允许用户撤消更改.
在阅读NSUndoManager文档后,我尝试了第一种方式:
“Simple undo” based on a simple selector with a single object argument.
Run Code Online (Sandbox Code Playgroud)
我期望撤消操作就像这样简单:
声明这个方法:
- (void)setAttributedStringToTextView:(NSAttributedString *)newAttributedString {
NSAttributedString *currentAttributedString = self.textView.attributedText;
if (! [currentAttributedString isEqualToAttributedString:newAttributedString]) {
[self.textView.undoManager registerUndoWithTarget:self
selector:@selector(setAttributedStringToTextView:)
object:currentAttributedString];
[self.textView.undoManager setActionName:@"Attributed string change"];
[self.textView setAttributedText:newAttributedString];
}
}
Run Code Online (Sandbox Code Playgroud)
通过调用以下命令更改我的UITextView中的文本:
[self setAttributedStringToTextView:mutableAttributedString];
Run Code Online (Sandbox Code Playgroud)
但在这之后,NSUndoManager表示它无法撤消.
NSLog(@"Can undo: %d", [self.textView.undoManager canUndo]);
// Prints: "Can undo: 0"
Run Code Online (Sandbox Code Playgroud)
所以我尝试了第二种方式:
“Invocation-based undo” which uses an NSInvocation object.
Run Code Online (Sandbox Code Playgroud)
声明:
- (void)setMyTextViewAttributedString:(NSAttributedString *)newAttributedString {
NSAttributedString *currentAttributedString = [self.textView attributedText];
if (! [currentAttributedString isEqualToAttributedString:newAttributedString]) …Run Code Online (Sandbox Code Playgroud) uitextview nsattributedstring nsundomanager uitextinput ios6
我正在实现一个自定义文本输入视图,它采用UITextInput协议,在使用时UITextView,双击一个单词使单词被选中,我想知道如何UITextInput使用它的标记化器来对字符串进行标记化,到现在为止我没有看到有没有分配的任何区别通过覆盖[UITextInput -tokenizer]方法获取UITextInput的标记化器.
我可以使用以下代码成功地将轻击手势添加到UITextView的一部分:
UITextPosition *pos = textView.endOfDocument;// textView ~ UITextView
for (int i=0;i<words*2-1;i++){// *2 since UITextGranularityWord considers a whitespace to be a word
UITextPosition *pos2 = [textView.tokenizer positionFromPosition:pos toBoundary:UITextGranularityWord inDirection:UITextLayoutDirectionLeft];
UITextRange *range = [textView textRangeFromPosition:pos toPosition:pos2];
CGRect resultFrame = [textView firstRectForRange:(UITextRange *)range ];
UIView* tapViewOnText = [[UIView alloc] initWithFrame:resultFrame];
[tapViewOnText addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(targetRoutine)]];
tapViewOnText.tag = 125;
[textView addSubview:tapViewOnText];
pos=pos2;
}
Run Code Online (Sandbox Code Playgroud)
我希望在a中模仿相同的行为UILabel.问题是,UITextInputTokenizer(使用来标记个人的话)的宣布UITextInput.h,只UITextView与UITextField符合UITextInput.h; UILabel才不是.这有解决方法吗?
我有一个基于自定义UITextInput的文本编辑器。除了通过标记文本进行多阶段输入之外,它工作得很好。
我的标记区域渲染正确,并且插入了标记文本,但键盘上方的候选列表是空白的。
例如,下面是日语(假名)键盘,显示了有关标准 UITextView 的建议:

这是我的自定义编辑器,显示相同的标记文本:

我花了几天时间调试这个问题,发现原因是该方法UIKeyboardImpl返回私有类NOdelegateSupportsCorrectionUI
如果我在类别中覆盖此方法UIKeyboardImpl并返回YES,则多级输入建议会正确显示在我的文本编辑器中。然而,这并没有解决问题的根本原因(而且它不可用)。
我还仔细研究了 Apple 的 SimpleTextInput 示例代码。这实现了一个基本的核心文本编辑器。SimpleTextInput 正确显示多级输入建议,但是我似乎找不到其实现中的任何差异,UITextInput导致它工作而我的崩溃。
(事实上,我无法“破坏”SimpleTextInput 示例显示多阶段输入的能力。这让我认为我对实现的关注UITextInput是错误的。而且这完全是另一回事。)
我们有一个使用UITextInput,UIKeyInput和UITextInputTraits协议实现的文本输入视图。
基本的文本输入和删除工作正常,通过insertText:和deleteBackward。但是,这些方法不会收到 QuickType 建议,而且我无法找到有关 QuickType 如何与这些协议交互的文档。
我应该如何实现这些协议来接收 QuickType 输入?
uitextinput ×10
ios ×9
uitextview ×4
objective-c ×3
uitextfield ×3
iphone ×2
cocoa-touch ×1
ios6 ×1
protocols ×1
quicktype ×1
swift ×1
uikeyboard ×1
uikeyinput ×1
uikit ×1
uilabel ×1
uitextrange ×1