eme*_*gro 122 cocoa-touch uipickerview uitextfield ios
我使用的是UITextField用UIPickerView它的inputView,这样当用户点击文本字段,一个选择器被召唤为他们选择一个选项.
几乎所有的作品,但我有一个问题:光标仍然闪烁的文本字段时,它是积极的,这是丑陋和不恰当的,因为用户不希望被输入到现场,并没有跟键盘.我知道我可以通过设置hackily解决这个问题editing,以NO文本字段,并对其进行监视触摸,或通过使用自定义风格的按钮替换它,召唤通过代码选择器.不过,我想使用的UITextFieldDelegate方法对所有的事件处理上的文本字段和黑客如用按钮替换文本字段不允许这种做法.
我怎样才能简单地将光标隐藏在UITextField?
小智 264
简单地将UITextField子类化并覆盖caretRectForPosition
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return CGRectZero;
}
Run Code Online (Sandbox Code Playgroud)
jam*_*one 148
从iOS 7开始,您现在可以tintColor = [UIColor clearColor]在textField上设置,插入符号将消失.
old*_*man 87
你可以清除文本字段的tintColor
self.textField.tintColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)
Swift 3.0
self.textField.tintColor = .clear
Run Code Online (Sandbox Code Playgroud)

小智 20
您可能还希望阻止用户选择,复制或粘贴任何文本,以便唯一的文本输入来自选择器视图.
- (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];
}
Run Code Online (Sandbox Code Playgroud)
http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/
ma1*_*w28 14
退房财产 selectedTextRange的的协议 UITextInput,对此类 UITextField符合.很少!这是面向对象编程的一个教训.
隐藏Caret
要隐藏插入符号,请忽略文本字段的选定文本范围.
textField.selectedTextRange = nil; // hides caret
Run Code Online (Sandbox Code Playgroud)
取消隐藏Caret
以下是两种取消隐藏插入符号的方法.
将文本字段的选定文本范围设置为文档的末尾.
UITextPosition *end = textField.endOfDocument;
textField.selectedTextRange = [textField textRangeFromPosition:end
toPosition:end];
Run Code Online (Sandbox Code Playgroud)要将插入符保持在同一位置,首先,将文本字段的选定文本范围存储到实例变量中.
_textFieldSelectedTextRange = textField.selectedTextRange;
textField.selectedTextRange = nil; // hides caret
Run Code Online (Sandbox Code Playgroud)
然后,当您想要取消隐藏插入符时,只需将文本字段的选定文本范围设置回原来的范围:
textField.selectedTextRange = _textFieldSelectedTextRange;
_textFieldLastSelectedTextRange = nil;
Run Code Online (Sandbox Code Playgroud)Rob*_*und 10
OP提供的答案,从问题机构复制,以帮助清理不断增长的未解决问题的尾部.
我找到了另一个解决方案:子类UIButton并覆盖这些方法
- (UIView *)inputView {
return inputView_;
}
- (void)setInputView:(UIView *)anInputView {
if (inputView_ != anInputView) {
[inputView_ release];
inputView_ = [anInputView retain];
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
Run Code Online (Sandbox Code Playgroud)
现在,按钮作为一个UIResponder,具有类似的行为,UITextField而且实现非常简单.
Net的Swift 3版本
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57564 次 |
| 最近记录: |