placeholder垂直居中
解决光标居中、字体不居中问题。
问题如图:
可以看出光标已经居中,但是文字还是偏上。
常规方法(下面代码)是没有效果的。
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter
解决:
新建一个类,需要继承UITextField:
#import <UIKit/UIKit.h>
@interface PlaceholderVerticalAlignment : UITextField
@end
问题:为什么减0.5
#import "PlaceholderVerticalAlignment.h"
@implementation PlaceholderVerticalAlignment
- (void)drawPlaceholderInRect:(CGRect)rect {
[super drawPlaceholderInRect:CGRectMake(0, <span style="color:#ff0000;">self.bounds.size.height * 0.5 - 0.5</span> , 0, 0)];
}
@end
用新类创建textfield
[[PlaceholderVerticalAlignment alloc] initWithFrame:CGRectMake()