您的位置:首页 > 移动开发 > Objective-C

placeholder垂直居中(解决光标居中、文字不居中问题)

2016-05-04 14:42 861 查看

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息