2013-05-17 40 views
7

Làm thế nào để thay đổi màu của placeholder văn bản tôi đặt trong UITextField kiểm soát của tôi, để làm cho nó đen?Làm thế nào Thay đổi văn bản giữ chỗ màu - iOS

+2

Phía trên tất cả các câu trả lời, từ iOS 6, có thuộc tính 'NSAttributedString * được phân bổPlaceholder', ví dụ: 'textField.attributedPlaceholder = [[Cấp phát NSAttributedString] initWithString: thuộc tính tiêu đề: @ {NSFontAttributeName: textField.font, NSForegroundColorAttributeName: textField.textColor}];' – Hemang

Trả lời

7

Bạn có thể ghi đè lên drawPlaceholderInRect:(CGRect)rect như vậy để tự làm cho văn bản giữ chỗ:

- (void) drawPlaceholderInRect:(CGRect)rect { 
[[UIColor blueColor] setFill]; 
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]]; 

}

1

Bạn có thể sử dụng để thay đổi màu sắc văn bản giữ chỗ.

- (void) drawPlaceholderInRect:(CGRect)rect { 
    [[UIColor blueColor] setFill]; 
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]]; 
} 
4

Bạn sẽ phải phân lớp lớp UITextField và ghi đè phương thức sau.

- (void) drawPlaceholderInRect:(CGRect)rect 
{ 
    [[UIColor blackColor] setFill]; 

    [[self placeholder] drawInRect:rect withFont:[UIFont italicSystemFontOfSize:17] lineBreakMode:UILineBreakModeCharacterWrap alignment:UITextAlignmentLeft]; 

} 
1

Sử dụng mã sau đây. sẽ giúp bạn.

- (void) drawPlaceholderInRect:(CGRect)rect 
    { 
    [[UIColor redColor] setFill]; 
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]]; 
    } 
45

sử dụng KVC

[yourtextfield setValue:[UIColor colorWithRed:120.0/255.0 green:116.0/255.0 blue:115.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"]; 

bạn có thể thiết lập màu sắc của riêng bạn để giữ chỗ

+0

Có ai đã gửi ứng dụng lên App Store bằng ứng dụng này không? Dường như với tôi như truy cập '_placeholder' có thể được tính là phương thức riêng ... – samvermette

+0

@samvermette có chúng tôi đã gửi một ứng dụng có mã này, nó sẽ không tham gia vào phương thức riêng tư – Vinodh

+1

Xem http://stackoverflow.com/a/3396065/309046 trả lời – satish

13

Đây là một cách tốt nhất để thay đổi màu sắc giữ chỗ của bạn qua KVO ...

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

Tôi hy vọng nó sẽ giúp bạn thay đổi màu giữ chỗ. Cảm ơn

+0

Bạn đang sử dụng nó trong sản xuất? Tôi muốn truy cập một tài sản riêng. – Geri

+0

nó hoạt động tốt. Cảm ơn –

Các vấn đề liên quan