forked from youngsoft/MyLinearLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFOLTest6ViewController.m
369 lines (296 loc) · 14 KB
/
FOLTest6ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//
// FOLTest6ViewController.m
// MyLayout
//
// Created by oybq on 16/2/19.
// Copyright © 2016年 YoungSoft. All rights reserved.
//
#import "FOLTest6ViewController.h"
#import "MyLayout.h"
#import "CFTool.h"
@interface FOLTest6ViewController ()
@end
@implementation FOLTest6ViewController
-(void)loadView
{
/**
* 这个例子用浮动布局来实现各种用户个人信息的页面布局,当然浮动布局不是万能的,这里只是为了强调用浮动布局实现的机制。
* 在很多界面中,有人诟病用线性布局来实现时会要嵌套很多的子布局,因此在一些场景中其实您不必全部都用线性布局,可以用浮动布局、流式布局或者相对布局来实现一些复杂一点的界面布局。
*/
UIScrollView *scrollView = [UIScrollView new];
self.view = scrollView;
MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
rootLayout.backgroundColor = [CFTool color:0];
rootLayout.myHorzMargin = 0;
rootLayout.heightSize.lBound(scrollView.heightSize,0,1); //默认虽然高度包裹,但是最小的高度要和滚动视图相等。
rootLayout.subviewVSpace = 10;
[scrollView addSubview:rootLayout];
//Create First User Profile type。
[self createUserProfile1Layout:rootLayout];
//Create Second User Profile type.
[self createUserProfile2Layout:rootLayout];
[self createUserProfile3Layout:rootLayout];
[self createUserProfile4Layout:rootLayout];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark -- Layout Construction
-(void)createUserProfile1Layout:(MyLinearLayout*)rootLayout
{
//这个例子建立一个只向上浮动的浮动布局视图,注意这里要想布局高度由子视图包裹的话则必须要设置布局视图的高度自适应。
//当在水平浮动布局中设置高度自适应,子视图不能逆向浮动只能向上浮动,并且子视图的weight不能设置为非0.
//当在水平浮动布局中设置高度自适应,需要我们设置某个子视图的clearFloat为YES进行手动换行处理。
MyFloatLayout *contentLayout = [MyFloatLayout floatLayoutWithOrientation:MyOrientation_Horz];
contentLayout.backgroundColor = [UIColor whiteColor];
contentLayout.myHeight = MyLayoutSize.wrap;
contentLayout.myHorzMargin = 0;
contentLayout.padding = UIEdgeInsetsMake(5, 5, 5, 5);
contentLayout.subviewHSpace = 5;
contentLayout.subviewVSpace = 5;
[rootLayout addSubview:contentLayout];
UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head1"]];
headImageView.mySize = CGSizeMake(40, 40); //头像部分固定尺寸。。
[contentLayout addSubview:headImageView];
UILabel *nameLabel = [UILabel new];
nameLabel.text = @"欧阳大哥";
nameLabel.font = [CFTool font:17];
nameLabel.textColor = [CFTool color:4];
nameLabel.clearFloat = YES; //注意这里要另外起一行。
nameLabel.widthSize.equalTo(contentLayout.widthSize).add(-45); //40的头像宽度外加5的左右间距。
[nameLabel sizeToFit];
[contentLayout addSubview:nameLabel];
UILabel *nickNameLabel = [UILabel new];
nickNameLabel.text = @"醉里挑灯看键";
nickNameLabel.font = [CFTool font:15];
nickNameLabel.textColor = [CFTool color:3];
[nickNameLabel sizeToFit];
[contentLayout addSubview:nickNameLabel];
UILabel *addressLabel = [UILabel new];
addressLabel.text = @"联系地址:中华人民共和国北京市朝阳区盈科中心B座2楼,其他的我就不会再告诉你了。";
addressLabel.font = [CFTool font:15];
addressLabel.textColor = [CFTool color:4];
addressLabel.heightSize.equalTo(@(MyLayoutSize.wrap));
addressLabel.widthSize.equalTo(contentLayout.widthSize).add(-45); //40的头像宽度外加5的左右间距。
[addressLabel sizeToFit];
[contentLayout addSubview:addressLabel];
UILabel *qqlabel = [UILabel new];
qqlabel.text = @"QQ群:178573773";
qqlabel.textColor = [CFTool color:4];
qqlabel.font = [CFTool font:15];
[qqlabel sizeToFit];
[contentLayout addSubview:qqlabel];
UILabel *githublabel = [UILabel new];
githublabel.text = @"github: https://github.com/youngsoft";
githublabel.font = [CFTool font:15];
githublabel.textColor = [CFTool color:9];
githublabel.widthSize.equalTo(contentLayout.widthSize).add(-45); //40的头像宽度外加5的左右间距。
githublabel.adjustsFontSizeToFitWidth = YES;
[githublabel sizeToFit];
[contentLayout addSubview:githublabel];
UILabel *detailLabel = [UILabel new];
detailLabel.text = @"坚持原创,以造轮子为乐!";
detailLabel.textColor = [CFTool color:2];
detailLabel.font = [CFTool font:20];
detailLabel.widthSize.equalTo(contentLayout.widthSize).add(-45); //40的头像宽度外加5的左右间距。
detailLabel.adjustsFontSizeToFitWidth = YES;
[detailLabel sizeToFit];
[contentLayout addSubview:detailLabel];
}
-(void)createUserProfile2Layout:(MyLinearLayout*)rootLayout
{
//浮动布局的一个缺点是居中对齐难以实现,所以这里需要对子视图做一些特殊处理.注意这里weight的使用。
MyFloatLayout *contentLayout = [MyFloatLayout floatLayoutWithOrientation:MyOrientation_Vert];
contentLayout.backgroundColor = [UIColor whiteColor];
contentLayout.myHeight = MyLayoutSize.wrap;
contentLayout.myHorzMargin = 0;
contentLayout.padding = UIEdgeInsetsMake(5, 5, 5, 5);
[rootLayout addSubview:contentLayout];
UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minions4"]];
headImageView.contentMode = UIViewContentModeCenter;
headImageView.weight = 1; //占据全部宽度。
headImageView.heightSize.equalTo(@80);
[contentLayout addSubview:headImageView];
UILabel *nameLabel = [UILabel new];
nameLabel.text = @"欧阳大哥";
nameLabel.font = [CFTool font:17];
nameLabel.textColor = [CFTool color:4];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.weight = 1;
nameLabel.myTop = 10;
[nameLabel sizeToFit];
[contentLayout addSubview:nameLabel];
UILabel *nickNameLabel = [UILabel new];
nickNameLabel.text = @"醉里挑灯看键";
nickNameLabel.font = [CFTool font:15];
nickNameLabel.textColor = [CFTool color:3];
nickNameLabel.textAlignment = NSTextAlignmentCenter;
nickNameLabel.weight = 1;
nickNameLabel.myTop = 5;
nickNameLabel.myBottom = 15;
[nickNameLabel sizeToFit];
[contentLayout addSubview:nickNameLabel];
NSArray *images = @[@"section1",@"section2", @"section3"];
NSArray *menus = @[@"Followers",@"Starred", @"Following"];
NSArray *values = @[@"140",@"5",@"0"];
//三个小图标均分宽度。
for (int i = 0; i < 3; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:images[i]]];
imageView.contentMode = UIViewContentModeCenter;
imageView.heightSize.equalTo(@20);
imageView.weight = 1.0/ (3 - i); //这里三个,第一个占用全部的1/3,第二个占用剩余的1/2,第三个占用剩余的1/1。这样就实现了均分宽度的效果。
[contentLayout addSubview:imageView];
}
//文本均分宽度
for (int i = 0; i < 3; i++)
{
UILabel *menuLabel = [UILabel new];
menuLabel.text = menus[i];
menuLabel.textColor = [CFTool color:2];
menuLabel.font = [CFTool font:14];
menuLabel.textAlignment = NSTextAlignmentCenter;
menuLabel.adjustsFontSizeToFitWidth = YES;
menuLabel.weight = 1.0/ (3 - i);
menuLabel.myTop = 10;
[menuLabel sizeToFit];
[contentLayout addSubview:menuLabel];
}
//文本均分宽度。
for (int i = 0; i < 3; i++)
{
UILabel *valueLabel = [UILabel new];
valueLabel.text = values[i];
valueLabel.textColor = [CFTool color:2];
valueLabel.font = [CFTool font:14];
valueLabel.textAlignment = NSTextAlignmentCenter;
valueLabel.adjustsFontSizeToFitWidth = YES;
valueLabel.weight = 1.0/ (3 - i);
valueLabel.myTop = 5;
[valueLabel sizeToFit];
[contentLayout addSubview:valueLabel];
}
}
-(void)createUserProfile3Layout:(MyLinearLayout*)rootLayout
{
//这个例用了viewLayoutCompleteBlock来实现一些特殊化处理。
MyFloatLayout *contentLayout = [MyFloatLayout floatLayoutWithOrientation:MyOrientation_Horz];
contentLayout.backgroundColor = [UIColor whiteColor];
contentLayout.myHeight = MyLayoutSize.wrap;
contentLayout.myHorzMargin = 0;
contentLayout.padding = UIEdgeInsetsMake(5, 5, 5, 5);
[rootLayout addSubview:contentLayout];
UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minions4"]];
headImageView.mySize = CGSizeMake(80, 80);
headImageView.myTrailing = 10;
headImageView.layer.cornerRadius = 5;
headImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
headImageView.layer.borderWidth = 0.5;
[contentLayout addSubview:headImageView];
headImageView.viewLayoutCompleteBlock = ^(MyBaseLayout *layout, UIView *sbv)
{
//这里我们建立一个特殊的子视图,我们可以在这个block里面建立子视图,不会影响到这次的子视图的布局。
UILabel *label = [UILabel new];
label.useFrame = YES; //这里我们设置useFrame为YES表示他不会参与布局,这样这个视图就可以摆脱浮动布局视图的约束。
label.text = @"99";
label.font = [CFTool font:12];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [CFTool color:2];
[label sizeToFit]; //这里内部会设置frame值。
label.layer.cornerRadius = label.frame.size.width/2;
label.layer.masksToBounds = YES;
CGRect labelRect = label.frame;
labelRect.origin.x = CGRectGetMaxX(sbv.frame) - label.frame.size.width / 2;
labelRect.origin.y = CGRectGetMinY(sbv.frame) - label.frame.size.height / 2;
label.frame = labelRect;
[layout addSubview:label];
};
UILabel *nameLabel = [UILabel new];
nameLabel.text = @"欧阳大哥";
nameLabel.font = [CFTool font:17];
nameLabel.textColor = [CFTool color:4];
nameLabel.clearFloat = YES;
[nameLabel sizeToFit];
[contentLayout addSubview:nameLabel];
UILabel *nickNameLabel = [UILabel new];
nickNameLabel.text = @"醉里挑灯看键";
nickNameLabel.font = [CFTool font:15];
nickNameLabel.textColor = [CFTool color:3];
[nickNameLabel sizeToFit];
[contentLayout addSubview:nickNameLabel];
UILabel *detailLabel = [UILabel new];
detailLabel.text = @"坚持原创,以造轮子为乐!";
detailLabel.textColor = [CFTool color:2];
detailLabel.font = [CFTool font:20];
detailLabel.adjustsFontSizeToFitWidth = YES;
[detailLabel sizeToFit];
[contentLayout addSubview:detailLabel];
}
-(void)createUserProfile4Layout:(MyLinearLayout*)rootLayout
{
MyFloatLayout *contentLayout = [MyFloatLayout floatLayoutWithOrientation:MyOrientation_Vert];
contentLayout.backgroundColor = [UIColor whiteColor];
contentLayout.myHeight = MyLayoutSize.wrap;
contentLayout.myHorzMargin = 0;
contentLayout.padding = UIEdgeInsetsMake(5, 5, 5, 5);
[rootLayout addSubview:contentLayout];
UIImageView *headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minions4"]];
headImageView.mySize = CGSizeMake(80, 80);
headImageView.myTrailing = 10;
headImageView.layer.cornerRadius = 5;
headImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
headImageView.layer.borderWidth = 0.5;
[contentLayout addSubview:headImageView];
UILabel *editButton = [UILabel new];
editButton.text = @"Edit";
editButton.textAlignment = NSTextAlignmentCenter;
editButton.backgroundColor = [CFTool color:5];
editButton.textColor = [CFTool color:4];
editButton.layer.cornerRadius = 5;
editButton.layer.masksToBounds = YES;
editButton.widthSize.equalTo(@(MyLayoutSize.wrap)).add(20);
editButton.heightSize.equalTo(@(MyLayoutSize.wrap)).add(4);
editButton.reverseFloat = YES;
[contentLayout addSubview:editButton];
UILabel *nameLabel = [UILabel new];
nameLabel.text = @"欧阳大哥";
nameLabel.font = [CFTool font:17];
nameLabel.textColor = [CFTool color:4];
nameLabel.weight = 1;
[nameLabel sizeToFit];
[contentLayout addSubview:nameLabel];
UIImageView *nickNameImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"edit"]];
[nickNameImageView sizeToFit];
nickNameImageView.myTop = 5;
[contentLayout addSubview:nickNameImageView];
UILabel *nickNameLabel = [UILabel new];
nickNameLabel.text = @"醉里挑灯看键";
nickNameLabel.font = [CFTool font:15];
nickNameLabel.textColor = [CFTool color:3];
[nickNameLabel sizeToFit];
nickNameLabel.myTop = 5;
[contentLayout addSubview:nickNameLabel];
UILabel *detailLabel = [UILabel new];
detailLabel.text = @"坚持原创,以造轮子为乐!";
detailLabel.textColor = [CFTool color:2];
detailLabel.font = [CFTool font:20];
detailLabel.adjustsFontSizeToFitWidth = YES;
[detailLabel sizeToFit];
detailLabel.weight = 1;
detailLabel.clearFloat = YES;
detailLabel.myTop = 5;
[contentLayout addSubview:detailLabel];
}
@end