UITextField设置leftView的Insets
发布日期:2022-03-12 04:49:21 浏览次数:25 分类:技术文章

本文共 1267 字,大约阅读时间需要 4 分钟。

Insets就是css中的padding

我们给UITextField设置了leftView,目的是在文本输入框左側显示一个图标。可是在ios7里,这个图标会紧紧地挨着TextField的左边框,非常不美观,所以就希望设置一个Insets。可是直接设置ImageView的bounds不行,须要用以下这种方法:

@interface YLSTextField : UITextField-(id)initWithFrame:(CGRect)frame Icon:(UIImageView*)icon;@end
@implementation YLSTextField-(id)initWithFrame:(CGRect)frame Icon:(UIImageView*)icon{    self = [super initWithFrame:frame];    if (self) {        self.leftView = icon;        self.leftViewMode = UITextFieldViewModeAlways;    }    return self;}-(CGRect) leftViewRectForBounds:(CGRect)bounds {    CGRect iconRect = [super leftViewRectForBounds:bounds];    iconRect.origin.x += 10;// 右偏10    return iconRect;}@end
UIImage *usernameImage = [UIImage imageNamed:@"user"];UIImageView *usernameIcon = [[UIImageView alloc] initWithImage:usernameImage];usernameIcon.frame = CGRectMake(0, 0, 20, 20);        self.username = [[YLSTextField alloc] initWithFrame:CGRectMake(0, 0, 240, 30) Icon:usernameIcon];self.username.placeholder = @"用户名";self.username.borderStyle = UITextBorderStyleRoundedRect;self.username.clearButtonMode = UITextFieldViewModeWhileEditing;[self.username setKeyboardType:UIKeyboardTypeNumberPad];
关键就是定义UITextField的子类,并覆盖其leftViewRectForBounds方法

转载于:https://www.cnblogs.com/gcczhongduan/p/4479851.html

转载地址:https://blog.csdn.net/weixin_30348519/article/details/98119132 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:分布式系统浅析
下一篇:Linux内核源代码分析方法

发表评论

最新留言

很好
[***.229.124.182]2024年03月16日 16时17分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章