AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)

一.什么是VFL语言?为什么要VFL语言?
VFL全称是Visual Format Language,翻译过来是“可视化格式语言”
VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言
代码分析:
    NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:<#(NSString *)#> options:<#(NSLayoutFormatOptions)#> metrics:<#(NSDictionary *)#> views:<#(NSDictionary *)#>]
VisualFormat:VFL语句
options:约束类型
metrics :VFL语句中用到的具体数值
views :VFL语句中用到的控件
 
二.例子
1.简单VFL
 - (void)viewDidLoad{
2 [super viewDidLoad];
3 UIView *redView = [[UIView alloc]init];
4 redView.backgroundColor = [UIColor redColor];
5 redView.translatesAutoresizingMaskIntoConstraints= NO;
[self.view addSubview:redView];
     UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints= NO;
[self.view addSubview:blueView];
//水平方向
NSString *hVFL=@"H:|-20-[redView]-30-[blueView(==redView)]-20-|";
NSArray *hCons =[NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:@{@"redView":redView,@"blueView":blueView}];
[self.view addConstraints:hCons];
//垂直方向
NSString *vVFL =@"V:|-20-[redView(50)]";
NSArray *vCons =[NSLayoutConstraint constraintsWithVisualFormat:vVFL options: metrics:nil views:@{@"redView":redView}];
[self.view addConstraints:vCons];
}

效果:

AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)

2. 复杂VFL无法完整表示 必须结合 constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:. 一起使用

 - (void)viewDidLoad{
2 [super viewDidLoad];
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints= NO;
[self.view addSubview:blueView];
     UIView *redView = [[UIView alloc]init];
redView.backgroundColor = [UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints= NO;
[self.view addSubview:redView];
//水平方向
NSString *hVFL =@"H:|-30-[blueView]-30-|";
NSArray *hCons = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options: metrics:nil views:@{@"blueView":blueView}];
[self.view addConstraints:hCons];
//垂直方向
NSString *vVFL =@"V:|-30-[blueView(50)]-20-[redView(==blueView)]";
NSArray *vCons = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView":blueView,@"redView":redView}];
[self.view addConstraints:vCons]; //VFL无法描述 redView 左边 和 blueViwe 中心对齐
NSLayoutConstraint *redLeftCon = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[self.view addConstraint:redLeftCon];
}

//注意 :官方文档中
The notation prefers good visualization over completeness of expressibility. Most of the constraints that are useful in real user interfaces can be expressed using visual format syntax, but there are a few that cannot. One useful constraint that cannot be expressed is a fixed aspect ratio (for example, imageView.width = 2 * imageView.height). To create such a constraint, you must use

constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.

效果:

AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)

上一篇:禁止用户使用密码的方式登录服务器,让其使用key的方式登录


下一篇:【C语言】03-第一个C程序代码分析