Andrew
Andrew

Reputation: 16051

How do i change the color of the text entry indicator?

It's seen in UITextView and UITextField - the flashing blue line which shows you where anything you type will be typed. I've seen some apps change the color of this and wasn't sure how to do that. How do I change it's color?

Upvotes: -1

Views: 6117

Answers (6)

Matthew Ferguson
Matthew Ferguson

Reputation: 95

For iOS 10 SDK , only the indicator, and Objective-C (someday will be missed) my working solution is :

for (UIView *subView in self.sBar.subviews){
    for (UIView *ndLevelSubView in subView.subviews){

        if ([ndLevelSubView isKindOfClass:[UITextField class]])
        {
            UITextField * sbTextField = (UITextField *)ndLevelSubView;
            [[sbTextField valueForKey:@"textInputTraits"] setValue:[UIColor n2pMainPurpleColor] forKey:@"insertionPointColor"];
            break;
        }

    }

}

Upvotes: 0

aToz
aToz

Reputation: 2464

Change the tint color, (iOS 7)

Change the tint color in the nib

or do it programmatically

myTextField.tintColor = [UIColor blackColor];

Upvotes: -1

malex
malex

Reputation: 10096

Try this:

[[textField valueForKey:@"textInputTraits"] setValue:CUSTOM_COLOR forKey:@"insertionPointColor"];

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.

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.

Upvotes: 1

In IOS 7 you can simple set tintColor, it change color for all active elements in view and subviews, includes cursor color:

_textField.tintColor = [UIColor redColor];

Upvotes: 8

KubajzT
KubajzT

Reputation: 34

[[textField textInputTraits] setValue:[UIColor redColor] forKey:@"insertionPointColor"];

Upvotes: 2

akashivskyy
akashivskyy

Reputation: 45210

There is no documented way to change the pointer's color. Hovewer, there is one undocumented textField's property, namely textInputTraits, which is an UITextInputTraits instance.

You can try this code to get this working, but your app may be rejected in review process.

[[textField textInputTraits] setValue:[UIColor redColor] forKey:@"insertionPointColor"];

Taken from https://stackoverflow.com/a/4273440/736649

Upvotes: 0

Related Questions