宽容他人,放过自己。

竖排label并实现自适应

Posted on By anchoriteFili

单选框和label的测试.zip

111328013951651.png

框架


.h

#pragma mark 四个选择单选框对应的lable
@property (nonatomic,retain) UILabel *firstLabel; //第一个对应的label
@property (nonatomic,retain) UILabel *secondLabel;
@property (nonatomic,retain) UILabel *thirdLabel;
@property (nonatomic,retain) UILabel *fourthLabel;

.m

#pragma mark 四个label的创建
    self.firstLabel.text = @"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;";
    
    self.firstLabel.frame = [self adaptHeightWithFrame:self.firstLabel.frame andText:@"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;"];
    [self.view addSubview:self.firstLabel];
    
     self.secondLabel.text = @"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;";
     
     self.secondLabel.frame = [self adaptHeightWithFrame:self.secondLabel.frame andText:@"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;"];
     [self.view addSubview:self.secondLabel];
    
    self.thirdLabel.text = @"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;";
    
    self.thirdLabel.frame = [self adaptHeightWithFrame:self.thirdLabel.frame andText:@"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;"];
    [self.view addSubview:self.thirdLabel];

    self.fourthLabel.text = @"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;";
    
    self.fourthLabel.frame = [self adaptHeightWithFrame:self.fourthLabel.frame andText:@"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;"];
    [self.view addSubview:self.fourthLabel];
#pragma mark -----------------4个选择label的创建---------------------
- (UILabel *)firstLabel {
    if (!_firstLabel) {
        _firstLabel = [self createLabelWithy:70];
    }
    return _firstLabel;
}

- (UILabel *)secondLabel {
    if (!_secondLabel) {
        _secondLabel = [self createLabelWithy:(self.firstLabel.frame.origin.y+self.firstLabel.frame.size.height+10)];
    }
    return _secondLabel;
}

- (UILabel *)thirdLabel {
    if (!_thirdLabel) {
        _thirdLabel = [self createLabelWithy:(self.secondLabel.frame.origin.y+self.secondLabel.frame.size.height+10)];
    }
    return _thirdLabel;
}

- (UILabel *)fourthLabel {
    if (!_fourthLabel) {
        _fourthLabel = [self createLabelWithy:(self.thirdLabel.frame.origin.y+self.thirdLabel.frame.size.height+10)];
    }
    return _fourthLabel;
}

#pragma mark -----------------四个单选label自适应高度的方法-------------------
- (CGRect)adaptHeightWithFrame:(CGRect)frame  andText:(NSString *)text {
    CGRect frameOne = frame;
    CGRect rect = [text boundingRectWithSize:CGSizeMake(268.5, 1000)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11]}
                                     context:nil];
    frameOne.size.height = rect.size.height;
    return frameOne;
}

#pragma mark 创建四个label的方法
- (UILabel *)createLabelWithy:(CGFloat)y {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTHADP*51.5, y, WIDTHADP*268.5, 0)];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor orangeColor];
    label.font = [UIFont systemFontOfSize:11.0];
    label.textColor = RGB_COLOR(120, 120, 120);
    return label;
}

整体思路:

1. 首先用方法自动创建label,可以先固定一些固定属性,如果是横排可传x
#pragma mark 创建四个label的方法
- (UILabel *)createLabelWithy:(CGFloat)y {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTHADP*51.5, y, WIDTHADP*268.5, 0)];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor orangeColor];
    label.font = [UIFont systemFontOfSize:11.0];
    label.textColor = RGB_COLOR(120, 120, 120);
    return label;
    
}
2. 用懒加载加载竖排的label,用创建label方法创建
#pragma mark 四个选择单选框对应的lable
@property (nonatomic,retain) UILabel *firstLabel; //第一个对应的label
@property (nonatomic,retain) UILabel *secondLabel;
@property (nonatomic,retain) UILabel *thirdLabel;
@property (nonatomic,retain) UILabel *fourthLabel;

#pragma mark -----------------4个选择label的创建---------------------
- (UILabel *)firstLabel {
    if (!_firstLabel) {
        _firstLabel = [self createLabelWithy:70];
    }
    return _firstLabel;
}

- (UILabel *)secondLabel {
    if (!_secondLabel) {
        _secondLabel = [self createLabelWithy:(self.firstLabel.frame.origin.y+self.firstLabel.frame.size.height+10)];
    }
    return _secondLabel;
}

- (UILabel *)thirdLabel {
    if (!_thirdLabel) {
        _thirdLabel = [self createLabelWithy:(self.secondLabel.frame.origin.y+self.secondLabel.frame.size.height+10)];
    }
    return _thirdLabel;
}

- (UILabel *)fourthLabel {
    if (!_fourthLabel) {
        _fourthLabel = [self createLabelWithy:(self.thirdLabel.frame.origin.y+self.thirdLabel.frame.size.height+10)];
    }
    return _fourthLabel;
}
3. label的加载:先对label进行赋值,然后自适应方法改变frame,最后添加到需要添加的地方
#pragma mark 四个label的创建
    
    self.firstLabel.text = @"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;";
    
    self.firstLabel.frame = [self adaptHeightWithFrame:self.firstLabel.frame andText:@"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;"];
    [self.view addSubview:self.firstLabel];
    
     self.secondLabel.text = @"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;";
     
     self.secondLabel.frame = [self adaptHeightWithFrame:self.secondLabel.frame andText:@"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;"];
     [self.view addSubview:self.secondLabel];
    
    self.thirdLabel.text = @"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;";
    
    self.thirdLabel.frame = [self adaptHeightWithFrame:self.thirdLabel.frame andText:@"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;"];
    [self.view addSubview:self.thirdLabel];

    self.fourthLabel.text = @"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;";
    
    self.fourthLabel.frame = [self adaptHeightWithFrame:self.fourthLabel.frame andText:@"jlasjfladjJSALFJDALFJAS;LFJA;LADSJ拉萨的减肥;拉大手机费;拉手机福利;啊啊发到家里发生开发阿斯蒂芬阿道夫拉到就发了卡的手机福利卡的死法律的时间弗拉的快速减肥嗄对拉手机福利卡的积分嗄对手机福利;"];
    [self.view addSubview:self.fourthLabel];