FLEXDescriptionTableViewCell.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // FLEXDescriptionTableViewCell.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-05.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXDescriptionTableViewCell.h"
  9. #import "FLEXUtility.h"
  10. @interface FLEXDescriptionTableViewCell ()
  11. @end
  12. @implementation FLEXDescriptionTableViewCell
  13. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  14. {
  15. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  16. if (self) {
  17. self.textLabel.numberOfLines = 0;
  18. self.textLabel.font = [[self class] labelFont];
  19. }
  20. return self;
  21. }
  22. - (void)layoutSubviews
  23. {
  24. [super layoutSubviews];
  25. self.textLabel.frame = UIEdgeInsetsInsetRect(self.contentView.bounds, [[self class] labelInsets]);
  26. }
  27. + (UIFont *)labelFont
  28. {
  29. return [FLEXUtility defaultTableViewCellLabelFont];
  30. }
  31. + (UIEdgeInsets)labelInsets
  32. {
  33. UIEdgeInsets labelInsets = UIEdgeInsetsZero;
  34. labelInsets.top = 15.0;
  35. labelInsets.bottom = 15.0;
  36. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
  37. labelInsets.left = 15.0;
  38. labelInsets.right = 0.0;
  39. } else {
  40. labelInsets.left = 10.0;
  41. labelInsets.right = 10.0;
  42. }
  43. return labelInsets;
  44. }
  45. + (CGFloat)preferredHeightWithText:(NSString *)text inTableViewWidth:(CGFloat)tableViewWidth
  46. {
  47. // Hardcoded margins from observation of cells in a grouped table on iOS 6.
  48. // There is no API to get the insets of the content view proir to layout.
  49. // Thankfully they removed the magic margins in iOS 7.
  50. // Differences are between the content view's width and the table view's width
  51. // Full screen iPhone - 20
  52. // Full screen iPad - 90
  53. CGFloat labelWidth = tableViewWidth;
  54. if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1) {
  55. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
  56. labelWidth -= 40.0;
  57. } else {
  58. labelWidth -= 90.0;
  59. }
  60. }
  61. UIEdgeInsets labelInsets = [self labelInsets];
  62. labelWidth -= (labelInsets.left + labelInsets.right);
  63. // Size an attributed string to get around deprecation warnings if the deployment target is >= 7 while still supporting deployment tagets back to 6.0.
  64. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: [self labelFont]}];
  65. CGSize constrainSize = CGSizeMake(labelWidth, CGFLOAT_MAX);
  66. CGFloat preferredLabelHeight = ceil([attributedText boundingRectWithSize:constrainSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height);
  67. CGFloat preferredCellHeight = preferredLabelHeight + labelInsets.top + labelInsets.bottom + 1.0;
  68. return preferredCellHeight;
  69. }
  70. @end