ios 隐藏 UITextField 的光标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3699727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide the cursor of an UITextField
提问by emenegro
I am using a UITextField
with a UIPickerView
for its inputView
, so that when the user taps the text field, a picker is summoned for them to select an option from.
我将 aUITextField
与 aUIPickerView
一起使用inputView
,以便当用户点击文本字段时,会调用一个选择器让他们从中选择一个选项。
Nearly everything works, but I have one problem: the cursor still flashes in the text field when it is active, which is ugly and inappropriate, since the user is not expected to type into the field and is not presented with a keyboard. I know I could hackily solve this by setting editing
to NO
on the text field and tracking touches on it, or by replacing it with a custom-styled button, and summoning the picker via code. However, I want to use the UITextFieldDelegate
methods for all the event handling on the text field and hacks such as replacing the text field with a button do not permit this approach.
几乎一切正常,但我有一个问题:当文本字段处于活动状态时,光标仍会在文本字段中闪烁,这很丑陋且不合适,因为不希望用户在该字段中键入内容并且没有显示键盘。我知道我可以通过在文本字段上设置editing
为NO
并跟踪对其的触摸,或者用自定义样式的按钮替换它,并通过代码召唤选择器来解决这个问题。但是,我想对UITextFieldDelegate
文本字段上的所有事件处理使用这些方法,并且诸如用按钮替换文本字段之类的技巧不允许这种方法。
How can I simply hide the cursor on the UITextField
instead?
我怎样才能简单地隐藏光标UITextField
?
回答by Joseph Chiu
Simply subclass UITextField and override caretRectForPosition
只需继承 UITextField 并覆盖 caretRectForPosition
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return CGRectZero;
}
回答by jamone
As of iOS 7 you can now just set the tintColor = [UIColor clearColor]
on the textField and the caret will disappear.
从 iOS 7 开始,您现在只需tintColor = [UIColor clearColor]
在 textField 上设置,插入符号就会消失。
回答by oldman
You can just clear the textfield's tintColor
您可以清除文本字段的 tintColor
self.textField.tintColor = [UIColor clearColor];
Swift 3.0
斯威夫特 3.0
self.textField.tintColor = .clear
回答by Nat
You might also want to stop the user from selecting, copying or pasting any text so that the only text input comes from the picker view.
您可能还希望阻止用户选择、复制或粘贴任何文本,以便唯一的文本输入来自选择器视图。
- (CGRect) caretRectForPosition:(UITextPosition*) position
{
return CGRectZero;
}
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:) || action == @selector(selectAll:) || action == @selector(paste:))
{
returnNO;
}
return [super canPerformAction:action withSender:sender];
}
http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/
http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/
回答by ma11hew28
Check out the propertyselectedTextRange
of the protocolUITextInput
, to which the classUITextField
conforms. Few! That's a lesson in object-oriented programing right there.
退房财产selectedTextRange
的的协议UITextInput
,对此类UITextField
符合。很少!这就是面向对象编程的一课。
Hide Caret
隐藏插入符号
To hide the caret, nil out the text field's selected text range.
要隐藏插入符号,请清除文本字段的选定文本范围。
textField.selectedTextRange = nil; // hides caret
Unhide Caret
取消隐藏插入符号
Here are two ways to unhide the caret.
这里有两种取消隐藏插入符号的方法。
Set the text field's selected text range to the end of the document.
UITextPosition *end = textField.endOfDocument; textField.selectedTextRange = [textField textRangeFromPosition:end toPosition:end];
To keep the caret in the same spot, first, store the text field's selected text range to an instance variable.
_textFieldSelectedTextRange = textField.selectedTextRange; textField.selectedTextRange = nil; // hides caret
Then, when you want to unhide the caret, simply set the text field's selected text range back to what it was originally:
textField.selectedTextRange = _textFieldSelectedTextRange; _textFieldLastSelectedTextRange = nil;
将文本字段的选定文本范围设置为文档末尾。
UITextPosition *end = textField.endOfDocument; textField.selectedTextRange = [textField textRangeFromPosition:end toPosition:end];
要将插入符号保持在同一位置,首先,将文本字段的选定文本范围存储到实例变量中。
_textFieldSelectedTextRange = textField.selectedTextRange; textField.selectedTextRange = nil; // hides caret
然后,当您想取消隐藏插入符号时,只需将文本字段的选定文本范围设置回原来的值:
textField.selectedTextRange = _textFieldSelectedTextRange; _textFieldLastSelectedTextRange = nil;
回答by Robert H?glund
Answer provided by the OP, copied from the question body to help clean up the ever growing tail of unanswered questions.
由 OP 提供的答案,从问题正文中复制,以帮助清理不断增长的未回答问题的尾部。
I found another solution: subclass UIButton
and override these methods
我找到了另一个解决方案:子类UIButton
并覆盖这些方法
- (UIView *)inputView {
return inputView_;
}
- (void)setInputView:(UIView *)anInputView {
if (inputView_ != anInputView) {
[inputView_ release];
inputView_ = [anInputView retain];
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
Now the button, as a UIResponder
, have a similar behavior than UITextField
and an implementation pretty straightforward.
现在,作为 a 的按钮UIResponder
具有类似的行为,UITextField
并且实现非常简单。
回答by Rom.
Swift 3 version of Net's post
Swift 3 版本的 Net 帖子
override func caretRect(for position: UITextPosition) -> CGRect {
return .zero
}
override func selectionRects(for range: UITextRange) -> [Any] {
return []
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
回答by Ahmad Al-Attal
set the tintColor to Clear Color
将 tintColor 设置为清除颜色
textfield.tintColor = [UIColor clearColor];
and you can also set from the interface builder
你也可以从界面构建器中设置
回答by G?ktu? Aral
If you want to hide cursor, you can easily use this! It worked for me..
如果你想隐藏光标,你可以很容易地使用它!它对我有用..
[[textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"]
回答by Robert H?glund
Answer provided by the OP, copied from the question body to help clean up the ever growing tail of unanswered questions.
由 OP 提供的答案,从问题正文中复制,以帮助清理不断增长的未回答问题的尾部。
I think I have the correct solution but If it can be improved will be welcome :) Well, I made a subclass of UITextField and overriden the method that returns the CGRect for the bounds
我想我有正确的解决方案,但如果可以改进将受到欢迎:) 好吧,我创建了 UITextField 的子类并覆盖了返回边界的 CGRect 的方法
-(CGRect)textRectForBounds:(CGRect)bounds {
return CGRectZero;
}
The problem? The text doesn't show because the rect is zero. But I added an UILabel as a subview of the control and overridden the setText method so, as we enter a text as usual, the text field text is nil and is the label which shows the text
问题?文本不显示,因为矩形为零。但是我添加了一个 UILabel 作为控件的子视图并覆盖了 setText 方法,因此,当我们像往常一样输入文本时,文本字段文本为零,并且是显示文本的标签
- (void)setText:(NSString *)aText {
[super setText:nil];
if (aText == nil) {
textLabel_.text = nil;
}
if (![aText isEqualToString:@""]) {
textLabel_.text = aText;
}
}
With this the thing works as expected. Have you know any way to improve it?
有了这个,事情就按预期工作了。你知道有什么方法可以改善吗?