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方法
分享到:
相关推荐
该文档详细列出UITextField常用以及不常用的相关属性及如何进行设置,对大家掌握UITextField的用法很有帮助。
实现UITextField的键盘隐藏以及为了防止键盘挡住TextField而上移下移
UITextField是IOS中非常常用的一个控件,用来接收用户输入信息,完成应用和用户的交互。它的主要属性设置如下: //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:...
UITextView 实现UITextField功能
iOS UITextField 扩展 密码框
iOS文本框,有时候需要用到类似炒股软件添加百分比或者数字等
UITextField 文本字段控件 小Demo
UITextField+Extension 是 UITextField扩展 使用runtime实现,完美支持第三方键盘,实现上移效果 通过添加UITextField类目,使用runtime实现,TextField被遮挡...可以设置距keyboard距离 完美支持第三方键盘显示与隐藏
使用通知,动态获取UITextField中汉字个数
UItextField和UItextView键盘覆盖问题,可以随键盘上高的改变,自动移动父类UIview
本demo主要是解决一般限制输入字符数量方法存在的一个bug,就是在将要超过限制时输入一串非常长的字符串,选择字符串时会提示超过限制问题;
UITextFieldBlockDemo
UITextField代理事件_类似QQ登陆窗口的简单实现 UITextField显示暗文_删除小叉号
一个UITextField类的category, 可以一行代码实现对UITextField字数的限制。
UITextField Demo IOS Objective-C 代码
轻量级可改变占位符颜色的UITextfield,可更改高亮状态和普通状态下的占位符颜色和文字颜色,灵活度高,可自己任意定义,
通过UItextField自定义UISearchBar 描述了UItextField代理的来实现uisearchbar功能
详尽的列出了UITextField的各个使用,并有汉字说明
一行代码,设置UITextField的输入格式限制,比如手机号、身份证号、银行卡号格式以及输入字符类型个数的限制等; 接口说明 /** * ZASTextFieldFormatDelegate代理 * */ @property (nonatomic,
UITextField邮箱后缀联想输入 github地址: https://github.com/cheng534078182/TextMatchEmail