label的核心代码
- (UILabel *)labelWithTitle:(NSString *)title{
UILabel *label = [[UILabel alloc] init];
label.text = title;
[label sizeToFit];
label.layer.cornerRadius = 3;
label.layer.borderWidth = 0.5;
label.userInteractionEnabled = YES;
label.font = [UIFont systemFontOfSize:13];
label.textAlignment = NSTextAlignmentCenter;
return label;
}
布局的核心代码
-(void)setupLayoutViews{
CGFloat x = 15;
CGFloat y = 10 + 40;
NSArray *titles = @[@"苹果",@"桃子",@"梨子",@"橘子",@"葡萄"];
for (int i = 0; i < titles.count; i++) {
UILabel *label = [self labelWithTitle:titles[I]];
CGFloat width = label.frame.size.width + 30;
if (x + width + 15 > self.bounds.size.width) {
y += 40;//换行
x = 15; //15位置开始
}
label.frame = CGRectMake(x, y, width, 30);
[self addSubview:label];
x += width + 10;//宽度+间隙
}
}
展示图
展示-图
流程图
流程-图
PS:初始化一个变量x
然后x叠加 (宽+间隙: x += width + 10),
如果x叠加的宽度大于当前容器视图的宽度则:
y向下移动(高度+间隙) ,并且让x从初始值(15)开始
网友评论