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设置leftView的Insets的更多相关文章

  1. 给UITextField设置头或尾空白

    有时候,我们需要在UITextField的头尾加入一些空白,如下图所示: 其中,黄色和红色部分代表空白. 实现起来,比较简单,只需要设置UITextField的leftView.leftViewMod ...

  2. uitextfield 设置为密码框显示

    uitextfield 设置为密码框显示: 在xib中,将文本secure的复选框选中即可.

  3. (转)设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)

    转自http://unmi.cc/uilable-uitextfield-padding-insets 主要是理解下UIEdgeInsets在IOS UI里的意义.靠,这货其实就是间隔,起个名字这么让 ...

  4. 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)

    转自http://unmi.cc/uilable-uitextfield-padding-insets 主要是理解下UIEdgeInsets在IOS UI里的意义. 靠,这货其实就是间隔,起个名字这么 ...

  5. UITextfield设置Placeholder颜色 控件 内边距、自适应高度

    //创建UITextField对象 UITextField * tf=[[UITextField alloc]init];    //设置Placeholder颜色 [text setAttribut ...

  6. iOS UITextField设置placeholder颜色

    设置UITextField的placeholder颜色 UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = ...

  7. iOS学习-UITextField设置placeholder的颜色

    UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(, , , )]; text.borderStyle = UITex ...

  8. IOS UITextField 设置光标位置

    textField.leftView = [[[UIView alloc] initWithFrame:CGRectMake(, , , )] autorelease]; textField.left ...

  9. UITextField设置密文延时处理---仿QQ登录密码输入

    系统的UITextField输入的时候最后一个字符会有1-2s的效果展示, 效果如下: 为了解决这个问题, 可以用字符 "●" 替换, 替换后效果如下: 用到的是UITextFie ...

随机推荐

  1. BZOJ 2245 SDOI 2011 工作安排 费用流

    题目大意:有一些商品须要被制造.有一些员工.每个员工会做一些物品,然而这些员工做物品越多,他们的愤慨值越大,这满足一个分段函数.给出哪些员工能够做哪些东西,给出这些分段函数,求最小的愤慨值以满足须要被 ...

  2. [Angular] HostListener Method Arguments - Blocking Default Keyboard Behavior

    We are going to see how to using method arguments for @HostListener. First, we can use HostListener ...

  3. std::的概念与作用

    std:: 当中std是名称空间,防止反复.比如说很多人给函数取名可能都叫f1():你使用的时候就可能造成问题.如果各人均把自己的f1()放进自己的名称空间.我们在使用的时候带上名称空间就不会有问题. ...

  4. 17、网卡驱动程序-DM9000举例

    (参考:cs89x0.c可以参考) DM9000 芯片实现网络功能的基础,在接收数据时采用中断方式,即当有数据到来并在 DM9000 内部 CRC 校验通过后会产生一个接收中断: 网卡驱动程序框架: ...

  5. php 微信支付企业付款

    1.所需参数 字段名 变量名 必填 示例值 类型 描述 公众账号appid mch_appid 是 wx8888888888888888 String 公众号的appId 商户号 mchid 是 19 ...

  6. JMS是一种应用于异步消息传递的标准API

    JMS是一种应用于异步消息传递的标准API,作为Java平台的一部分,JMS可以允许不同应用.不同模块之间实现可靠.异步数据通信.一些概念 JMS provider    An implementat ...

  7. php获取调用本方法的上个方法,php堆栈,函数入库

    $array =debug_backtrace(); //print_r($array);//信息很齐全 unset($array[0]); foreach($array as $row) { $ht ...

  8. Opencv中integral计算积分图

    Paul Viola和Michael Jones在2001年首次将积分图应用在图像特征提取上,在他们的论文"Rapid Object Detection using a Boosted Ca ...

  9. C++常用数据结构的实现

    常用数据结构与算法的实现.整理与总结 我将我所有数据结构的实现放在了github中:Data-Structures-Implemented-By-Me 常用数据结构与算法的实现.整理与总结 KMP字符 ...

  10. [React] Use React.cloneElement to Extend Functionality of Children Components

    We can utilize React.cloneElement in order to create new components with extended data or functional ...