FLEXMultilineTableViewCell.m 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // FLEXMultilineTableViewCell.m
  3. // UICatalog
  4. //
  5. // Created by Ryan Olson on 2/13/15.
  6. // Copyright (c) 2015 f. All rights reserved.
  7. //
  8. #import "FLEXMultilineTableViewCell.h"
  9. NSString *const kFLEXMultilineTableViewCellIdentifier = @"kFLEXMultilineTableViewCellIdentifier";
  10. @implementation FLEXMultilineTableViewCell
  11. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  12. {
  13. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  14. if (self) {
  15. self.textLabel.numberOfLines = 0;
  16. }
  17. return self;
  18. }
  19. - (void)layoutSubviews
  20. {
  21. [super layoutSubviews];
  22. self.textLabel.frame = UIEdgeInsetsInsetRect(self.contentView.bounds, [[self class] labelInsets]);
  23. }
  24. + (UIEdgeInsets)labelInsets
  25. {
  26. return UIEdgeInsetsMake(10.0, 15.0, 10.0, 15.0);
  27. }
  28. + (CGFloat)preferredHeightWithAttributedText:(NSAttributedString *)attributedText inTableViewWidth:(CGFloat)tableViewWidth style:(UITableViewStyle)style showsAccessory:(BOOL)showsAccessory
  29. {
  30. CGFloat labelWidth = tableViewWidth;
  31. // Content view inset due to accessory view observed on iOS 8.1 iPhone 6.
  32. if (showsAccessory) {
  33. labelWidth -= 34.0;
  34. }
  35. UIEdgeInsets labelInsets = [self labelInsets];
  36. labelWidth -= (labelInsets.left + labelInsets.right);
  37. CGSize constrainSize = CGSizeMake(labelWidth, CGFLOAT_MAX);
  38. CGFloat preferredLabelHeight = ceil([attributedText boundingRectWithSize:constrainSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height);
  39. CGFloat preferredCellHeight = preferredLabelHeight + labelInsets.top + labelInsets.bottom + 1.0;
  40. return preferredCellHeight;
  41. }
  42. @end