自己定义UITextField
目的是实现例如以下的效果:
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的更多相关文章
- UITextField,常见属性的罗列和用法
UITextField是UIControl的子类 ,属于控件类(因为它有能力响应一些高级事件),在故事版中可以直接拖拽过来使用. 首先定义 UITextField *name; name = [[UI ...
- 如何创建圆角 UITextField 与内阴影
本文转自http://www.itstrike.cn/Question/9309fbd6-ef5d-4392-b361-a60fd0a3b18e.html 主要学习如何创建内阴影 我自定义 UITex ...
- UITextField设置leftView的Insets
Insets就是css中的padding 我们给UITextField设置了leftView,目的是在文本输入框左側显示一个图标.可是在ios7里,这个图标会紧紧地挨着TextField的左边框,非常 ...
- 超文本传输协议-HTTP/1.1
超文本传输协议-HTTP/1.1(修订版) ---译者:孙超进本协议不限流传发布.版权声明Copyright (C) The Internet Society (1999). All Rights R ...
- UIButton、UILabel、UITextField 初学者需要了解的基本定义和常用设置
以下是三个IOS开发中最常用的控件,作为IOS基础学习教程知识 ,初学者需要了解其基本定义和常用设置,以便在开发在熟练运用. UIButton按钮 第一.UIButton的定义 UIButton * ...
- UI控件(UITextField)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UITextField* textField1 = ...
- UITextField使用详解
转iOS中UITextField使用详解 (1) //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFr ...
- 你真的了解UITextField吗?
一:首先查看一下关于UITextField的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextIn ...
- UI第三节—— UITextField详解
戏言:UITextField对于需要登陆注册的界面的作用还是相当明显,但是对于键盘过的遮挡问题,可是重点哦!这里就涉及到通知(NSNotificationCenter)的内容. //注册事件 [[NS ...
随机推荐
- Linux Shell系列教程之(八)Shell printf命令详解
本文是Linux Shell系列教程的第(八)篇,更多shell教程请看:Linux Shell系列教程 在上一篇:Linux Shell系列教程之(七)Shell输出这篇文章中,已经对Shell p ...
- 【Luogu】P3521ROT-Tree Rotations(线段树合并)
题目链接 神奇的线段树合并qwq 不过就思路而言很好想…… 观察到一棵树无论怎么交换两棵左右子树,子树内部的最优逆序对并没影响……决策只影响左右子树之间的逆序对…… 于是线段树合并直接乱搞就好啦 ...
- POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28399 Accepted: 9684 De ...
- Java如何获取ISO 8601时间
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZ ...
- Java面试题之Java反射的原理
什么是Java的反射? 在运行状态中,对于任意一个类,都能够知道这个类的属性和方法: 反射的作用? 如果给定一个类名,就可以通过反射机制来获取类的所有信息,也可以动态的创建对象和编译: 反射的原理? ...
- Java面试题之什么情况下会触发类的初始化
以下情况会触发类的初始化: 遇到new,getstatic,putstatic,invokestatic这4条指令: 使用java.lang.reflect包的方法对类进行反射调用: 初始化一个类的时 ...
- Beyond compare vs kdiff3
這裡使用的 kdiff3 版本是 0.9.98 基於以下 三點,最終選擇了 beyond compare 1. kdiff3 不能刪檔案, 以下為例,不能刪1 2. kdiff3 ...
- PHP实现15位身份证号转18位
PHP实现15位身份证号转18位 参考博客: 作者:selfimpr626 来源:CSDN (根据身份证号计算年龄,15位身份证号码转18位) 原文:https://blog.csdn.net/wei ...
- HDU 6188最小费用流
题目链接:http://hdu.hustoj.com/showproblem.php?pid=6118 掉坑里了,图很好建,Wa了一发,看了Disscuss里面有人提供了一组样例,画图发现:最小流模板 ...
- BZOJ 4568 [Scoi2016]幸运数字(树链剖分 + 异或线性基)
题目链接 BZOJ 4568 考虑树链剖分+线段树维护每一段区域的异或线性基 对于每个询问,求出该点集的异或线性基.然后求一下这个线性基里面能异或出的最大值即可. #include <bits ...