对于UI控件,我们创建一个新项目,类别是IOS类下的Applicatin中的Single View Application。
UILabel
UILable是iPhone界面最基本的控件,主要用来显示文本信息。
修改项目中的ViewController.m文件,达到创建UILable,设置其中的显示内容,字体大小颜色,阴影,字体对齐方式,显示行数等。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s6 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s8 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s9 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s10 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
//创建UI控件
-(void)createUI
{
//定义并创建一个UI Label
//UILabel可以显示在屏幕上的,并且可以显示文字的UI控件
UILabel* label=[[UILabel alloc]init];
label.text=@"hello world!bababababkkkkk";
label.frame=CGRectMake(100, 150, 160, 90);
label.backgroundColor=[UIColor redColor];
[self.view addSubview:label];
//设置字体大小颜色
label.font=[UIFont systemFontOfSize:22];
label.textColor=[UIColor blackColor];
//设置阴影颜色
label.shadowOffset=CGSizeMake(5, 10);
label.shadowColor=[UIColor grayColor];
//文字对齐方式
label.textAlignment=NSTextAlignmentCenter;
//文字显示行数,默认为1 如果值为0 即按需要显示
label.numberOfLines=3;
}
UIButton
修改项目中的ViewController.m文件,达到创建按钮,设置文字内容和风格,按钮事件等效果:
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s10 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s11 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s12 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}
-(void)creatRectButton
{
//通过类方法来创建 圆角按钮
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(100, 100, 100, 40);
//设置按钮文字内容
[btn setTitle:@"button1" forState:UIControlStateNormal]; //正常状态
[btn setTitle:@"press button" forState:UIControlStateHighlighted]; //按下状态
//设置文字颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
//设置风格颜色
[btn setTintColor:[UIColor whiteColor]];
//设置文字大小
btn.titleLabel.font = [UIFont systemFontOfSize:12];
//设置按钮背景颜色
btn.backgroundColor=[UIColor grayColor];
//按钮事件
//向按钮添加事件函数 UIControlEvent--事件处理函数类型 TouchUpInside--按钮弹起 并光标离开位置在按钮范围内
[btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
btn.tag=101; //设置按钮标记值 除指针外也可以通过tag区分
//添加到视图中并显示
[self.view addSubview:btn];
}
-(void)pressBtn{
NSLog(@"@button pressed");
}
运行效果:
还可以创建一个图片按钮:
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}span.s1 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
//可以显示图片的按钮
-(void)createImgBtn{
//创建一个自定义类型
UIButton* btnImage=[UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame=CGRectMake(100, 300, 50, 50);
UIImage* icon01=[UIImage imageNamed:@"1.jpeg"];
UIImage* icon02=[UIImage imageNamed:@"2.jpeg"];
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
[self.view addSubview:btnImage];
}
UIWindow
UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow。
iOS程序启动完毕后,创建的第一个视图控件就是UIWindow,接着创建控制器的view,最后将控制器的view添加到UIWindow上,于是控制器的view就显示在屏幕上了。
一个iOS程序之所以能显示到屏幕上,完全是因为它有UIWindow。也就说,没有UIWindow,就看不见任何UI界面。
通过自编程序了解UIWindow创建过程:
在AppDelegate.m文件中修改:
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s7 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s9 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s10 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s11 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
//当程序框架初始化成功后,调用此函数
//用来初始化整个程序框架结构。即整个程序对IOS开发工程师的入口函数
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建一个window对象
//整个程序中只有一个UIWindow对象
//在程序基本上表示屏幕窗口
//UIWindow也是继承自UIView,是一个特殊的UIView
//UIScreen表示屏幕硬件表示类,mainSreen获得主屏幕设备信息
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//创建一个视图控制器,作为UIWindow的跟视图控制器
self.window.rootViewController=[[UIViewController alloc]init];
self.window.backgroundColor=[UIColor blueColor];
UIView* view=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor=[UIColor redColor];
UIView* backView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 240, 360)];
backView.backgroundColor = [UIColor greenColor];
//背景视图
[backView addSubview:view];
//将backView作为view的父视图.子视图的坐标的参考系是父视图的坐标
[self.window addSubview:backView];
//使window有效并显示到屏幕上
[self.window makeKeyAndVisible];
//这些window是同一个window
//当子视图添加到父视图上时,子视图window会被父视图window赋值,打印输出的是window地址
NSLog(@"%@",view.window);
NSLog(@"%@",backView.window);
NSLog(@"%@",self.window);
return YES;
}