FLEXMultilineTableViewCell.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. UIEdgeInsets labelInsets = UIEdgeInsetsZero;
  27. labelInsets.top = 10.0;
  28. labelInsets.bottom = 10.0;
  29. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
  30. labelInsets.left = 15.0;
  31. labelInsets.right = 15.0;
  32. } else {
  33. labelInsets.left = 10.0;
  34. labelInsets.right = 10.0;
  35. }
  36. return labelInsets;
  37. }
  38. + (CGFloat)preferredHeightWithAttributedText:(NSAttributedString *)attributedText inTableViewWidth:(CGFloat)tableViewWidth style:(UITableViewStyle)style showsAccessory:(BOOL)showsAccessory
  39. {
  40. // Hardcoded margins from observation of cells in a grouped table on iOS 6.
  41. // There is no API to get the insets of the content view proir to layout.
  42. // Thankfully they removed the magic margins in iOS 7.
  43. // Differences are between the content view's width and the table view's width
  44. // Full screen iPhone - 20
  45. // Full screen iPad - 90
  46. CGFloat labelWidth = tableViewWidth;
  47. if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1 && style == UITableViewStyleGrouped) {
  48. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
  49. labelWidth -= 40.0;
  50. } else {
  51. labelWidth -= 90.0;
  52. }
  53. }
  54. // Content view inset due to accessory view observed on iOS 8.1 iPhone 6.
  55. if (showsAccessory) {
  56. labelWidth -= 34.0;
  57. }
  58. UIEdgeInsets labelInsets = [self labelInsets];
  59. labelWidth -= (labelInsets.left + labelInsets.right);
  60. CGSize constrainSize = CGSizeMake(labelWidth, CGFLOAT_MAX);
  61. CGFloat preferredLabelHeight = ceil([attributedText boundingRectWithSize:constrainSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height);
  62. CGFloat preferredCellHeight = preferredLabelHeight + labelInsets.top + labelInsets.bottom + 1.0;
  63. return preferredCellHeight;
  64. }
  65. @end