美文网首页
NSLayoutConstraint约束的active属性

NSLayoutConstraint约束的active属性

作者: Z了个L | 来源:发表于2016-10-15 11:10 被阅读209次
    
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentLabelHeightCon; // 评论数量Label高度约束,在xib中,高度约束值为0
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *firstCon;              // 第一个评论高度约束,在xib中,高度约束值为0
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *secondCon;             // 第二个评论高度约束,在xib中,高度约束值为0
    
    
    /**
     *  关注动态 - 作品数据
     */
    - (void)setDish:(XCFDish *)dish {
        _dish = dish;
    
        [self.iconView setHeaderWithURL:[NSURL URLWithString:dish.author.photo60]];
        self.authorNameLabel.text = dish.author.name;
        self.createTimeLabel.text = dish.friendly_create_time;
        self.dishNameLabel.text = dish.name;
        self.descLabel.text = dish.desc;
        self.diggsCountLabel.text = [NSString stringWithFormat:@"%@人", dish.ndiggs];
    
        if (dish.is_orphan) {
            self.actionLabel.text = @"分享到";
        } else {
            self.actionLabel.text = @"做过";
        }
    
        self.commentCountLabel.text = @"";
        self.firstCommentLabel.text = @"";
        self.secondCommentLabel.text = @"";
        // 调整点赞按钮约束
        self.diggsButtonCon.constant = dish.commentViewHeight;
    
        if (dish.latest_comments.count) { // 如果有评论
    
            NSLog(@"self.firstCon.active==%zd", self.firstCon.active);
    
            // active默认值是YES,如果想自动获得UILabel的高度,把该属性设置为NO,并且给label设置文本,那么label的高度会自动计算显示出来,不需要其他处理
            // 显示第1条评论
            self.firstCon.active = NO;
            XCFComment *firstComment = [dish.latest_comments lastObject];
            [self.firstCommentLabel setAttributeTextWithString:[NSString stringWithFormat:@"%@:%@", firstComment.author.name, firstComment.txt]
                                                         range:NSMakeRange(0, firstComment.author.name.length)];
    
            // 如果有1条以上评论
            if (dish.latest_comments.count > 1) {
    
                // 显示第2条评论
                self.secondCon.active = NO;
                XCFComment *secondComment = dish.latest_comments[dish.latest_comments.count - 2];
                [self.secondCommentLabel setAttributeTextWithString:[NSString stringWithFormat:@"%@:%@", secondComment.author.name, secondComment.txt]
                                                              range:NSMakeRange(0, secondComment.author.name.length)];
    
    //            if ([secondComment.txt rangeOfString:@"@"].location != NSNotFound) {
    //
    //                NSRange range1 = [secondComment.txt rangeOfString:@"@"];
    //                NSRange range2 = [secondComment.txt rangeOfString:@" "];
    //                NSRange result = NSMakeRange(range1.location, range2.location-range1.location);
    //                XCFLog(@"%zd %zd", result.location, result.length);
    //            }
                // 如果有2条以上评论
                if (dish.latest_comments.count > 2) {
    
                    // 显示评论总数Label
                    self.commentLabelHeightCon.active = NO;
                    self.commentCountLabel.text = [NSString stringWithFormat:@"所有%ld条评论", (long)dish.ncomments];
                }
            }
        }
    
    }
    
    
    
    • 效果图:


    相关文章

      网友评论

          本文标题:NSLayoutConstraint约束的active属性

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