第一種方法實現:code
一、自定義ZZPlaceholderColorTextField集成於UITextFieldci
二、it
- (void)drawRect:(CGRect)rect { self.tintColor = [UIColor whiteColor]; NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithCapacity:1]; attrs[NSForegroundColorAttributeName] = [UIColor whiteColor]; self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attrs]; // Drawing code }
第二種方法實現:io
二、table
- (void)drawRect:(CGRect)rect { self.tintColor = [UIColor whiteColor]; // Drawing code } #pragma mark 重寫drawPlaceholderInRect方法 - (void)drawPlaceholderInRect:(CGRect)rect { NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSForegroundColorAttributeName] = [UIColor whiteColor]; attrs[NSFontAttributeName] = self.font; /* //畫出佔位符 CGRect placeholderRect; placeholderRect.size.width = rect.size.width; placeholderRect.size.height = rect.size.height; placeholderRect.origin.x = 0; placeholderRect.origin.y = (rect.size.height - self.font.lineHeight) * 0.5; [self.placeholder drawInRect:placeholderRect withAttributes:attrs]; */ //或者 CGPoint placeholderPoint = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5); [self.placeholder drawAtPoint:placeholderPoint withAttributes:attrs]; }