// // MessageCell.m // Juick // // Created by Vitaly Takmazov on 29.10.13. // Copyright (c) 2013 com.juick. All rights reserved. // #import "MessageCell.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.titleLabel = [[TTTAttributedLabel 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.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; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:kLabelHorizontalInsets]]; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:(kLabelHorizontalInsets / 2)]]; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-kLabelHorizontalInsets]]; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.bodyLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:kLabelHorizontalInsets]]; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.bodyLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeBottom multiplier:1.0f constant:(kLabelHorizontalInsets / 4)]]; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.bodyLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-kLabelHorizontalInsets]]; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.bodyLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-(kLabelHorizontalInsets / 2)]]; 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