目的是实现例如以下的效果:

UITextField的leftView是自己定义的UIView,当中:

1、包括一个居中显示的icon。而且上,左,下各有1px的间隙

2、左上和左下是圆角,右边没有圆角

居中展示icon

关键是leftView不是UIImageView。而是把UIImageView设置为subview

CGFloat height = frame.size.height;

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, height - 1, height - 2)];

UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
icon.frame = CGRectMake(height / 4, height / 4, height / 2, height / 2);
[leftView addSubview:icon];

上面的代码,使icon在leftView居中显示

制造1px间隙

top和bottom的间隙非常easy做,仅仅要令leftView的height比UITextField的height少2。然后y设置为1

左側的间隙比較麻烦,首先将width设置为比UITextField的width少1。可是直接设置x为1无效。须要override下面方法:

-(CGRect) leftViewRectForBounds:(CGRect)bounds {
CGRect iconRect = [super leftViewRectForBounds:bounds];
iconRect.origin.x += 1;
return iconRect;
}

制造部分圆角

制造全部的圆角比較简单,仅仅须要:

self.layer.cornerRadius = 5;

可是要制造部分圆角,就比較麻烦:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:leftView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = leftView.bounds;
maskLayer.path = maskPath.CGPath;
leftView.layer.mask = maskLayer;

创建阴影

self.layer.shadowColor = [UIColor colorWithRed:132/255.0f green:214/255.0f blue:219/255.0f alpha:1.0f].CGColor;
self.layer.shadowOpacity = 0.5;
self.layer.shadowOffset = CGSizeMake(3, 3);

完整的代码

@implementation LosTextField

-(id)initWithFrame:(CGRect)frame Icon:(NSString*)iconName
{
self = [super initWithFrame:frame];
if (self) { CGFloat height = frame.size.height; UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, height - 1, height - 2)];
leftView.backgroundColor = [UIColor colorWithRed:132/255.0f green:214/255.0f blue:219/255.0f alpha:1.0f]; UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
icon.frame = CGRectMake(height / 4, height / 4, height / 2, height / 2);
[leftView addSubview:icon]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:leftView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = leftView.bounds;
maskLayer.path = maskPath.CGPath;
leftView.layer.mask = maskLayer; self.leftView = leftView;
self.leftViewMode = UITextFieldViewModeAlways;
}
return self;
} -(CGRect) leftViewRectForBounds:(CGRect)bounds {
CGRect iconRect = [super leftViewRectForBounds:bounds];
iconRect.origin.x += 1;
return iconRect;
} @end
self.username = [[LosTextField alloc] initWithFrame:CGRectMake(20, 150, 280, 40) Icon:@"login_username"];

自己定义UITextField的更多相关文章

  1. UITextField,常见属性的罗列和用法

    UITextField是UIControl的子类 ,属于控件类(因为它有能力响应一些高级事件),在故事版中可以直接拖拽过来使用. 首先定义 UITextField *name; name = [[UI ...

  2. 如何创建圆角 UITextField 与内阴影

    本文转自http://www.itstrike.cn/Question/9309fbd6-ef5d-4392-b361-a60fd0a3b18e.html 主要学习如何创建内阴影 我自定义 UITex ...

  3. UITextField设置leftView的Insets

    Insets就是css中的padding 我们给UITextField设置了leftView,目的是在文本输入框左側显示一个图标.可是在ios7里,这个图标会紧紧地挨着TextField的左边框,非常 ...

  4. 超文本传输协议-HTTP/1.1

    超文本传输协议-HTTP/1.1(修订版) ---译者:孙超进本协议不限流传发布.版权声明Copyright (C) The Internet Society (1999). All Rights R ...

  5. UIButton、UILabel、UITextField 初学者需要了解的基本定义和常用设置

     以下是三个IOS开发中最常用的控件,作为IOS基础学习教程知识 ,初学者需要了解其基本定义和常用设置,以便在开发在熟练运用. UIButton按钮 第一.UIButton的定义 UIButton * ...

  6. UI控件(UITextField)

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextField* textField1 = ...

  7. UITextField使用详解

    转iOS中UITextField使用详解 (1) //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFr ...

  8. 你真的了解UITextField吗?

    一:首先查看一下关于UITextField的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextIn ...

  9. UI第三节—— UITextField详解

    戏言:UITextField对于需要登陆注册的界面的作用还是相当明显,但是对于键盘过的遮挡问题,可是重点哦!这里就涉及到通知(NSNotificationCenter)的内容. //注册事件 [[NS ...

随机推荐

  1. 九度oj 题目1416:猴子吃坚果

    题目描述: 动物园的猴子吃坚果的顺序都是按强壮程度来定的,最强壮的吃完才能轮到下一个,现在我们给出各个猴子的名字,强壮程度,吃饱的量,然后查询对应的猴子必须要扔多少坚果才可以轮到. 输入: 输入有多组 ...

  2. Js 希望某链接只能点击一次

    <a onclick=”function(){...}”> 希望这连接只能执行一次 <a onclick=”function(){...}; this.onclick()=funct ...

  3. BZOJ 4870 [Shoi2017]组合数问题 ——动态规划 矩阵乘法

    注意到$r<k$ 别问我为什么要强调. 考场上前30分水水. 然后写阶乘的时候大力$n\log {n}$预处理 本机跑的挺快的,然后稳稳的T掉了. 然后就是简单的矩阵乘法了. #include ...

  4. Spring和redis简单测试demo

    1.1 首先创建一个Maven工程 File --> New --> Other,然后选择Maven目录下的Maven Project,如下图: 然后在弹出的面板中选择配置后,下一步即可, ...

  5. for语句执行顺序

    for语句的结构如下所示: for(初始化;条件;调整) { 示例语句; } 第一次顺序:初始化 ->条件->示例语句 第二次顺序:调整->条件->示例语句 第三次顺序:调整- ...

  6. 从输入url到页面呈现的过程

    从输入url到页面呈现的过程包括两个基本过程:网络通信和页面渲染 网络通信主要过程是 域名解析 -> TCP连接 -> HTTP请求 -> 服务端响应,返回HTML 页面渲染的主要过 ...

  7. python中的daemon守护进程实现方法

    原文参考:http://blog.csdn.net/tao_627/article/details/49532021 守护进程是生存期长的一种进程.它们独立于控制终端并且周期性的执行某种任务或等待处理 ...

  8. 文本生成器(bzoj 1030)

    Description JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以随机生成一些文章―――总是 ...

  9. POJ3625 Building Roads

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10803   Accepted: 3062 Description Fa ...

  10. ObjectDataSource配置数据源的时候,选择业务对象下拉菜单没有任何东西

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 问题:在App_Code这个文件夹中添加了一个类,然后保存,但是在ObjectDataSource配置数据源的时候 ...