PostTableViewCell.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // TweetTableViewCell.m
  2. //
  3. // Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "PostTableViewCell.h"
  23. #import "Post.h"
  24. #import "User.h"
  25. #import "UIImageView+AFNetworking.h"
  26. @implementation PostTableViewCell
  27. - (id)initWithStyle:(UITableViewCellStyle)style
  28. reuseIdentifier:(NSString *)reuseIdentifier
  29. {
  30. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  31. if (!self) {
  32. return nil;
  33. }
  34. self.textLabel.adjustsFontSizeToFitWidth = YES;
  35. self.textLabel.textColor = [UIColor darkGrayColor];
  36. self.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
  37. self.detailTextLabel.numberOfLines = 0;
  38. self.selectionStyle = UITableViewCellSelectionStyleGray;
  39. return self;
  40. }
  41. - (void)setPost:(Post *)post {
  42. _post = post;
  43. self.textLabel.text = _post.user.username;
  44. self.detailTextLabel.text = _post.text;
  45. [self.imageView setImageWithURL:_post.user.avatarImageURL placeholderImage:[UIImage imageNamed:@"profile-image-placeholder"]];
  46. [self setNeedsLayout];
  47. }
  48. + (CGFloat)heightForCellWithPost:(Post *)post {
  49. return (CGFloat)fmaxf(70.0f, (float)[self detailTextHeight:post.text] + 45.0f);
  50. }
  51. + (CGFloat)detailTextHeight:(NSString *)text {
  52. CGRect rectToFit = [text boundingRectWithSize:CGSizeMake(240.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0f]} context:nil];
  53. return rectToFit.size.height;
  54. }
  55. #pragma mark - UIView
  56. - (void)layoutSubviews {
  57. [super layoutSubviews];
  58. self.imageView.frame = CGRectMake(10.0f, 10.0f, 50.0f, 50.0f);
  59. self.textLabel.frame = CGRectMake(70.0f, 6.0f, 240.0f, 20.0f);
  60. CGRect detailTextLabelFrame = CGRectOffset(self.textLabel.frame, 0.0f, 25.0f);
  61. CGFloat calculatedHeight = [[self class] detailTextHeight:self.post.text];
  62. detailTextLabelFrame.size.height = calculatedHeight;
  63. self.detailTextLabel.frame = detailTextLabelFrame;
  64. }
  65. @end