Rudolf Adamkovič
Rudolf Adamkovič

Reputation: 31486

UITextField selection background color

Is it possible to get UITextField's selection background color without using private APIs?

Upvotes: 2

Views: 1673

Answers (4)

Laszlo
Laszlo

Reputation: 2778

Based on phix23's answer you can make a UITextField subclass and overwrite the selectionHighlightColor property. Like:

- (UIColor *)selectionHighlightColor{
    return [UIColor clearColor];
}

Tested in iOS 9.3, also in production

Upvotes: 1

Tommy Devoy
Tommy Devoy

Reputation: 13549

There isn't an actual method available, but I just wrote one that does the trick using a UILongPressGestureRecognizer. It's unclear whether you want to set the selected background color or just read it? But you can try this out and maybe it will give you some ideas:

-(void)viewDidLoad
{
     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(selectionDetected:)];
    longPress.minimumPressDuration=0.3;//you could vary this value for whatever you prefer
    [self.yourTextField addGestureRecognizer:longPress];
    [longPress release];
}

-(void)selectionDetected:(UILongPressGestureRecognizer*)longPress
{    
    if(longPress.state==1)
    {
        self.yourTextField.backgroundColor = [UIColor blueColor];//selected
    }
    else if(longPress.state==3)
    {
        self.yourTextField.backgroundColor= [UIColor orangeColor];//no longer selected..put back to original color
    }
}

Until the user lifts their finger (even if they move around the screen), it will remain the "selected" color. Not sure if this is really what you're looking for but maybe it'll help somebody.

Upvotes: 1

Jasmin Mistry
Jasmin Mistry

Reputation: 1476

NO Apple is not providing such apis.

Upvotes: 0

Felix
Felix

Reputation: 35384

No, there is no public API for that.

You can try it with private API though:

UITextField has an instance method named selectionHighlightColor and UIColor has a class method with the same name. Both return the same color which should be [UIColor colorWithRed:0 green:.33 blue:.65 alpha:.2].

Upvotes: 2

Related Questions