禁用 UITextField 中的闪烁光标?

发布于 2024-11-30 13:51:26 字数 239 浏览 7 评论 0原文

我已按照此处的说明进行操作,并成功设置了一个 UITextField使用 UIDatePicker 进行更新。然而 UITextField 中的光标正在闪烁,这对我来说似乎有点尴尬。

有什么解决方案可以摆脱该光标吗?

I've followed the instructions here and succesfully set up a UITextField that gets updated with a UIDatePicker. However the cursor in the UITextField is blinking, which seems quite a bit awkward to me.

Is there any solution to get rid of that cursor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

り繁华旳梦境 2024-12-07 13:51:26

我意识到这是一个老问题,但随着 iOS 7 的更新,现在可以通过执行以下操作来隐藏光标:


[[self textFieldName] setTintColor:[UIColor clearColor]];

但它仅适用于 iOS 7+。

I realise this is an old question, but with the updates to iOS 7, it is now possible to hide the cursor by doing the following:


[[self textFieldName] setTintColor:[UIColor clearColor]];

It will only work on iOS 7+ however.

冬天的雪花 2024-12-07 13:51:26

子类 UITextfield 并重写 - (CGRect)caretRectForPosition:(UITextPosition *)position 方法并返回 CGRectZero

- (CGRect)caretRectForPosition:(UITextPosition *)position {
    return CGRectZero;
}

Subclass UITextfield and Override the - (CGRect)caretRectForPosition:(UITextPosition *)position method and return CGRectZero.

- (CGRect)caretRectForPosition:(UITextPosition *)position {
    return CGRectZero;
}
暮倦 2024-12-07 13:51:26

我希望它对你有帮助。

设置光标 UIColor ->空的。

 [[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];

在斯威夫特:2.3

self.textField.valueForKey("textInputTraits")?.setValue(UIColor.clearColor() , forKey:"insertionPointColor")

I hope it will helpful to you.

Set Cursor UIColor -> Empty.

 [[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];

In Swift : 2.3

self.textField.valueForKey("textInputTraits")?.setValue(UIColor.clearColor() , forKey:"insertionPointColor")
猫九 2024-12-07 13:51:26

我无法让 jcm 的解决方案发挥作用。我最终做的是子类化 UILabel 以模仿 UITextField 的交互功能,而不包含我不想要的部分(如光标)。我在这里写了一篇关于它的博客文章:

http ://pietrorea.com/2012/07/how-to-hide-the-cursor-in-a-uitextfield/

基本上,UILabel子类需要覆盖isUserInteractionEnabled、inputView、inputViewAccessory 和 canBecomeFirstResponder。只有几行代码,而且更有意义。

I couldn't get jcm's solution to work. What I ended up doing was to subclass UILabel to mimic a UITextField's interactive functionality without the parts that I didn't want (like the cursor). I wrote a blog post about it here:

http://pietrorea.com/2012/07/how-to-hide-the-cursor-in-a-uitextfield/

Basically, the UILabel subclass needs to overwrite isUserInteractionEnabled, inputView, inputViewAccessory and canBecomeFirstResponder. It's only a few lines of code and it makes more sense.

℉絮湮 2024-12-07 13:51:26

完全愚蠢的黑客,但如果您在 Interface Builder 属性检查器的 UIView 部分中设置文本字段的色调以匹配背景颜色,则光标将显示为不可见:

Totally silly hack, but if you set the text field's tint color in the UIView section of the Interface Builder property inspector to match the background color, the cursor will appear invisible:

虐人心 2024-12-07 13:51:26

我所做的是将另一个 UITextField 覆盖在我想要隐藏光标的 UITextField 之上。然后在委托方法 textFieldShouldBeginEditing 中,我将另一个 textField 设置为第一响应者并返回 NO。

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag==TAG_OF_DUMMY_TEXTFIELD) {
        [otherField becomeFirstResponder];
        return NO;
    }
    return YES;
}

然后在日期选择器调用的方法中:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@YOUR_DATE_FORMAT];
dummyField.text = [dateFormatter stringFromDate:datePicker.date];

在 Interface Builder 中,otherField(具有 datePicker 输入视图的字段)位于 dummyField(隐藏光标的字段)后面。

What I did was to overlay another UITextField on top of the one whose cursor I wanted to hide. Then in the delegate method textFieldShouldBeginEditing I set the other textField to become first responder and returned NO.

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag==TAG_OF_DUMMY_TEXTFIELD) {
        [otherField becomeFirstResponder];
        return NO;
    }
    return YES;
}

And then in the method the date picker calls:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@YOUR_DATE_FORMAT];
dummyField.text = [dateFormatter stringFromDate:datePicker.date];

In Interface Builder otherField (the one with the datePicker input view) is behind dummyField (the one that hides the cursor).

岁吢 2024-12-07 13:51:26

这不是最好的解决方案,但您也可以将色调颜色的不透明度设置为 0%。

输入图片此处描述

Not the best solution, but you could also set the Opacity of the tint color to 0%.

enter image description here

无人接听 2024-12-07 13:51:26

我发现这个解决方案是最容易实现的。

确保在 .h 文件中定义 UITextFieldDelegate:

.... UIViewController <UITextFieldDelegate>

在 .m 文件中,将其添加到为日期选择器调用的方法中:

[yourTextField resignFirstResponder];

这将防止文本字段闪烁。

I found this solution to be the easiest to implement.

Make sure you define UITextFieldDelegate in your .h file:

.... UIViewController <UITextFieldDelegate>

In your .m file, add this to the method you call fo the date picker:

[yourTextField resignFirstResponder];

This will prevent the textfield from blinking.

羁绊已千年 2024-12-07 13:51:26

巴拉吉的方法确实有效。

我也多次使用过这样的KVC解决方案。
尽管它似乎没有记录,但它确实有效。坦白说,你没有使用任何
这里的私有方法 - 只有键值编码是合法的。

它与 [addNewCategoryTextField textInputTraits] 截然不同。

PS 昨天我的新应用程序出现在 AppStore 上,这种方法没有任何问题。当我使用 KVC 更改一些只读属性(如 navigatonBar)或私有 ivars 时,这并不是第一个情况。

Balaji's approach does work.

I also used such KVC solutions many times.
Despite it seems to be undocumented, but it works. Frankly, you don't use any
private methods here - only Key-Value Coding which is legal.

It is drastically different from [addNewCategoryTextField textInputTraits].

P.S. Yesterday my new app appeared at AppStore without any problems with this approach. And it is not the first case when I use KVC in changing some read-only properties (like navigatonBar) or private ivars.

ぃ双果 2024-12-07 13:51:26

您可以通过关联对象将 BOOLcursorless 属性添加到类别中的 UITextField

@interface UITextField (Cursorless)

@property (nonatomic, assign) BOOL cursorless;

@end

然后使用方法调配来调配 caretRectForPosition: 和使用 cursorlessCGRectZero 及其默认值之间切换的方法。

这通过插入类别产生了一个简单的界面。以下文件对此进行了演示。

的好处

只需将它们放入即可享受这个简单界面UITextField 类别 :
https://github.com/rexmas/RexDK/blob /master/RexDK/UI/UITextField%2BRXCursorless.h
https://github.com/rexmas/RexDK/blob /master/RexDK/UI/UITextField%2BRXCursorless.m

方法调整:
https://github.com/rexmas/RexDK/blob /master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.h
https://github.com/rexmas/RexDK/blob /master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.m

You can add a BOOL cursorless property to UITextField in a category via associated objects.

@interface UITextField (Cursorless)

@property (nonatomic, assign) BOOL cursorless;

@end

Then use method swizzling to swizzle caretRectForPosition: with a method that toggles between CGRectZero and its default value using cursorless.

This leads to a simple interface via a drop-in category. This is demonstrated in the following files.

Simply drop them in and get the benefit of this simple interface

UITextField category:
https://github.com/rexmas/RexDK/blob/master/RexDK/UI/UITextField%2BRXCursorless.h
https://github.com/rexmas/RexDK/blob/master/RexDK/UI/UITextField%2BRXCursorless.m

Method Swizzling:
https://github.com/rexmas/RexDK/blob/master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.h
https://github.com/rexmas/RexDK/blob/master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.m

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文