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作为选 ...
随机推荐
- UVa11324 最大团 The Largest Clique-有向图强连通分量&DP
https://vjudge.net/problem/UVA-11324 给定一张有向图G,求一个节点数目最大的节点集,使得该集合中的任意两个节点u和v满足:要么u可以到达v,要么v可以到达u(u,v ...
- 在centos上安装mysql5.7的三种方法
带OS信息的是已编译的二进制文件,不带OS信息的是源码包 mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz 二进制包 mysql-5.5.51.tar.gz 源码包 ...
- Datatable导出Excel
; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...
- 错误处理php
一 HP关闭脚本错误提示的方法: 打开PHP安装目录下的php.ini文件 找到display_errors = On 修改为 display_errors = off 注意:如果你已经把PHP.in ...
- 使用/proc实现内核与用户空间通信
1. 前言 Linux内核空间与用户空间的通信可通过"/proc"目录的文件读写来实现,如果只是控制内核中的参数而不是传输较多数据的话,用“/proc”是很合适的.另外一种内核 ...
- (笔记)angular material radio用法
- javaSE第二天
第二天 7 1:关键字(掌握) 7 2:标识符(掌握) 7 (1)就是给类,接口,方法,变量等起名字的字符序列 7 (2)组成规则: 7 (3)注意事项: 8 (4 ...
- zedboard上移植OPENCV库
zedboard上移植OPENCV库 之前做了很多移植OPENCV库的工作,但是需要包含的各种库,需要交叉编译,X264 ,JPGE ,FFMPGE等等 注意:在<嵌入式系统软硬件协同设计实战指 ...
- 【PHP】金额数字转换成大写形式
<?php /*将数字金额转成大写*/ function num_to_upper($num) { $d = array('零','壹','贰','叁','肆','伍','陆','柒','捌', ...
- C# DataGridViewComboBoxColumn 数据绑定
dataGridView1.Columns.Clear(); dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = ...