31

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Tobias Frischholz
  • 331
  • 1
  • 3
  • 4

10 Answers10

48

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.

Jamie Chapman
  • 4,229
  • 5
  • 29
  • 47
39

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

- (CGRect)caretRectForPosition:(UITextPosition *)position {
    return CGRectZero;
}
Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75
  • It's not working with iOS 4.3. UITextInput Protocol adapted only iOS 5 and above. – Dinesh Raja Mar 29 '13 at 06:30
  • 4
    This should be the selected answer. It's to the point and addresses the specific part we want to hide -- namely the caret. This is the best solution out of all of them. Thanks. – Art Geigel Jun 11 '13 at 05:10
31

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")
Bhagabata
  • 463
  • 4
  • 12
Balaji G
  • 673
  • 1
  • 8
  • 21
6

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.

pietrorea
  • 841
  • 6
  • 14
  • i recently added your PRLabel class in my project.. but i dont know what i did wrong, it is seriously messing upp with the tableview index ... i think when the picker view is shown, the tableview is scrolled but visually not updated ... i have a PRLabel in section 1 .. i touch it , picker view shows then when i touch a cell in section 0 it is interpreting as section 1 .. – raw3d Apr 18 '13 at 11:55
2

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:

brandonscript
  • 68,675
  • 32
  • 163
  • 220
1

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

enter image description here

1

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).

jcm
  • 1,781
  • 1
  • 15
  • 27
0

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.

nimrod
  • 5,595
  • 29
  • 85
  • 149
0

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.

malex
  • 9,874
  • 3
  • 56
  • 77
-1

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

Awesome-o
  • 2,002
  • 1
  • 26
  • 38