Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple wrapper for VFL
// formula as 'width == width'
static NSLayoutConstraint*
_lcItemRel(UIView *v1, NSString *formula, UIView *v2, float ratio, float constant) {
static NSDictionary *attrValueForName = nil;
static dispatch_once_t attrOnceToken;
dispatch_once(&attrOnceToken, ^{
attrValueForName =
@{
@"left": @(NSLayoutAttributeLeft),
@"right": @(NSLayoutAttributeRight),
@"top": @(NSLayoutAttributeTop),
@"bottom": @(NSLayoutAttributeBottom),
@"leading": @(NSLayoutAttributeLeading),
@"trailing": @(NSLayoutAttributeTrailing),
@"width": @(NSLayoutAttributeWidth),
@"height": @(NSLayoutAttributeHeight),
@"centerX": @(NSLayoutAttributeCenterX),
@"centerY": @(NSLayoutAttributeCenterY),
@"baseline": @(NSLayoutAttributeBaseline),
};
});
static NSDictionary *relationValueForName = nil;
static dispatch_once_t relationOnceToken;
dispatch_once(&relationOnceToken, ^{
relationValueForName =
@{
@"==": @(NSLayoutRelationEqual),
@">=": @(NSLayoutRelationGreaterThanOrEqual),
@"<=": @(NSLayoutRelationLessThanOrEqual),
};
});
NSArray *kv = [formula componentsSeparatedByString:@" "];
NSLayoutAttribute a1, a2;
NSLayoutRelation r;
if (kv[0]==nil || kv[1]==nil || kv[2]==nil) {
return nil;
}
a1 = (NSLayoutAttribute)[((NSNumber*)attrValueForName[kv[0]]) integerValue];
a2 = (NSLayoutAttribute)[((NSNumber*)attrValueForName[kv[2]]) integerValue];
r = (NSLayoutRelation)[((NSNumber*)relationValueForName[kv[1]]) integerValue];
return [NSLayoutConstraint constraintWithItem:v1
attribute:a1
relatedBy:r
toItem:v2
attribute:a2
multiplier:ratio
constant:constant];
}
static NSArray*
_lcAryOfVFL(NSString *formula, NSLayoutFormatOptions opt, NSDictionary *metric, NSDictionary *views) {
return [NSLayoutConstraint
constraintsWithVisualFormat:formula
options:opt
metrics:metric
views:views];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment