自定义视图.
来源:互联网 发布:什么是淘宝钻石买家 编辑:程序博客网 时间:2024/05/16 12:59
自定义视图:
自定义视图:除了系统UI提供的各种视图外,自己可以利用各种控件组合成各种的新的视图,积累自己的代码库,方便自己开发.
自定义视图的步骤(再次以Label和TextField为例):
1.创建一个UIView的子类
2.在类的初始化方法中添加视图
//初始化方法-(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self addAllSubviews];//在初始方法中添加视图 } return self;}//添加视图-(void)addAllSubviews{ //UILabel _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,self.frame.size.width /3, self.frame.size.height)]; [self addSubview:_label]; //UITextField _textField = [[UITextField alloc]initWithFrame:CGRectMake(self.frame.size.width / 3 ,0,self.frame.size.width / 3 * 2,self.frame.size.height)]; [self addSubview:_textField]; //UIButton _button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; _button.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y + 10, self.frame.size.width, self.frame.size.height); [self addSubview:_button];}3.在.h的文件中提供一些接口(方法),便于外界操作
//// Login.h// UIlession3_review//// Created by lanou3g on 15/8/26.// Copyright (c) 2015年 lanou3g. All rights reserved.////自定义登陆界面视图#import <UIKit/UIKit.h>@interface Login : UIView <UITextFieldDelegate>@property(nonatomic,readonly,retain)UILabel *label;@property(nonatomic,readonly,retain)UITextField *textField;@property(nonatomic,retain)UIButton *button;//设置TextField的样式-(void)setTextFieldStyle:(UITextBorderStyle)style;//设置Label和TextField的字体-(void)setLabelText:(NSString *)str;-(void)setTextFieldPlaceholder:(NSString *)str;//字体的对齐方式-(void)setLabelTextAlignment:(NSTextAlignment)alignment;-(void)settextFieldAlignment:(NSTextAlignment)alignment;//清除模式-(void)setTextfieldClearMode:(UITextFieldViewMode)clearMode;//密文模式-(void)setTextSecurity:(BOOL)security;//代理模式-(void)setDelegate;@end在.m中实现上述方法:
<span style="font-size:18px;">//设置边框样式-(void)setTextFieldStyle:(UITextBorderStyle)style{ self.textField.borderStyle = style;}//设置Label和TextField的字体-(void)setLabelText:(NSString *)str{ self.label.text = str;}-(void)setTextFieldPlaceholder:(NSString *)str{ self.textField.placeholder = str;}//设置对齐方式-(void)setLabelTextAlignment:(NSTextAlignment)alignment{ self.label.textAlignment = alignment;}-(void)settextFieldAlignment:(NSTextAlignment)alignment{ self.textField.textAlignment = alignment;}//清除模式-(void)setTextfieldClearMode:(UITextFieldViewMode)clearMode{ _textField.clearButtonMode = clearMode;}//密文模式-(void)setTextSecurity:(BOOL)security{ _textField.secureTextEntry = security;}//代理模式-(void)setDelegate{ self.textField.delegate = self;}//实现代理-(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES;}</span><span style="font-size:18px;">测试文件:</span><pre name="code" class="objc"> self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; UIView *backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]; backgroundView.backgroundColor = [UIColor whiteColor]; [self.window addSubview:backgroundView]; Login *login = [[Login alloc]initWithFrame:CGRectMake(40, 40, 300, 40)]; [backgroundView addSubview:login]; //设置边框的样式 [login setTextFieldStyle:UITextBorderStyleRoundedRect]; //设置字体 [login setLabelText:@"登陆:"]; [login setTextFieldPlaceholder:@"用户名/手机号/邮箱"]; //字体对齐方式 [login setLabelTextAlignment:NSTextAlignmentCenter]; [login settextFieldAlignment:NSTextAlignmentLeft]; //清除模式 [login setTextfieldClearMode:UITextFieldViewModeAlways]; //密文模式 [login setTextSecurity:YES]; //代理 [login setDelegate];
0 0
- 自定义视图-马赛克视图
- 自定义视图
- 自定义视图
- 自定义视图
- 自定义视图
- 自定义视图
- 自定义视图
- 自定义视图
- 自定义视图
- 自定义视图.
- 自定义视图
- 自定义视图
- 自定义视图
- 自定义视图
- onLayoutLayoutAndroid自定义视图自定义Layout
- 自定义视图引擎
- Android-自定义视图
- Android/OPhone自定义视图
- 嵌入式linux内核制作
- 常见HTTP错误代码大全
- LeetCode题解:Reverse Bits
- 9+实现斐波那契Fibonacci数列f(n),
- plsql函数大全
- 自定义视图.
- 计算机网络面试题2
- HDU-4763 Theme Section
- C/C++编程题之完美数判断
- 属性动画实现viewpager改变页面的时候,带有指示条的动画效果
- Windows Azure Service Bus - Bridge of Azure And Private Cloud
- 欢迎使用CSDN-markdown编辑器
- 按键中断
- POJ 1753 Flip Game(高斯消元法,枚举自由变元)