UI总结-UILabel,UITextField,UIButton
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[ UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
[self.window makeKeyAndVisible];
self.window.backgroundColor = [UIColor whiteColor];
UIViewController *vc = [[UIViewController alloc]init];
self.window.rootViewController = vc;
[_window release];
[vc release];
//UILabel是用于显示文本信息的UI控件,他的父类是UIView
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 200, 30)];
aLabel.backgroundColor = [UIColor redColor];
//UILabel的属性
//显示文本
aLabel.text = @"休杰克曼休杰克曼休杰克曼休杰克曼休杰克曼休杰克曼休杰克曼";
//文本在label中的位置 textAlignment枚举值0.1.2 左中右
aLabel.textAlignment = NSTextAlignmentCenter;
//修改文本的颜色,大小
aLabel.textColor = [UIColor blueColor];
aLabel.font = [UIFont systemFontOfSize:19];
//设置边框
aLabel.layer.borderWidth = 1;
//加边框弧度制
aLabel.layer.cornerRadius = 10;
//隐藏多余的分布
aLabel.layer.masksToBounds = YES;
//文本行数,当行数为0,表示不限行数
aLabel.numberOfLines = 0;
//如果先设置自适应后设置文本内容,会出现label消失的问题,所以一定要先设置文本内容,后设置尺寸自适应.
[aLabel sizeToFit];
//根据什么换行
aLabel.lineBreakMode = NSLineBreakByWordWrapping;
//设置阴影
aLabel.shadowColor = [UIColor cyanColor];
//设置阴影的尺寸
aLabel.shadowOffset = CGSizeMake(2, 2);
[self.window addSubview:aLabel];
[aLabel release];
//UITextField:用于接收用户输入文本的UI控件,他的父类是UIControl
UITextField *aTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 130, 200, 30)];
aTextField.backgroundColor = [UIColor grayColor];
//textfield的属性
//边框样式
aTextField.borderStyle = UITextBorderStyleRoundedRect;
//显示文本
//aTextField.text = @"金刚狼";
//占位字符串
aTextField.placeholder = @"请输入你要选择的商品";
//它也有font,textcolor,textalignment这些属性.
//设置弹出键盘样式,这个属性不是textfield的
aTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
//密文输入
aTextField.secureTextEntry = YES;
//UITextField可以设代理,我们可以用它来收键盘.它的代理方法都不是必须要写的.
aTextField.delegate = self;
[self.window addSubview:aTextField];
[aTextField release];
//UIButton:按钮,用于响应用户点击的控件,它的父类是UIControl
//button 是通过便利构造器来创建新对象的
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
//两种button类型在使用上基本一致,就是system在设置的时候文字默认为蓝色,而出custom默认是白色,在设置图片的时候,system使用setimage方法图片无法显示,custemer可以正常显示.
Button.backgroundColor = [UIColor redColor];
Button.frame = CGRectMake(20, 180, 100, 30);
Button.layer.borderWidth = 1;
Button.layer.cornerRadius = 5;
[self.window addSubview:Button];
//button关联事件
[Button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//button的标题,标题的颜色
[Button setTitle:@"冰火之歌" forState:UIControlStateNormal];
[Button setTitle:@"高粱" forState:UIControlStateHighlighted];
[Button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted
];
//设置button的背景图
[Button setImage:[UIImage imageNamed:@"818006c62c85ebc153a7b5a47c5efbd3.jpg"] forState:UIControlStateNormal];
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(void)click:(UIButton *)button{
NSLog(@"do click!");
}

网友评论