UI-Day01--UILabel和UIWindow(二)

第一部分: UIWindow

  在此一些没有经验的,会有个疑问,那就是自己该如何创建一个工程,由于某些原因,等明天再把具体步骤和截图补上

        1.创建完后的main函数

       

 1  return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
 2 
 3         //第三个参数,传入一个类名(Class),这个类必须是UIApplication类的子类,这个函数UIApplicationMain     
他会创建UIApplication子类的对象,如果传nil,只创建UIApplication类的对象。
4 5 //UIApplication对象不断检测应用程序的当前状态。当操作系统修改了应用程序的当前状态,会发送通知,UIApplication会检测到这个通知。 9 //第四个参数,传入代理类的名字,通过名字,可以获得Class,可以创建一个代理对象。 10 11 //AppDelegate是遵从协议的类,会创建出遵从协议的对象,是UIApplication对象的代理. 15 //当UIApplication对象检测到当前程序状态改变,委托Delegate对象调用协议上有的方法,处理问题。

 

第二部分:AppDelegate.m的六种方法

 

重点是第一种方法

 

No.1 UIWindow

 

//这个方法是整个APP的主入口  UI的入口 UIApplication会委托AppDelegate执行这个方法

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 2 
 3    
 4 
 5     //UI代码的实现
 6 
 7     //UIWindow 程序的窗体 一般情况下一个程序最好只有一个窗体
 8 
 9     //UIWindow的大小和手机屏幕的大小一致
10 
11     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
12 
13     //UIWindow用来展示UI或添加控件
14 
15     
16 
17     //设置UIWindow的背景颜色
18 
19     self.window.backgroundColor = [UIColor whiteColor];
20 
21     //UIColor 颜色的类  whiteColor 创建一个白色对象

 

    

 

No.2 UIView

 

 

 1   //iOS -->入门控件  所有看得见的控件的基类
 2 
 3     UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 130)];
 4 
 5     //创建UIView对象  给一个矩形尺寸的大小
 6 
 7     //坐标原点:(手机屏幕的左上角:x横坐标-0,y纵坐标-0,width:手机屏幕的宽度:320,height屏幕高度:480 -4s/568-5s)
 8 
 9     //手机状态栏高度20
10 
11     /*
12 
13      struct CGRect {
14 
15         CGPoint origin;
16 
17         CGSize size;
18 
19      };
20 
21      
22 
23      struct CGSize {
24 
25         CGFloat width;
26 
27         CGFloat height;
28 
29      };
30 
31      struct CGPoint {
32 
33         CGFloat x;
34 
35         CGFloat y;
36 
37      };
38 
39      */
40 
41  
42 
43  
44 
45           //设置视图的底层颜色
46 
47     view.backgroundColor = [UIColor redColor];
48 
49     
50 
51     //在widow上添加子试图,添加子试图会给view引用计数+1,子试图的生命周期交给window管理
52 
53           //【注】子视图的概念要跟子类的概念区分开
54 
55  
56 
57     [self.window addSubview:view];
58 
59 //    [view relese];
60 
61  
62 
63     //让UIWindow可见
64 
65     [self.window makeKeyAndVisible];
66 
67     
68 
69     //command + r 运行
70 
71     
72 
73     //NSLog打印,不会打印到屏幕上,打印到debug区
74 
75     NSLog(@"程序已经被载入");
76 
77     
78 
79     return YES;
80 
81 }

 

 

 

No.3 UILabel

 

  1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2 
  3     
  4 
  5  
  6 
  7     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
  8 
  9     self.window.backgroundColor = [UIColor whiteColor];
 10 
 11     
 12 
 13  
 14 
 15     //UILabel 它的父类是UIView  用来显示文字的控件,【注】不能对文字(内容)进行编辑
 16 
 17     UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 220, 30)];
 18 
 19     label1.backgroundColor = [UIColor redColor];
 20 
 21  
 22 
 23     //01.通过RGB调配颜色
 24 
 25     //02.alpha是字体透明度的关键字,0.5为半透明,0为全透明
 26 
 27     label1.backgroundColor = [UIColor colorWithRed:0.2 green:0.4 blue:0.4 alpha:1];
 28 
 29  
 30 
 31  
 32 
 33     //03.显示文字内容
 34 
 35     label1.text = @"我是彪签1";
 36 
 37     
 38 
 39     [self.window addSubview:label1];
 40 
 41     
 42 
 43     
 44 
 45     UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 70, 220, 30)];
 46 
 47     label2.backgroundColor = [UIColor purpleColor];
 48 
 49     label2.text = @"俺是彪签2";
 50 
 51     //04.设置文字的颜色
 52 
 53     label2.textColor = [UIColor redColor];
 54 
 55     //05.设置文字的对齐方式  居中对齐NSTextAlignmentCenter
 56 
 57     label2.textAlignment = NSTextAlignmentCenter;
 58 
 59     
 60 
 61     [self.window addSubview:label2];
 62 
 63     
 64 
 65     
 66 
 67     UILabel * label3 = [[UILabel alloc]initWithFrame:CGRectMake(10, 110, 280, 30)];
 68 
 69     label3.backgroundColor = [UIColor yellowColor];
 70 
 71     label3.textColor = [UIColor blueColor];
 72 
 73  
 74 
 75     //06.右对齐
 76 
 77     label3.textAlignment = NSTextAlignmentRight;
 78 
 79  
 80 
 81  UIFont 字体类
 82     //01.设置字体的大小
 83     label3.font = [UIFont systemFontOfSize:40]; 84 
 85  
 86 
 87     //02.设置文字阴影
 88 
 89     label3.shadowColor = [UIColor redColor];
 90 
 91  
 92 
 93     //03.设置阴影的偏移量
 94 
 95     label3.shadowOffset = CGSizeMake(-5, -5);
 96 
 97     label3.text = @"偶是彪签3";
 98 
 99     
100 
101     [self.window addSubview:label3];
102 
103     
104 
105  
106 
107 //当写入字符特别长的情况
108 
109     UILabel * label4 = [[UILabel alloc]initWithFrame:CGRectMake(10, 150, 280, 130)];
110 
111     label4.backgroundColor = [UIColor greenColor];
112 
113     label4.textColor = [UIColor blackColor];
114 
115  
116 
117     label4.text = @"我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签,我们是彪签";
118 
119     //label4.font = [UIFont systemFontOfSize:20];
120 
121     //label4.font = [UIFont boldSystemFontOfSize:20];
122 
123     label4.font = [UIFont fontWithName:@"BodoniSvtyTwoOSITCTT-Bold" size:20];
124 
125  
126 
127     //省略头部分:如果文字超出 NSLineBreakByTruncatingHead省略头部分.....ABC
128 
129     //省略尾部分:NSLineBreakByTruncatingTail abc.....
130 
131     //省略中间部分:NSLineBreakByTruncatingMiddle ab....db
132 
133  
134 
135     label4.lineBreakMode = NSLineBreakByTruncatingHead;
136 
137  
138 
139     //不限制行数 ,可以显示任意行
140 
141     label4.numberOfLines = 0;
142 
143     //透明度
144 
145     label4.alpha = 0.8;
146 
147     //[UIColor clearColor];//透明
148 
149     //自适应高度(根据label内容)
150 
151     [label4 sizeToFit];
152 
153     
154 
155     //字体族
156 
157     NSArray * familyArr = [UIFont familyNames];
158 
159     for (NSString * name in familyArr) {
160 
161         NSArray * names = [UIFont fontNamesForFamilyName:name];
162 
163         for (NSString * str in names) {
164 
165             NSLog(@"%@",str);
166 
167         }
168 
169     }
170 
171     
172 
173     
174 
175     [self.window addSubview:label4];
176 
177  
178 
179  
180 
181     
182 
183     [self.window makeKeyAndVisible];
184 
185     return YES;
186 
187 }

 

 

 

No.4 SencondLabel

 此处功能实现的是设置一个标签(几个字的文字的 UILabel),然后碰到屏幕边后会进行反弹,而不出界

  1 @implementation AppDelegate
  2 
  3 {
  4 
  5     NSTimer * _timer;//定时器
  6 
  7 }
  8 
  9  
 10 //入口
 11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 12 
 13     
 14 
 15     
 16 
 17     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
 18 
 19     self.window.backgroundColor = [UIColor greenColor];
 20 
 21  
 22 
 23     //01.创建label   调用自定义创建label方法(方法在下边单独列出createLabel是方法名)
 24 
 25     [self createLabel];
 26 
 27     
 28 
 29     
 30 
 31     //02.创建即启动定时器
 32 
 33     _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(everyTime) userInfo:nil repeats:YES];
 34 
 35     //第一个参数:传入时间:秒
 36 
 37     //每隔一段时间会把selector发送给target,即每隔一次传入时间,self便会调用selector方法一次
 38 
 39     //第四个参数:传nil
 40 
 41     //第五个参数:是否重复调用,如果传NO,只调用一次
 42 
 43     
 44 
 45     [self.window makeKeyAndVisible];
 46 
 47     return YES;
 48 
 49 }
 50 
 51  
 52 
 53  
 54 
 55 //创建label方法
 56 
 57 -(void)createLabel{
 58 
 59     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180, 30)];
 60 
 61     label.text = @"我是会飞的彪签";
 62 
 63     label.backgroundColor = [UIColor clearColor];
 64 
 65     label.textColor = [UIColor colorWithRed:0.3 green:0.4 blue:0.3 alpha:1];
 66 
 67  
 68 
 69     //标记 笔记不能重复  标记最好不要是个位数
 70 
 71     label.tag = 11;
 72 
 73  
 74 
 75     [label sizeToFit];//自动适应屏幕显示
 76 
 77  
 78 
 79     //添加到window上
 80 
 81     [self.window addSubview:label];
 82 
 83 }
 84 
 85  
 86 
 87 //自定义传入事件方法
 88 
 89 -(void)everyTime{
 90 
 91     
 92 
 93     UILabel * label = (UILabel *)[self.window viewWithTag:11];//标签
 94 
 95     static NSInteger _x = 1,_y = 1;
 96 
 97     
 98 
 99     CGRect rect = label.frame;
100 
101     //rect的x坐标和y坐标每次加1
102 
103     rect.origin.x += _x;//0 + 1 1 = 1 -1
104 
105     rect.origin.y += _y;
106 
107     
108 
109     //左右边界
110 
111     if (rect.origin.x > 320 - label.frame.size.width || rect.origin.x <= 0) {
112 
113         _x *= -1;
114 
115     }
116 
117     //上下边界
118 
119     if (rect.origin.y > self.window.frame.size.height - label.frame.size.height || rect.origin.y <= 0) {
120 
121         _y *= -1;
122 
123     }
124 
125     //变化后的frame赋给label
126 
127     label.frame = rect;
128 
129 }

 

东西写的有点多

 

转载于:https://www.cnblogs.com/IOS-BUDO/p/4774337.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值