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

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. 【java基础 14】锁的粒度:ThreadLocal、volatile、Atomic和Synchronized

    导读:题目中提到的几个关键字,分别是解决并发问题中,加锁所使用到的几个关键字,每个关键字代表的锁的粒度 不同,本篇博客,主要是从概念定义上,区分这几个关键字的应用场景.(PS:睡梦中,依稀记得有回面试 ...

  2. curl保存图片

    $url = 'http://p1.qhimg.com/t013dfc89f8a039122c.jpg?size=690x460'; function http_get_data($url) { $c ...

  3. 【Luogu】P2155沙拉公主的困惑(数论)

    题目链接 数论果然是硬伤qwq 还是智商上的硬伤 我们来讲两个道理 No.1 求1~i!中与i!互质的数的个数 实际上就是求i!的欧拉函数 有如下递推式: f[1]=1 if(i为合数) f[i]=f ...

  4. CSS:IE,Chrome,Firefox兼容性和CSS Hack(转载)

    原作者:微米博客 以前写过一篇关于CSS hack的文章,但近期回头看了看发现理解的不够深刻,总结的也不凝练,于是今天重新测试从新写一篇.常用的CSS hack如下(笔者只对IE&FF& ...

  5. HDU——1596find the safest road(邻接矩阵+优先队列SPFA)

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. BZOJ 4552 [Tjoi2016&Heoi2016]排序 ——线段树 二分答案

    听说是BC原题. 好题,二分答案变成01序列,就可以方便的用线段树维护了. 然后就是区间查询和覆盖了. #include <map> #include <cmath> #inc ...

  7. wifi hand

    wpa airmon-ng start wlan0airodump-ng -c 6 -w logs  wlan0monaireplay-ng -0 5 -a ap'mac -c clink'mac w ...

  8. linux maven安装(三)

    1.下载maven http://maven.apache.org/download.cgi 我下载的是:apache-maven-3.3.9-bin.tar.gz 解压:tar -zxvf apac ...

  9. Zabbix实现短信报警设置(实战)

    配置环境: zabbix 2.2.15 1.配置示警媒介类型 此文件所在位置:/usr/lib/zabbix/alertscripts/ 必须拥有执行权限,并且改变所属用户和组 要修改此脚本的路径,需 ...

  10. Codeforces 622F The Sum of the k-th Powers

    Discription There are well-known formulas: , , . Also mathematicians found similar formulas for high ...