Как оптимизировать код, написанный в VFL autolayout
У меня есть 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 ответ
Удивительный ответ /questions/3137222/ravnomernoe-razmeschenie-neskolkih-predstavlenij-v-predstavlenii-kontejnera/3137231#3137231, единственное, что вам нужно будет использовать VFL для указания значений множителя вместо конструктора интерфейса, как показано в ответе. Но это должно быть легко, как только вы поймете логику.