UI - UIViewAndUILabel

来源:互联网 发布:如何删除mac用户和组群 编辑:程序博客网 时间:2024/05/14 14:04

<AppDelegate.m>

#import "AppDelegate.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    //设置 window 的背景颜色    self.window.backgroundColor = [UIColor cyanColor];    //自己调配颜色,需要设置红,绿,蓝,透明度,值在0 ~ 1 之间    self.window.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0.5 alpha:0.8];        //将 window 设置为主 window 并且显示    [self.window makeKeyAndVisible];   /*        //黄色视图    //1.创建控件对象    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 220, 100)];    //2.配置属性    yellowView.backgroundColor = [UIColor yellowColor];    //3.添加到父视图    [self.window addSubview:yellowView];    //4.释放所有权    [yellowView release];            //绿色视图    //创建控件对象    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 220, 100)];    //配置属性    greenView.backgroundColor = [UIColor greenColor];    //添加到父视图    [self.window addSubview:greenView];    //释放所有权    [greenView release];        //红色视图    //创建控件对象    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(50, 350, 220, 100)];    //配置属性    redView.backgroundColor  = [UIColor redColor];    //添加到父视图    [self.window addSubview:redView];    //释放所有权    [redView release];        //frame 用来控制视图的位置和大小 注意位置是相对于它的父视图来说的        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];    whiteView.backgroundColor = [UIColor whiteColor];    //添加到父视图//    [self.window addSubview:whiteView];    //以背景为父视图//    [greenView addSubview:whiteView];      //以绿色为父视图    [redView addSubview:whiteView];          //以红色为父视图    [whiteView release];            //center 代表视图的中心点,一般用来控制视图位置  注意:相对于父视图来说的        NSLog(@"%@",NSStringFromCGPoint(whiteView.center));        whiteView.center = CGPointMake(50, 50);        //bounds 是自身视图左上点相对于自身坐标系的位置,更改 bounds 位置坐标,不会对自身做出改变,但是能够改变子视图的位置(子视图的位置是相对于父视图的坐标系来说的)  我们一般使用 bounds 的结构体成员 size 更改视图的大小       UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];    blackView.backgroundColor = [UIColor blackColor];    [greenView addSubview:blackView];    [blackView release];        //更改 bounds    greenView.bounds = CGRectMake(30, 30, 220, 100);   //将会往左上移            */    //===================================================================================       /*   多层视图比较            //aView    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];    aView.backgroundColor = [UIColor redColor];    [self.window addSubview:aView];    [aView release];        //bView    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(50, 0, 100, 400)];    bView.backgroundColor = [UIColor orangeColor];    [self.window addSubview:bView];    [bView release];        //cView    UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(100, 120, 200, 150)];    cView.backgroundColor = [UIColor yellowColor];    [self.window addSubview:cView];    [cView release];        //dView    UIView *dView = [[UIView alloc] initWithFrame:CGRectMake(100, 80, 100, 100)];    dView.backgroundColor = [UIColor greenColor];    [self.window insertSubview:dView atIndex:1];    [dView release];        //eView    UIView *eView = [[UIView alloc] initWithFrame:CGRectMake(65, 50, 100, 100)];    eView.backgroundColor = [UIColor blueColor];    [self.window insertSubview:eView aboveSubview:bView];    [eView release];            //将橙色的bview 移动最前面    [self.window bringSubviewToFront:bView];    //将黄色的cview 移动到最后面    [self.window sendSubviewToBack:cView];    //将橙色的 bview 和黄色的 cview 交换位置    [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:4];    //把某个视图从父视图上移除    [eView removeFromSuperview];        //UIView    //1.创建对象    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 220, 468)];    //2.配置属性    //(1)设置背景颜色    view.backgroundColor = [UIColor grayColor];    //(2)设置显隐性    view.hidden = NO;   //即默认为 NO , 改为 YES 后变为不可见    //(3)设置透明度    view.alpha = 0.8;   //默认为 1 ,取值范围在 0 ~ 1.        //3.添加父视图    [self.window addSubview:view];        //(4)获取 superview    UIView *View_1 = [view superview];    NSLog(@"==%@==",View_1);    //(5)获取子视图    NSArray *subViews = [view subviews];    NSLog(@"==%@==",subViews);        //(6)设置 tag 值    view.tag = 108;          // 默认是NO,当设置为yes时,超出当前视图的尺寸的内容和子视图不会显示。与 layer 层的maskToBounds 的作用是类似的     view.clipsToBounds = YES;     //默认为No, exclusiveTouch的意义在于:     //如果当前设置了exclusiveTouch的UIView是整个触摸事件的第一响应者,那么到你所有的手指离开屏幕前其他的UIView是无法接受到整个事件周期内所有的触摸事件。     view.exclusiveTouch = No;     //默认为YES,表示当父视图尺寸改变时,子视图也会随着改变。     view.autoresizesSubviews = YES;     //默认为UIViewAutoresizingNone,不会自动伸缩。     view.autoresizingMask = UIViewAutoresizingNone;     //内容模式     view.contentMode = UIViewContentModeScaleToFill;(默认)         //4.释放所有权    [view release];        //通过 tag 值找到对应 view    UIView *view_2 = [self.window viewWithTag:108];    view_2.backgroundColor = [UIColor redColor];    view_2.alpha = 1.0;            */    //===================================================================================    //UILabel 是 UI 中常用的控件,它和 view 一样也表示一块矩形区域 相比较 view 来说仅仅是多了文字的管理功能 在创建上没有什么差别    //UILabel 在使用上也包括四步 : 1.创建对象  2.配置属性  3.添加父视图  4.释放所有权        //1.创建对象    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200 , 300)];    //2.配置属性    label.backgroundColor = [UIColor yellowColor];    //(2)设置显示的文字    label.text = @"新年快乐,心想事成,万事如意!!!876578324658234781687698796789870342343645767345260786239075734";    //(3)设置字体颜色    label.textColor = [UIColor redColor];    //(4)设置文字对齐方式    label.textAlignment = NSTextAlignmentCenter;    //(5)设置字体类型和字体大小    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];    label.font = [UIFont systemFontOfSize:21];  //默认的字体是17号字体    //(6)设置行数    label.numberOfLines = 3;   //输出3行,没显⽰的信息以省略号代替    label.numberOfLines = 0;   //默认为1 ,没显⽰的信息以省略号代替    //(7)设置文字阴影的大小    label.shadowOffset = CGSizeMake(2, 2);    //(8)设置文字阴影颜色    label.shadowColor = [UIColor grayColor];        //设置标签文字过长时的显示方式,这个属性使用于label中文本的换行和截短。    //首先numberofLines必须设置为0,才有效果。    label.lineBreakMode = NSLineBreakByCharWrapping;//以字符为显示单位显示,后面部分省略不显示。    label.lineBreakMode = NSLineBreakByClipping;//剪切与文本宽度相同的内容长度,后半部分被删除。    label.lineBreakMode = NSLineBreakByTruncatingHead;//前面部分文字以……方式省略,显示尾部文字内容。    label.lineBreakMode = NSLineBreakByTruncatingMiddle;//中间的内容以……方式省略,显示头尾的文字内容。    label.lineBreakMode = NSLineBreakByTruncatingTail;//结尾部分的内容以……方式省略,显示头的文字内容。    label.lineBreakMode = NSLineBreakByWordWrapping;//以单词为显示单位显示,后面部分省略不显示。    //设置文字内容是否可变,默认 yes    label.enabled = YES;    //文字内容自适应标签宽度,与minimumScaleFactor属性配合使用;默认 no, 固定字体大小,    label.adjustsFontSizeToFitWidth = YES;    //最小字体    label.minimumScaleFactor = 12;    //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为    label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;//    UIBaselineAdjustmentAlignBaselines=0;//默认,文本最上端与中线对齐。//    UIBaselineAdjustmentAlignCenters;   //文本中线与label中线对齐。//    UIBaselineAdjustmentNone  //文本最低端与label中线对齐。        //优先选择标签布局的最大宽度    label.preferredMaxLayoutWidth = 100;    //设置标签是否忽略或移除用户交互。默认为NO    label.userInteractionEnabled = YES;        //设置高亮颜色,与 highlighted属性 配合使用    label.highlightedTextColor = [UIColor redColor];//    label.highlighted = YES;    //设置文本阴影颜色    label.shadowColor = [UIColor grayColor];    //设置文本阴影与原文本的偏移量    label.shadowOffset = CGSizeMake(1.0, 5.0);        //3.添加父视图    [self.window addSubview:label];    //4.释放所有权    [label release];       //做UI的时候我们可能想给某个Label添加一个背景图片,但查看的时候会发现好像只有设置背景颜色的方法,不过我们也可以通过这种方式来解决:UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"itemkaung2.png"]];[myLabel setBackgroundColor:color];        //=========================创建登陆界面====================================              //创建背景视图     UIView *containterView = [[UIView alloc] initWithFrame:self.window.bounds];          //配置属性     containterView.backgroundColor = [UIColor grayColor];     //添加到父视图     [self.window addSubview:containterView];     //释放所有权     [containterView release];               //创建用户名及其输入的色块     UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 90, 50)];         nameLabel.text = @"用户名:";     nameLabel.textAlignment = NSTextAlignmentCenter;     [containterView addSubview:nameLabel];     [nameLabel release];          UIView *imputNameView = [[UIView alloc] initWithFrame:CGRectMake(130, 100, 170, 50)];     imputNameView.backgroundColor = [UIColor blueColor];     [containterView addSubview:imputNameView];     [imputNameView release];          //创建密码及其输入的色块     UILabel *passWordLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 170, 90, 50)];         passWordLabel.text = @"密码:";     passWordLabel.textAlignment = NSTextAlignmentCenter;     [containterView addSubview:passWordLabel];     [passWordLabel release];          UIView *imputPassWordView = [[UIView alloc] initWithFrame:CGRectMake(130, 170, 170, 50)];     imputPassWordView.backgroundColor = [UIColor blueColor];     [containterView addSubview:imputPassWordView];     [imputPassWordView release];          //创建其他字条     UIView *loginView = [[UIView alloc] initWithFrame:CGRectMake(10, 260, 90, 40)];     loginView.backgroundColor = [UIColor greenColor];     [containterView addSubview:loginView];     [loginView release];          UIView *findPassWordView = [[UIView alloc] initWithFrame:CGRectMake(115, 260, 90, 40)];     findPassWordView.backgroundColor = [UIColor greenColor];     [containterView addSubview:findPassWordView];     [findPassWordView release];          UIView *registeView = [[UIView alloc] initWithFrame:CGRectMake(220, 260, 90, 40)];     registeView.backgroundColor = [UIColor greenColor];     [containterView addSubview:registeView];     [registeView release];         //============================UIAlertView=====================================            //需要在 .h 中添加协议服从  <UIAlertViewDelegate>    UIAlertView *altertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"这是一个提示的消息" delegate: self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"忽略",@"更多" , nil];        [altertView show];        return YES;}//无论点击哪个选项,都会触发下面的方法- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    NSLog(@"%ld",buttonIndex);//取消的下标为0,剩下的从1 到 n 排列    }  - (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end





                                             
0 0