Centering Placeholder Text in Subclass of UITextField

我与影子孤独终老i 提交于 2019-12-05 10:24:42

Here you go buddy

subclassing UITextField will do the work:

// CustomTextField.h
@interface CustomTextField : UITextField {
}
@end

override the methods:

@implementation
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    return CGRectMake(x,y,width,height);//Return your desired x,y position and width,height
}

- (void)drawPlaceholderInRect:(CGRect)rect {
    //draw place holder.
 [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:12]];

}
@end

i think this will work (tested)

- (void) drawPlaceholderInRect:(CGRect)rect {

      [[UIColor redColor] setFill];
      [self.placeholder drawInRect:rect
                          withFont:self.font
                     lineBreakMode:UILineBreakModeTailTruncation
                         alignment:self.textAlignment];
    }

https://github.com/mschulkind/cordova-true-native-ios/blob/master/Classes/UITextFieldPlugin.m

You can do this in UITextfield subclass

- (void)drawPlaceholderInRect:(CGRect)rect {

[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8] setFill];

[[self placeholder] drawInRect:rect withFont:[UIFont boldSystemFontOfSize:16.0] lineBreakMode:UILineBreakModeTailTruncation alignment:NSTextAlignmentCenter];

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!