一个正在开发中的项目,旨在为 iOS 中的视图添加布局机制(类似于 Android)。当您需要在滚动视图中显示大量数据时,这是很有用的,例如,无需根据前一个元素的位置进行调整所有框架。可用的布局助手有:UVerticalLayout 和 UIHorizontalLayout。在最新版本中,您可以将布局的内容放入一个块中,从而更好地从代码中查看布局(它没有其他目的)。
简单地复制
到您的项目文件夹中,并导入 KALayoutHelper.h
或使用此行 cocoapod:pod 'KALayoutHelper', :git => 'https://github.com/kirualex/KALayoutHelper.git'
__block UIVerticalLayout * blockVertical = verticalLayout;
[verticalLayout setContentBlock:^{
// Label
UILabel * labelV = [[UILabel alloc] init];
labelV.font = [UIFont boldSystemFontOfSize:18];
labelV.text = @"Verticaly";
[blockVertical addSubview:labelV withPadding:20];
// TextViews
UITextView * textV1 = [[UITextView alloc] init];
textV1.text = @"Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores?";
[blockVertical addSubview:textV1 withPadding:10];
UITextView * textV2 = [[UITextView alloc] init];
textV2.text = @"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium !";
[blockVertical addSubview:textV2 withPadding:10];
// Images
UIImageView * imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star"]];
[blockVertical addSubview:imageView1 withPadding:10 andHorizontalAlignment:KALayoutHorizontalAlignmentLeft];
UIImageView * imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star"]];
[blockVertical addSubview:imageView2 withPadding:10 andHorizontalAlignment:KALayoutHorizontalAlignmentCenter];
UIImageView * imageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star"]];
[blockVertical addSubview:imageView3 withPadding:10 andHorizontalAlignment:KALayoutHorizontalAlignmentRight];
// Label
UILabel * labelV2 = [[UILabel alloc] init];
labelV2.font = [UIFont boldSystemFontOfSize:18];
labelV2.text = @"Horizontaly";
[blockVertical addSubview:labelV2 withPadding:10];
// HORIZONTAL ------------------------------------------------------------------------------
// Init horizontal layout (frame will be set automatically)
// You just need to set the correct sizes for subview width
UIHorizontalLayout * horizontalLayout = [[UIHorizontalLayout alloc] init];
[horizontalLayout setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1]];
__block UIHorizontalLayout * blockHorizontal = horizontalLayout;
[horizontalLayout setContentBlock:^{
// TextViews
UITextView * textH1 = [[UITextView alloc] initWithFrame:CGRectMake(0,0,145,0)];
[textH1 setBackgroundColor:[UIColor clearColor]];
textH1.text = @"Iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veriticabo.";
[blockHorizontal addSubview:textH1];
UITextView * textH2 = [[UITextView alloc] initWithFrame:CGRectMake(0,0,145,0)];
[textH2 setBackgroundColor:[UIColor clearColor]];
textH2.text = @"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium atae vitae dicta sunt explicabo.";
[blockHorizontal addSubview:textH2 withPadding:10];
}];
[blockVertical addSubview:horizontalLayout withPadding:10];
}];
UIHorizontalLayout * horizontalLayout2 = [[UIHorizontalLayout alloc] init];
__block UIHorizontalLayout * blockHorizontal2 = horizontalLayout2;
[horizontalLayout2 setContentBlock:^{
// TextViews
UITextView * textH3 = [[UITextView alloc] initWithFrame:CGRectMake(0,0,93,0)];
textH3.text = @"Iste natus error sit voluptatem , eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.";
textH3.backgroundColor = [UIColor redColor];
[blockHorizontal2 addSubview:textH3 withPadding:0 andVerticalAlignment:KALayoutVerticalAlignmentCenter];
UITextView * textH4 = [[UITextView alloc] initWithFrame:CGRectMake(0,0,93,0)];
textH4.text = @"Sed ut perspiciatis unde omnis iste accusantium doloremque laudantium, totam rem aperiam natus error sit voluptatem accusantium doloremque o.";
[blockHorizontal2 addSubview:textH4 withPadding:10];
UITextView * textH5 = [[UITextView alloc] initWithFrame:CGRectMake(0,0,93,0)];
textH5.text = @"Sed ut perspiciatis unde omnis iste accusantium doloremque laudantium, totam rem aperiam natus error sit voluptatem accusantium doloremque laudantium, totam.";
[blockHorizontal2 addSubview:textH5 withPadding:10];
}];