White Text in UITextField = Invisible text in iPho

2019-01-23 10:31发布

问题:

If I have white text in my UITextField, the selection window (when selecting text) is invisible because the background on the little window is also white.

Any way to fix this?

回答1:

Sure! The background color of the loupe always matches the backgroundColor property of the text field. (But not the background property.) Example:

textField.textColor = [UIColor whiteColor];
textField.backgroundColor = [UIColor blackColor];

If your text field absolutely requires a transparent background, you'll have to fake it—by using a background image containing the graphics underneath the text field. You may do it manually—by taking a screenshot of your interface and cropping it—or programmatically, like this:

#import <QuartzCore/CALayer.h>
...
// `view` contains the graphics underneath the text field
UIGraphicsBeginImageContext(textField.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint origin = [textField convertPoint:textField.bounds.origin toView:view];
CGContextTranslateCTM(context, -origin.x, -origin.y);
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
textField.background = image;

Since the background is drawn on top of the background color, the text field will appear transparent, and you'll be able to use any background color you want for the loupe.



回答2:

I'm afraid not. The problem's existed since the earliest days of the iPhone OS—white text appears as white-on-white in the cursor-positioning loupe as well. If this is a serious problem for your app, your only real options are to change the text color or to file a radar feature request with Apple.



回答3:

Glass takes color from textView.backgroundColor. So I made some dirty hack that works great:

@interface FakeBgTextView : UITextView {
    UIColor *_fakeBackgroundColor;
}

- (UIColor *)backgroundColor;
- (void)setFakeBackgroundColor:(UIColor *)color;
@end

@implementation FakeBgTextView
...
- (UIColor *)backgroundColor {
    return _fakeBackgroundColor;
}

- (void)setFakeBackgroundColor:(UIColor *)color {
    [_fakeBackgroundColor release];
    _fakeBackgroundColor = [color retain];
}
...
@end


回答4:

I know this is a little old, but in iOS5, it appears to be a simulator-only issue. It will render correctly on the device.



回答5:

One solution that I've employed for this issue is to change the text color to a that will show up in the loupe (but may not look as good in the overall view) while editing and then changing it back to the better display color when finished editing.

It behaves as if you were highlighting the text of the field from a visual standpoint and allows you to use your preferred color for the display of the entered data when not editing.

Set the color to a highlight color that will have enough contrast in the loupe on didBeginEditing, then change it back on didEndEditing.

Just one other possible approach and one I've used in a couple of apps.

eg.

- (void)textFieldDidBeginEditing:(UITextField *)textField {
  textField.textColor = [UIColor colorWithRed:116.0/255.0 green:160.0/255.0 blue:246.0/255.0 alpha:1.0];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  textField.textColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0];
}