Introduction to the VFL language for iOS learning

Source: Internet
Author: User
Tags abstract language

What is the VFL language

The VFL (visual format Language), "Visual formatting language".

The VFL is an abstract language that Apple has introduced to simplify AutoLayout coding.

Syntax description
H:[cancelbutton (-[acceptbutton)-]cancelbutton width 72,acceptbutton width 50, The spacing between them is H:[wideview (>=)]wideview width is greater than or equal to 60point, the constraint priority is 700 (the priority is the maximum value is 1000, The higher the precedence of the constraint is satisfied first  v:[redbox][yellowbox (= =RedBox)] vertically, there is a redBox, immediately below a height equal to the redBox height of Yellowbox  H:|--[find]-[findnext]-[findfield (>=)]-| horizontally, the find distance from the left edge of the parent view is 10, followed by the FindNext distance from the Find interval default width, and then findfield with a width of not less than 20, which is the default width of FindNext and the right edge of the parent view. (vertical bar "|" Represents the edge of the Superview).
How to use
use the VFL to create an array of constraints + (Nsarray *) Constraintswithvisualformat: (NSString *) format options: (nslayoutformatoptions) OPTs metrics: (nsdictionary *) metrics Views: (Nsdictionary *) views;  FORMAT:VFL Statement opts: The control  used in the VIEWS:VFL statement of the specific value used in the constraint type METRICS:VFL statement Create a dictionary (internally containing the controls used in the VFL statement) to define the Nsdictionaryofvariablebindings (...) shortcut macro .
Example Show

As follows:

Implementation code:

-(void) horizontallayout{//1. Add two controlsUIView *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]; //2. Adding Constraints//2.1 Constraints in the horizontal directionNSString *HVFL =@"H:|-30-[blueview]-30-[redview (==blueview)]-30-|"; Nsarray*hcons = [Nslayoutconstraint CONSTRAINTSWITHVISUALFORMAT:HVFL options:nslayoutformatalignallbottom | Nslayoutformatalignalltop Metrics:nil views:@{@"Blueview": Blueview,@"Redview": Redview}];        [Self.view addconstraints:hcons]; //2.2 Constraints in the vertical directionNSString *VVFL =@"V:[blueview (]-30-|)"; Nsarray*vcons = [Nslayoutconstraint CONSTRAINTSWITHVISUALFORMAT:VVFL options:0Metrics:nil views:@{@"Blueview": Blueview}]; [Self.view addconstraints:vcons];}-(void) verticallayout{//1. Add two controlsUIView *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]; //2. Adding Constraints//2.1 Constraints in the horizontal directionNSString *HVFL =@"h:|-30-[blueview]-30-|"; Nsarray*hcons = [Nslayoutconstraint CONSTRAINTSWITHVISUALFORMAT:HVFL options:0Metrics:nil views:@{@"Blueview": Blueview}];    [Self.view addconstraints:hcons]; //2.2 Constraints in the vertical directionNSString *VVFL =@"V:|-30-[blueview (]-30-[redview) (==blueview)]"; Nsarray*vcons = [Nslayoutconstraint constraintswithvisualformat:vvfl options:nslayoutformatalignallright metrics:nil Views: @{@"Blueview": Blueview,@"Redview": Redview}];    [Self.view addconstraints:vcons]; Nslayoutconstraint*redleftcon = [Nslayoutconstraint constraintwithitem:redview attribute:nslayoutattributeleft relatedBy: Nslayoutrelationequal Toitem:blueview Attribute:nslayoutattributecenterx Multiplier:1.0constant0]; [Self.view Addconstraint:redleftcon];}
Summary

Finally, a summary of the format string is presented:

Function An expression
Horizontal direction H:
Vertical direction V:
Views [View]
Superview |
Relationship >=,==,<=
Space, Gap -
Priority level @value

Introduction to the VFL language for iOS learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.