iOS学习之基础控件
// 创建UILabel对象
UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
// 设置文字
userNameLabel.text = @"用户名";
// 将UILabel添加到父视图
[self.window addSubview:userNameLabel];
// 释放所有权(MRC模式下)
// [userNameLabel release];
// 创建UILabel对象(适配屏幕)
UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userNameLabel.frame) + , CGRectGetMinY(userNameLabel.frame), CGRectGetWidth(self.window.frame) - CGRectGetWidth(userNameLabel.frame) - , CGRectGetHeight(userNameLabel.frame))];
UILabel *l2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(userNameLabel.frame), CGRectGetMaxY(userNameLabel.frame) + , CGRectGetWidth(userNameLabel.frame), CGRectGetHeight(userNameLabel.frame))];
UILabel *l3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userNameLabel.frame) + , CGRectGetMaxY(userNameLabel.frame) + , CGRectGetWidth(self.window.frame) - CGRectGetWidth(userNameLabel.frame) - , CGRectGetHeight(userNameLabel.frame))];
// 设置背景颜色
userNameLabel.backgroundColor = [UIColor cyanColor];
// 设置文本对齐方式
userNameLabel.textAlignment = NSTextAlignmentRight;
// 设置文字颜色
userNameLabel.textColor = [UIColor purpleColor];
// 设置字体
userNameLabel.font = [UIFont fontWithName:];
// 打印所有字体样式
NSLog(@"%@", [UIFont familyNames]);
// 显示行数
userNameLabel.numberOfLines = ;
//断行模式(以单词断行)
userNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
// 阴影颜色
userNameLabel.shadowColor = [UIColor blackColor];
// 阴影大小
userNameLabel.shadowOffset = CGSizeMake(, );
// 创建UITextField对象
UITextField *userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(, , , )];
// 设置边框风格
userNameTextField.borderStyle = UITextBorderStyle RoundedRect;
// 设置占位符
userNameTextField.placeholder = @"手机号/邮箱";
// 将UITextField添加到父视图
[self.window addSubview:userNameTextField];
// 释放所有权(MRC)
[userNameTextField release];
// 设置文本内容
userNameTextField.text = @"用户名:";
userNameTextField.textColor = [UIColor blackColor];
// 文本对齐方式
userNameTextField.textAlignment = NSTextAlignmentLeft;
// 是否允许编辑
userNameTextField.enabled = YES;
// 开始编辑时是否清空输入框
userNameTextField.clearsOnBeginEditing = YES;
//是否安全输入
userNameTextField.secureTextEntry = YES;
// 弹出键盘的类型
userNameTextField.keyboardType = UIKeyboardTypeAlphabet;
// 键盘右下角return按钮类型(枚举值)
userNameTextField.returnKeyType = UIReturnKeyDefault;
// 清除按钮模式
userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
// 输入框左视图
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
leftView.backgroundColor = [UIColor yellowColor];
userNameTextField.leftView = leftView;
userNameTextField.leftViewMode = UITextFieldViewModeAlways;
// 输入框右视图
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
rightView.backgroundColor = [UIColor redColor];
userNameTextField.rightView = rightView;
userNameTextField.rightViewMode = UITextFieldViewModeAlways;
// 1.设置代理
userNameTextField.delegate = self;
// 2.遵守协议
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>
// 3.实现协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"键盘上回车按键");
[textField resignFirstResponder];
return YES;
}
// 成为第一响应者,运行就进入编辑状态
[userNameTextField becomeFirstResponder];
// 1.当textField将要开始编辑的时候告诉委托人
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
// 2.当textField已经编辑的时候告诉委托人
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
// 3.当textField将要完成编辑的时候告诉委托人
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
// 4.当textField已经完成编辑的时候告诉委托人
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
// 5.当点击键盘上回车按键时候告诉委托人
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return YES;
}
// 1.创建对象并初始化 (使用类方法)
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
// 2.设置属性
button.frame = CGRectMake(, , , );
button.backgroundColor = [UIColor orangeColor];
// 设置标题 普通状态下
[button setTitle:@"点 我" forState:UIControlStateNormal];
// 设置标题 高亮状态下(点住)
[button setTitle:@"谁点我" forState:UIControlStateHighlighted];
// 设置标题颜色
button.tintColor = [UIColor whiteColor];
// 设置button的背景图片
// 创建UIImage对象
UIImage *afuImage = [UIImage imageNamed:@"afu.jpg"];
// 普通状态下
[button setBackgroundImage:afuImage forState:UIControlStateNormal];
// 高亮状态下
[button setBackgroundImage:[UIImage imageNamed:@"zhatian.jpg"] forState:UIControlStateHighlighted];
// 设置前景图片(必须是镂空图)
[button setImage:[UIImage imageNamed:@"222.png"] forState:UIControlStateNormal];
// 3.添加事件
// 单击状态下
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
// 4.添加到父视图
[self.window addSubview:button];
// 实现按钮点击事件
- (void)buttonClick:(UIButton *)sender {
NSLog(@"点我");
sender.backgroundColor = [UIColor colorWithRed:arc4random() % / / / ];
// 移除事件
[sender removeTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
iOS学习之基础控件的更多相关文章
- iOS学习笔记——基础控件(上)
本篇简单罗列一下一些常用的UI控件以及它们特有的属性,事件等等.由于是笔记,相比起来不会太详细 UIView 所有UI控件都继承于这个UIView,它所拥有的属性必是所有控件都拥有,这些属性都是控件最 ...
- iOS学习之UIPickerView控件的关联选择
接上篇iOS学习之UIPickerView控件的简单使用 接着上篇的代码 http://download.csdn.net/detail/totogo2010/4391870 ,我们要实现的效果如下: ...
- IOS 学习笔记(6) 控件 文本域(UITextField)的使用方法
UITextField控件的诸多特性都和UITextView相似,比如成为输入文本焦点时键盘自动显示,支持长按弹出动作选项,能够接收输入事件(开始输入,修改内容,结束输入和点击回车等). 1.特有的特 ...
- iOS学习之UIDatePicker控件使用
iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. , 您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Ti ...
- ios 学习笔记之控件属性
1.文本框 设置密码属性:Secure Text Entry 勾选; 设置文本框带清除属性: Clear Button =Is always visible; 默认是不带清除属性:Never app ...
- IOS 学习笔记(7) 控件 分隔栏控件(UISegmentControl)的使用方法
分隔栏控件的系统默认式样一共有3种,分别是“普通式样”,"边框式样","条状式样" 分隔栏控件中有一个momentary属性,默认时NO.当开发者配置成YES时 ...
- IOS 学习笔记(5) 控件 文本视图(UITextView)的使用方法
相对于UILabell所支持的较短文本内容,UITextView对于长文本的支持更好.UITextView能够以滚动的方式全部浏览到长文本,并且就像UILabel那样,从ISO6,他也提供了对NSAt ...
- IOS 学习笔记(4) 控件 标签(UILabel)的使用方法
虽说Label的中文翻译是标签标记,但它其实是一个静态文本内容的展现控件. 一般来说,UILabel只是一个只读的文本视图,开发者可以利用UiLabel来展示内容长度有固定上限的文字内容.并且,UIL ...
- iOS学习之UIPickerView控件的简单使用
UIPickerView控件在给用户选择某些特定的数据时经常使用到,这里演示一个简单的选择数据,显示在UITextField输入框里,把UIPickerView作为输入View,用Toolbar作为选 ...
随机推荐
- 由多次使用Statement实例引起的Result set already closed异常的解决方案
在不同版本的Weblogic平台上迁移应用,产生了很严重的JDBC驱动版本不兼容的问题. 但是归根究底是代码的问题,废话少说,上代码示例. ..... //以下是问题代码 ResultSet rs=n ...
- Bug修复问题
采用下面的代码,访问网页:http://www.weather.com.cn/data/cityinfo/101010100.html,想读取下图中红框中的内容,但是抛出了IOException,通过 ...
- 二模09day1解题报告
T1.词编码(word) 给出一些原长为n的01串经过变化后的串求原串.原串的特点是:各个1的位置号和%(n+1)==0 变法(只取其一): 改一个0为1 删一个 加一个 不变. 其中2优先考虑位置靠 ...
- ionic pull to refresh 下拉更新頁面
有些項目都用到了下拉更新頁面的效果: 1. 在index.html 中添加ion-refresher 指令 且在我們需要更新內容的外面 添加 如 <ion-refresher pulling-t ...
- ASP.NET的SEO:基础知识
本系列目录 首先谈一点我自己的体会,我还是希望能和大家分享: 当你读到一定数量的SEO资料后,你会发现,对同一个问题,众说纷纭,莫衷一是.这其实主要是因为以下一些原因造成的:1. 很多SEO技巧,是& ...
- css兼容
1.不同浏览器默认边距不同,必须对body自定义:margin:0;padding:0; 2.margin.padding属性值为%时,不是所有浏览器都支持: 3.transparent属性,IE7之 ...
- POJ C++程序设计 编程题#1 编程作业—STL1
编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出结 ...
- SQLServer存储过程入门
1.创建一个返回结果集的存储过程 create procedure firstpro As begin select * from dbo.Person End 执行: execute dbo.fir ...
- JS模块化工具requirejs教程(二):基本知识
基本API require会定义三个变量:define,require,requirejs,其中require === requirejs,一般使用require更简短 define 从名字就可以看出 ...
- (转)浅谈HTML5与css3画饼图!
神马系饼图? 饼图,大家都应该熟知,在统计数据对比方面,几乎处处用到.如cnzz的统计饼图 从饼图中,很形象地展示了访问者地区的分布,以扇形为块的方式拼成一个大圆. 都使用什么方法实现 目前众多站点制 ...