自定义cell设置现价,原价(加横线)

原价,现价分别是连个label。这两个label不能直接限制死他们的宽度,因为他们的宽度不确定,而由于lable的特殊性,不设置它的宽度约束时,宽度取决于文字的内容,所以两个lable的约束设置好一些必要的约束即可,不用约束完整(特指宽度)。

对于有横线的lable使用自定义lable,在lable上面画图即可

CenterLineLabel.m
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    //方式一:实心的矩形框
    UIRectFill(CGRectMake(0, rect.size.height * 0.5, rect.size.width, 1));
    
        // 方式二:画线
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 设置起点
//    CGContextMoveToPoint(ctx, 0, rect.size.height * 0.5);
//    // 连线到另一个点
//    CGContextAddLineToPoint(ctx, rect.size.width, rect.size.height * 0.5);
//    // 渲染
//    CGContextStrokePath(ctx);
}

在cell中设置lable的类型为CenterLineLable

    // 处理现价小数位的问题
    self.currentPriceLabel.text = [NSString stringWithFormat:@"¥ %@", deal.current_price];
    NSUInteger dotLoc = [self.currentPriceLabel.text rangeOfString:@"."].location;//小数点的位置
    //19.998
    //length = 6
    //dotLoc = 2
    if (dotLoc != NSNotFound) {//保留两位小数
        // 超过2位小数
        if (self.currentPriceLabel.text.length - dotLoc > 3) {
            self.currentPriceLabel.text = [self.currentPriceLabel.text substringToIndex:dotLoc + 3];
        }
    }

 

posted @ 2017-03-12 12:45  justqi  阅读(404)  评论(0编辑  收藏  举报