FLEXMultilineTableViewCell.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // FLEXMultilineTableViewCell.m
  3. // FLEX
  4. //
  5. // Created by Ryan Olson on 2/13/15.
  6. // Copyright (c) 2015 f. All rights reserved.
  7. //
  8. #import "FLEXMultilineTableViewCell.h"
  9. #import "UIView+FLEX_Layout.h"
  10. @interface FLEXMultilineTableViewCell ()
  11. @property (nonatomic, readonly) UILabel *_titleLabel;
  12. @property (nonatomic, readonly) UILabel *_subtitleLabel;
  13. @property (nonatomic) BOOL constraintsUpdated;
  14. @end
  15. @implementation FLEXMultilineTableViewCell
  16. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  17. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  18. if (self) {
  19. self.titleLabel.numberOfLines = 0;
  20. self.subtitleLabel.numberOfLines = 0;
  21. }
  22. return self;
  23. }
  24. + (UIEdgeInsets)labelInsets {
  25. return UIEdgeInsetsMake(10.0, 15.0, 10.0, 15.0);
  26. }
  27. + (CGFloat)preferredHeightWithAttributedText:(NSAttributedString *)attributedText
  28. inTableViewWidth:(CGFloat)tableViewWidth
  29. style:(UITableViewStyle)style
  30. showsAccessory:(BOOL)showsAccessory {
  31. CGFloat labelWidth = tableViewWidth;
  32. // Content view inset due to accessory view observed on iOS 8.1 iPhone 6.
  33. if (showsAccessory) {
  34. labelWidth -= 34.0;
  35. }
  36. UIEdgeInsets labelInsets = [self labelInsets];
  37. labelWidth -= (labelInsets.left + labelInsets.right);
  38. CGSize constrainSize = CGSizeMake(labelWidth, CGFLOAT_MAX);
  39. CGFloat preferredLabelHeight = ceil([attributedText boundingRectWithSize:constrainSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height);
  40. CGFloat preferredCellHeight = preferredLabelHeight + labelInsets.top + labelInsets.bottom + 1.0;
  41. return preferredCellHeight;
  42. }
  43. @end
  44. @implementation FLEXMultilineDetailTableViewCell
  45. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  46. return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
  47. }
  48. @end