UITextField设置leftView的Insets
2014-05-17 11:44
369 查看
Insets就是css中的padding
我们给UITextField设置了leftView,目的是在文本输入框左侧显示一个图标。但是在ios7里,这个图标会紧紧地挨着TextField的左边框,很不美观,所以就希望设置一个Insets。但是直接设置ImageView的bounds不行,需要用下面这个方法:
关键就是定义UITextField的子类,并覆盖其leftViewRectForBounds方法
我们给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方法
相关文章推荐
- UITextField设置leftView的Insets
- iOS开发之UITextField 左侧设置小图标 leftview
- UILabel 内容的UIEdgeInsets 和 UITextField的 leftView/rightView
- 如何设置UITextField的leftView和rightView的inset
- iOS之UITextField设置leftView与rightView
- 设置 UILabel 和 UITextField 的 Padding 或 Insets
- ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- the item width must be less than the width of the UICollectionView minus the section insets left and
- 升级到XCode6后,iOS8里设置tableview的setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
- (转)设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets(内容与边距)
- UITextField的leftView
- 升级到XCode6后,iOS8里设置tableview的setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 给Button或TextView设置drawableLeft图片(左图标)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets
- 【Android】TextView动态设置android:drawableLeft|Right|Top|Bottom,SetColor