UITextField set placeholder color as dark in iOS

前端 未结 8 751
误落风尘
误落风尘 2021-02-01 10:59

I have tried to set UITextField \"Placeholder\" color as dark.

NSAttributedString * search = [[NSAttributedString alloc] initWithSt         


        
相关标签:
8条回答
  • 2021-02-01 11:17

    To make it future-proof and completely customizable (color, font, size, etc.), I would just add a UILabel on top and hide/show it manually according to the UITextField contents.

    Just make sure to set the label's userInteractionEnabled to NO.

    0 讨论(0)
  • 2021-02-01 11:18

    Try this

     [textField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
    
    0 讨论(0)
  • 2021-02-01 11:21

    As suggested by Apple, subclassing UITextField and overriding - (void)drawPlaceholderInRect:(CGRect)rect is the way to go:

    - (void)drawPlaceholderInRect:(CGRect)rect { 
        UIColor *colour = [UIColor lightGrayColor]; 
        if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) 
        { // iOS7 and later 
            NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font}; 
            CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil]; 
            [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes]; } 
        else { // iOS 6 
            [colour setFill]; 
            [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment]; 
        } 
     } 
    

    Credit: http://www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6

    0 讨论(0)
  • 2021-02-01 11:26

    Overriding the drawPlaceholderInRect: method to draw our own placeholder text.

    - (void)drawPlaceholderInRect:(CGRect)rect
    {
        UIColor *colour = [UIColor darkColor];
        
        if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) {
            // iOS7 and later
            NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font};
            CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil];
            [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes];
        }
        else {
            // iOS 6
            [colour setFill];
            [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment];
        }
    }
    

    OR

    This case, likely crash if the internal variable changes in the future

    Programmatically:

      [self.MyTextField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
    

    In UIStoryBoard:

    enter image description here

    OR

    EDIT: Playing with UITextFiled delegates. its like a tricky:

    -(void)viewDidLoad{
      self.mytxtField.text = @"PlaceholderText";
      self.mytxtField.delegate = self;
       self.mytxtField.textColor = [UIColor darkGrayColor];
    
    }
    
    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
               if ([self.mytxtField.text isEqualToString: @"PlaceholderText"]) {
                 self.mytxtField.text = @"";
                 self.mytxtField.textColor = [UIColor blackColor]; 
            }
        }
    
    
    -(void)textFieldDidEndEditing:(UITextField *)textField{
          if ([self.mytxtField.text length]<1) {
                self.mytxtField.text =@"PlaceholderText";
                self.mytxtField.textColor = [UIColor darkGrayColor]; 
    
            }  
    
    0 讨论(0)
  • 2021-02-01 11:26

    For iOS8, you can take advantage of @IBDesignable & @IBInspectable.

    Here is a UITextField subclass in Swift which lets you set the placeholder color in Interface Builder and see the result instantly:

    @IBDesignable class EDTextField: UITextField {
        @IBInspectable var placeholderColor: UIColor = UIColor.whiteColor() {
            didSet {
                if let placeholder = self.placeholder {
                    let attributes = [NSForegroundColorAttributeName: placeholderColor]
                    attributedPlaceholder = NSAttributedString(string: placeholder, attributes: attributes)
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-01 11:29

    Its very simple....

    Try this to change placeholder text color..

    UIColor *color = [UIColor blackColor];
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
    
    0 讨论(0)
提交回复
热议问题