Working
with UITextFields is generally quite easy, you can customise most attributes
apart from the placeholder text, while you can obviously change the text you
can't change the colour of the text or it's position. This
was a problem we had on a recent project, our design required us to have
UITextFields with black backgrounds, the placeholder text colour is dark grey
which is almost invisible in our app and Apple provides no easy way to change
this.
We solved this by subclassing UITextField and overriding drawPlaceholderInRect:
method to draw our own placeholder text. This is fairly straight forward in
iOS6 but did not work as expected in iOS 7.
- (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.fontlineBreakMode:NSLineBreakByTruncatingTailalignment:self.textAlignment]:}}
No comments:
Post a Comment