summaryrefslogtreecommitdiff
path: root/Juick/MessageCell.m
blob: f20acbe583550a9cf856b9739df8622cdebe8bbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
//  MessageCell.m
//  Juick
//
//  Created by Vitaly Takmazov on 29.10.13.
//  Copyright (c) 2013 com.juick. All rights reserved.
//

#import "MessageCell.h"
#import "Masonry.h"

@interface MessageCell ()

@property (nonatomic, assign) BOOL didSetupConstraints;

@end

@implementation MessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.avatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
        [self.avatar setBackgroundColor:[UIColor clearColor]];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
        [self.titleLabel setNumberOfLines:1];
        [self.titleLabel setTextAlignment:NSTextAlignmentLeft];
        [self.titleLabel setTextColor:[UIColor blackColor]];
        [self.titleLabel setBackgroundColor:[UIColor clearColor]];
        
        self.bodyLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        self.bodyLabel.dataDetectorTypes = NSTextCheckingTypeLink;
        self.bodyLabel.delegate = self;
        [self.bodyLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        [self.bodyLabel setLineBreakMode:NSLineBreakByTruncatingTail];
        [self.bodyLabel setNumberOfLines:0];
        [self.bodyLabel setTextAlignment:NSTextAlignmentLeft];
        [self.bodyLabel setTextColor:[UIColor darkGrayColor]];
        [self.bodyLabel setBackgroundColor:[UIColor clearColor]];
        [self.contentView addSubview:self.avatar];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.bodyLabel];
        
        [self updateFonts];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)updateConstraints {
    [super updateConstraints];
    
    if (self.didSetupConstraints) return;
    
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 20, 10, 20);
    
    [self.avatar makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.top).with.offset(padding.top);
        make.leading.equalTo(self.contentView.leading).with.offset(padding.left);
        make.width.lessThanOrEqualTo(@32);
        make.height.lessThanOrEqualTo(@32);
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.avatar.trailing).with.offset(padding.right);
        make.top.equalTo(self.contentView.top).with.offset(padding.top);
        make.trailing.equalTo(self.contentView.trailing).with.offset(-padding.right);
    }];
    
    [self.bodyLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView.leading).with.offset(padding.left);
        make.top.equalTo(self.avatar.bottom).with.offset(padding.top);
        make.trailing.equalTo(self.contentView.trailing).with.offset(-padding.right);
        make.bottom.equalTo(self.contentView.bottom).with.offset(-padding.bottom);
    }];
    
    self.didSetupConstraints = YES;
}


- (void)updateFonts
{
    self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
    self.bodyLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}

- (void) attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    [[UIApplication sharedApplication] openURL:url];
}

@end