UIView & UILabel

来源:互联网 发布:犀牛软件反应迟钝 编辑:程序博客网 时间:2024/04/29 12:01

AppDelegate . m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        // 创建一个window对象, 大小和屏幕一样    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];        //距离等号左面 最近的.是 setter 方法 其他的是getter    //设置window的背景颜色    self.window.backgroundColor = [UIColor grayColor];        //设置window为主window, 并且可见    [self.window makeKeyAndVisible];        //内存管理    [_window release];

/* UIView */

UIViewiOS中所有控件类的基类是屏幕上的一块矩形区域 

学习一个新类:

1.看头文件, 看继承关系

2.找有没有自己的初始化方法

3.看属性列表


    //1. 创建一个view, 指定view在父视图上的位置和大小    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];        //2.对view进行相关的设置    view.backgroundColor = [UIColor purpleColor];        //3. 添加到父视图上    [self.window addSubview:view];        //4.内存管理    [view release];

 UIView的层级关系

1.任何一个view只能有一个父视图,但是可以有很多个子视图

2.父视图对任何子视图都有所有权关系 , 添加子视图的时候,会对子视图对象的引用计数+1

3.父视图和子视图的概念是相对而言的

4.所有的子视图的位置都是以父视图的坐标系做标准


/*  调整子视图的顺序 */

     //1.将视图移动到最上端    [self.window bringSubviewToFront:view];        //2.将视图移到最下面    [self.window sendSubviewToBack:view];        //3.两个视图交换    [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];


/*  UIView 属性方法 */

    //子视图自己从父视图移除(子视图调用)    [view removeFromSuperview];        // frame 一个view相对于父视图的位置和大小    view.frame = CGRectMake(100, 100, 200, 200);        // center : 一个view的中心点相对于父视图的位置    view.center = CGPointMake(200, 200);        // bounds: 一个view相对于自己的坐标系的位置和大小        //alpha 是一个0-1 的数, 代表view的透明度, 会影响所有的子视图    view.alpha = 0.5;    self.window.alpha = 1;        //视图是否隐藏掉    view.hidden = YES;        //标记一个view, 方便父视图快速的找到子视图    view.tag = 10000;        //父视图通过tag找子视图    UIView *searchView = [self.window viewWithTag:10000];


/* UILabel */


    //UILabel 可以显示文字 UIView的子类    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, 310, 100)];    label.backgroundColor = [UIColor whiteColor];        //显示文字    label.text = @"小苹果";        //字体颜色    label.textColor =[UIColor redColor];        //改变字体大小 系统默认17号字    label.font = [UIFont systemFontOfSize:30];        //文本对齐方式 中间对齐    label.textAlignment = NSTextAlignmentCenter;        //字体阴影颜色    label.shadowColor = [UIColor blueColor];        //阴影的偏移量    label.shadowOffset = CGSizeMake(2, 2);        //如果文本过长, 文本的断行模式    //枚举  mode type option style     label.lineBreakMode = NSLineBreakByTruncatingHead;        // 显示的行数 断行从第二行开始    label.numberOfLines = 2;        //!!!!!(常用)当设置为0的时候, label会根据自己的frame大小, 自己设置行数    label.numberOfLines = 0;        //让label自己适应内容的大小    [label sizeToFit];        [self.window addSubview:label];        [label release];


0 0
原创粉丝点击