目的是实现如下的效果:
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的间隙很容易做,只要令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是IOS开发中用户交互中重要的一个控件,常被用来做账号密码框,输入信息框等。本文给大家介绍iOS开发之自定义UITextField的方法,感兴趣的朋友一起学习吧
CustomTextField 在Swift中创建一个自定义UITextField,您可以简单地实现UITextFieldDelegate以供重用
通过UItextField自定义UISearchBar 描述了UItextField代理的来实现uisearchbar功能
CustomTextField(自定义 UITextField) clearButtonImage - 夹板按钮的设置图像 allCornersRadius - 设置角半径,所有角 topCornersRadius - 设置角半径、顶角 bottomCornersRadius - 设置角半径、底角 ...
可以自定义UITextField以及UITextView在输入文字时(即获得焦点时)的背景颜色以及文字颜色。如demo所示,第一幅图是第一个TextField获得焦点时,背景设成黄色;第二幅图是第二个TextField获得焦点时,背景设成...
Android基础控件——EditText的自定义、高仿iOS的UITextField
一个方法自定义输入内容的限制,如保留两位小数,限制输入特殊符号
自定义AlertView,包含有头部图片展示,输入框UITextfield,标题title,内容,侧边图片Icon
来源:github/ASTextFieldLicence:MIT作者:Adil Soomro 自定义 UITextField 的外观,可用作登录框。
来源:Licence:MIT作者:LifeGameˇ ... 自定义UITextField,根据第一响应者动态变化输入框的指示名字。输入框标题可以自动往上缩,也可以清理所输入的内容。支持暗码功能。 小编注:感谢用户@LifeGameˇ !
自定义UITextField输入方式,输入文字时弹出的键盘工具栏上,有“previous”、“next”、“done”等按钮,还有一个“autofill”按钮,用于自动填入某些已经定义好的文字。 [优才 · Code4App]编译测试,适用环境...
但是UITextField的诸多回调里面,没有一个是适合触发搜索时间的。 UITextFieldTextDidChangeNotification调用过于频繁,每输入一个字符就调一次接口怕是不太合适。 UITextFieldTextDidEndEditingNotification只有在...
HXSearchBar是一款完全可以自定义的搜索框 特性: -继承自UISearchBar,使用起来...-HXSearchBar可以自定义光标颜色,清除按钮图标,取消按钮图片,获取取消按钮,获取输入框UITextField,想怎么自定义完全看你的需求
iOS自定义数字键盘 使用简单 适配iPhone6/6PLUS self.keyBoardTF.inputView = [[WSNumberKeyBoard alloc] initWithControlTextField:self.keyBoardTF ReturnButtonName:nil andClickBlock:^(UITextField *...
AnimatedTextInput, iOS的动画UITextField和UITextView替换 AnimatedTextInput iOS Jobandtalent应用中使用的iOS自定义文本输入组件。 安装使用cocoapods在项目中安装这里自定义控件。pod 'AnimatedTextInput'
TextField-InputView, UITextField InputView模式:终结注册类、信息采集类
一个完全包装的“ UITextField”,完全可以在SwiftUI中使用。 :eagle: | | 开始使用 iTextField 将iTextField添加到您的项目 import SwiftUI import iTextField struct ContentView : View { @State var text: ...
在原有的原生方法中增加一些方法帮助用户快速创建TextField,并且可以使用自定义typedef方法用于快速的创建完成一些自定义赋值