UI_基本控件_UILabel_UITextField_UIButton_UIFont_UIAlertView
label控件
// label控件
UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 150)];
// 背景颜色
lable1.backgroundColor = [UIColor cyanColor];
// 透明度
lable1.alpha = 0.8;
[self.window addSubview:lable1];
[lable1 release];
// 显示的文本信息
lable1.text = @"用户名:";
// 文本颜色
lable1.textColor = [UIColor redColor];
// 阴影颜色
lable1.shadowColor = [UIColor yellowColor];
// 阴影的偏移量
lable1.shadowOffset = CGSizeMake(3, 3);
// 文本的对齐方式
lable1.textAlignment = NSTextAlignmentCenter; // 右对齐
// 当文本过长时,label显示的断行方式
lable1.lineBreakMode = NSLineBreakByTruncatingMiddle;
lable1.lineBreakMode = NSLineBreakByTruncatingHead;
lable1.lineBreakMode = NSLineBreakByTruncatingTail;
// 控制label显示的行数
lable1.numberOfLines = 2;
UITextField 控件
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
textField.backgroundColor = [UIColor cyanColor];
textField.alpha = 0.8;
textField.textColor = [UIColor blackColor];
[self.window addSubview:textField];
[textField release];
// 默认的占位字符, 一旦用户输入, 自行隐藏
textField.placeholder = @" 请输入用户名:";
// 1.UITextField输入控制
// 输入文本全部转为黑点
textField.secureTextEntry = YES;
// 改变键盘类型
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.keyboardType = UIKeyboardTypeEmailAddress;
// 2.UITextField外观控制
// 边框样式
textField.borderStyle = UITextBorderStyleRoundedRect;
// 清除按钮
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
// 设置textField的tag值
textField.tag = 10000;
UIButton
// UIButton 按钮.处理点按
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 50, 280, 40);
// 设置按钮标题
// 参数1:标题内容
// 参数2:按钮的状态
[button setTitle:@"确认" forState:UIControlStateNormal];
button.backgroundColor = [UIColor grayColor];
button.alpha = 0.6;
[self.window addSubview:button];
// 点击的时候设置button高亮
button.showsTouchWhenHighlighted = YES;
UIButton 设置点击方法
// 给按钮绑定一个方法, 点击的时候, 让某个特定的对象调用这个方法
// 参数1:执行方法的对象
// 参数2:要执行的方法(参数1的对象去执行)
// 参数3:按钮让绑定的对象调用方法,需要出发的事件
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
给按钮点击设定方法
- (void)buttonClicked:(UIButton *)button
{
NSLog(@"被点击了!");
}
UIAlertView 的使用
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码将保存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"不确定", nil];
[alertView show];
[alertView release];
UIAlertView 一般嵌套在Button点击事件中.
将上述按键点击方法和UIAlertView结合
- (void)buttonClicked:(UIButton *)button
{
NSLog(@"被点击了!");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码将保存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"不确定", nil];
[alertView show];
[alertView release];
}
设置UIAlertView中的选项触发的事件,还是上述和UIButton点击方法结合
<pre name="code" class="objc"> // 1. 签订协议
@interface MainViewController : UIViewController <UIAlertViewDelegate>
- (void)buttonClick:(UIButton *)button
{
// 2.指定代理人(delegate),一般都是写self(当前类的一个对象)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"重新输入" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
[alert show];
[alert release];
}
// 3.实现相应的协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// 点击了alertView上的哪个按钮, 根据bottonIndex索引值设置相应方法
NSLog(@"%d", buttonIndex);
}
UIFont 系统默认字体大小:17
view15.font = [UIFont systemFontOfSize:20];
view15.font = [UIFont fontWithName:@"Monaca" size:18];
view15.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:11] size:20];
// 显示系统字体
NSLog(@"%@", [UIFont familyNames]);