//初始化window,并设置大小
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
// Override point for customization after application launch.
//设置window背景颜色为白色.(不设置背景颜色,默认是黑色)
self.window.backgroundColor = [UIColor whiteColor];
/*
创建UIView
1/初始化
2.设置属性
3.添加视图
4.释放对象
*/
//创建一个View,并初始化,设定其位置(x=100,y=100),宽高(height =100,width=100);
UIView *View = [[UIView alloc]initWithFrame:CGRectMake(100,100,100,100)];
//设置View背景颜色(红色)(不设置,默认为透明)
View.backgroundColor = [UIColor redColor];
//在window上添加视图View
[self.window addSubview:View];
//释放View对象
[View release];
frame是⼀个结构体,包含2部分内容:origin和size。
origin也是⼀个结构体,包含2部分内容:x和y。
size同样是⼀个结构体,包含2部分内容:width和height。
frame的origin和size是相对于⽗视图来说的。
CGRectMake()函数可以帮我们快速构造⼀个CGRect变量
UIView *View = [[UIView alloc] initWithFrame:<#(CGRect)#>];
UIView *View = [[UIView alloc] initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedefstructCGPoint CGPoint;
struct CGSize {
CGPoint width;
CGPoint height;
};
typedefstructCGSize CGSize;
struct CGRect {
CGPoint origin;
CGSize size;
};
typedefstructCGRect CGRect;
因此,可以取出视图(View)的起点坐标,及宽高如下:
//取出该View的起点的x值,即横向坐标;width,即宽度.
NSLog(@"%f",aView.frame.origin.x);
NSLog(@"%f", aView.frame.size.width);
center(中⼼点)也是view重要的属性。center是个结构体,包含2个部分:x和y。
center与frame有着密切的联系。
center.x = frame.origin.x + frame.size.width/2;
center.y = frame.origin.y + frame.size.height/2;
//center 用于改变视图的中心点坐标,因此可以改变视图的坐标
View.center = CGPointMake(<#CGFloat x#>, <#CGFloat y#>)
bounds(边界)也是view的重要属性,⽤于定义⾃⼰的边界。它同frame⼀样是⼀个CGRect结构体变量。
当⼀个view设置bounds时,会把⾃⼰当成⼀个容器,定义⾃⼰的边界⼤⼩ 以及 左上⾓的初始坐标。
当⼦视图添加到此视图时,会根据bounds指定的原点(0,0)计算frame,⽽⾮左上⾓。
//bounds默认的起始点是0,0宽高与frame相同,用来改变子视图坐标
View.bounds = CGRectMake(10, 10, 100, 100);
/*
改变父视图的bounds
1.子视图的坐标系
2.父视图没有变化
3.原来的坐标系的(0,0)点改变例如(10,10),即改变bounds
4.寻找现在的(0,0)点(原点)
5.子视图根据新的坐标原点发生变化
*/
UIView的addSubview:⽅法可以添加⼦视图,对于同⼀个视图的所有⼦视图来讲,后添加的⼦视图会把已加的⼦视图盖在下⾯。UIView除了提供添加视图的⽅法,还提供了管理视图层次的⽅法
UIView作为其他UI控件的BaseClass,提供了很多属性
UILabel(标签):是显⽰⽂本的控件。在App中UILabel是出现频率最⾼的控件。
UILabel是UIView⼦类,作为⼦类⼀般是为了扩充⽗类的功能,UILabel扩展了⽂字显⽰的功能,UILabel是能显⽰⽂字的视图。
/*创建UILabel(只要显示的控件,都是这4步)
1.初始化
2.设置属性
3.添加显示
4.释放对象
*/
//创建一个UILabel并初始化,设定其位置(x=100,y=100),宽高(height =200,width=200);
UILabel *View = [[UILabelalloc]initWithFrame:CGRectMake(100,100, 200,200)];
//设置视图背景颜色(红色)
View.backgroundColor = [UIColorredColor];
//设置显示文字
View.text =@"Hello World";
//设置文字颜色
View.textColor = [UIColorblueColor];
//设置对齐方式
View.textAlignment =NSTextAlignmentCenter;(中心对齐)
View.textAlignment = NSTextAlignmentLeft;(左对齐)
View.textAlignment = NSTextAlignmentRight;(右对齐)
//设置显示文本行数如果想有多少显示多少,就给0.
View.numberOfLines =0;
//添加设置阴影颜色
View.shadowColor = [UIColorwhiteColor];
//设置阴影位置(和原文字相比,阴影向x正方向偏移2,向y正方向偏移1)
View.shadowOffset =CGSizeMake(2,1);
//设置字体大小
View.font = [UIFontsystemFontOfSize:30]; (比较常用,只设置大小)
View.font = [UIFont fontWithName:@"Bodoni 72" size:30]; (设置字体,和大小,不常用)
//打印已安装的字体
NSArray *fontArray = [UIFontfamilyNames];
for (NSString *namein fontArray) {
NSLog(@"%@",name);
}
//断行模式
View.lineBreakMode =NSLineBreakByWordWrapping; (以单词为单位换行,常用)
App靠window来呈现内容,⼀个程序⼀般只创建⼀个window。App中能看到的元素,都是UIView及其⼦类。UIView作为所有可视化控件的BaseClass,提供了许多属性和⽅法。显⽰效果控制(frame、alpha等)、视图添加和移除(addSubview:等)、视图层次调整(bringSubviewToFront:等)等。UILabel属于具体的视图,有⾃⼰的侧重点