如何优化用VFL自动布局编写的代码

时间:2015-12-29 14:37:56

标签: ios objective-c autolayout visual-format-language

我有一个UIView我在其中创建UILabels而没有for loop并使用VFL。 它的工作完美,但代码可读性非常差。甚至我不明白如何使用for循环来创建这些标签。 我必须将身高设置为30,这是我不想要的。我想让它成长automatically。 此外,我将subcontentView height常量设置为硬编码constantHeightSubContentView.constant = 250; 我不想使用UIStackView。  这是我试过的代码:

-(void)createProfileView{

if (ProfileView == nil) {
    ProfileView = [[UIView alloc] init];
    [ProfileView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [ProfileView setBackgroundColor:[UIColor yellowColor]];
    [subContentView addSubview:ProfileView];
    views[@"ProfileView"] = ProfileView;

    NSArray* constraints;
    NSString* format;

    format = @"|[ProfileView]|";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [subContentView addConstraints:constraints];

    format = @"V:|-50-[ProfileView]|";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [subContentView addConstraints:constraints];

    //create labels
    //homeTown
    UILabel *lblHomeTown = [self getLabel];
    [lblHomeTown setBackgroundColor:[UIColor greenColor]];
    [lblHomeTown setText:@"Hometown:"];
    [ProfileView addSubview:lblHomeTown];
    views[@"lblHomeTown"] = lblHomeTown;

    UILabel *lblHomeTownDetail = [self getLabel];
    [lblHomeTownDetail setBackgroundColor:[UIColor grayColor]];
    [lblHomeTownDetail setText:self.player.homeTown];
    [ProfileView addSubview:lblHomeTownDetail];
    views[@"lblHomeTownDetail"] = lblHomeTownDetail;

    format = @"|-10-[lblHomeTown]-2-[lblHomeTownDetail]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:[lblHomeTown(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:|[lblHomeTownDetail(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];
    //hometown

    //highSchool
    //create labels
    UILabel *lblHighSchool = [self getLabel];
    [lblHighSchool setBackgroundColor:[UIColor magentaColor]];
    [lblHighSchool setText:@"High School:"];
    [ProfileView addSubview:lblHighSchool];
    views[@"lblHighSchool"] = lblHighSchool;

    UILabel *lblHighSchoolDetail = [self getLabel];
    [lblHighSchoolDetail setBackgroundColor:[UIColor redColor]];
    [lblHighSchoolDetail setText:self.player.highSchool];
    [ProfileView addSubview:lblHighSchoolDetail];
    views[@"lblHighSchoolDetail"] = lblHighSchoolDetail;

    format = @"|-10-[lblHighSchool]-2-[lblHighSchoolDetail]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:|[lblHomeTown(30)]-10-[lblHighSchool(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:|[lblHomeTown(30)]-10-[lblHighSchoolDetail(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];
    //highschool


    //experience
    UILabel *lblExperience = [self getLabel];
    [lblExperience setText:@"Experience:"];
    [ProfileView addSubview:lblExperience];
    views[@"lblExperience"] = lblExperience;

    UILabel *lblExperienceDetail = [self getLabel];
    [lblExperienceDetail setText:self.player.experience];
    [ProfileView addSubview:lblExperienceDetail];
    views[@"lblExperienceDetail"] = lblExperienceDetail;

    format = @"|-10-[lblExperience]-2-[lblExperienceDetail]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:[lblHighSchool(30)]-10-[lblExperience(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:[lblHighSchool(30)]-10-[lblExperienceDetail(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];
     //experience

    //description
    UILabel *lblDescription = [self getLabel];
    [lblDescription setText:self.player.playerDescription];
    [ProfileView addSubview:lblDescription];
    views[@"lblDescription"] = lblDescription;

    format = @"|-10-[lblDescription]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:[lblExperience(30)]-10-[lblDescription]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    //description
}
constantHeightSubContentView.constant = 250;
[subContentView bringSubviewToFront:ProfileView];

}

-(UILabel *)getLabel{

UILabel *lbl = [[UILabel alloc] init];
[lbl setTranslatesAutoresizingMaskIntoConstraints:NO];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setNumberOfLines:0];
[lbl setTextColor:[UIColor blackColor]];
[lbl setFont:[UIFont fontWithName:@"AppleSDGothicNeo-Regular" size:13.0f]];
lbl.lineBreakMode = NSLineBreakByWordWrapping;
return lbl;

}

1 个答案:

答案 0 :(得分:0)

惊人的答案https://stackoverflow.com/a/25898949/1612489,唯一的问题是你必须使用VFL来指定乘数值而不是界面构建器,如答案中所示。但是一旦你理解了所使用的逻辑,这应该很容易。