美文网首页
实现两个label前后对齐

实现两个label前后对齐

作者: 大炮打小鸟 | 来源:发表于2019-11-01 14:49 被阅读0次
image.png

一看就是令人头疼的UI设计

        UILabel *dateLabel = [[UILabel alloc]init];
        dateLabel.frame = CGRectMake(UI_View_Width-30-200, 33, 200, 15);
        dateLabel.font = [UIFont systemFontOfSize:15];
        dateLabel.textColor = BB_White_Color;
        dateLabel.textAlignment = NSTextAlignmentRight;
        dateLabel.text = [NSString stringWithFormat:@"还款日期  %@",_dataSource[@"date"]];
        [cell.contentView addSubview:dateLabel];
        
        UILabel *amountLabel = [[UILabel alloc]init];
        amountLabel.frame = CGRectMake(UI_View_Width-30-200, 72, 200, 15);
        amountLabel.font = [UIFont systemFontOfSize:15];
        amountLabel.textColor = BB_White_Color;
        amountLabel.textAlignment = NSTextAlignmentRight;
        amountLabel.text = [NSString stringWithFormat:@"借款金额  %@元",_dataSource[@"amount"]];
        [cell.contentView addSubview:amountLabel];
        
        if ([dateLabel sizeThatFits:CGSizeMake(UI_View_Width-30, 15)].width>[amountLabel sizeThatFits:CGSizeMake(UI_View_Width-30, 15)].width) {
            NSString *space = @" ";
            for (int i=0; i<MAXFLOAT; i++) {
                amountLabel.text = [NSString stringWithFormat:@"借款金额  %@%@元",space,_dataSource[@"amount"]];
                if ([dateLabel sizeThatFits:CGSizeMake(UI_View_Width-30, 15)].width<=[amountLabel sizeThatFits:CGSizeMake(UI_View_Width-30, 15)].width) {
                    break;
                }
                space = [space stringByAppendingString:@" "];
            }
        }else{
            NSString *space = @" ";
            for (int i=0; i<MAXFLOAT; i++) {
                dateLabel.text = [NSString stringWithFormat:@"还款日期  %@%@",space,_dataSource[@"date"]];
                if ([dateLabel sizeThatFits:CGSizeMake(UI_View_Width-30, 15)].width>=[amountLabel sizeThatFits:CGSizeMake(UI_View_Width-30, 15)].width) {
                    break;
                }
                space = [space stringByAppendingString:@" "];
            }
        }

相关文章

网友评论

      本文标题:实现两个label前后对齐

      本文链接:https://www.haomeiwen.com/subject/ixgfbctx.html