Нет описания

KXCustomServiceTableViewCell.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // KXCustomServiceTableViewCell.m
  3. // CAISHEN
  4. //
  5. // Created by xiaoxi on 2017/12/6.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXCustomServiceTableViewCell.h"
  9. @implementation KXCustomServiceTableViewCell
  10. - (void)awakeFromNib {
  11. [super awakeFromNib];
  12. // Initialization code
  13. }
  14. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  15. [super setSelected:selected animated:animated];
  16. // Configure the view for the selected state
  17. }
  18. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  19. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  20. if (self) {
  21. self.selectionStyle = UITableViewCellSelectionStyleNone;
  22. self.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8];
  23. [self setupUI];
  24. }
  25. return self;
  26. }
  27. - (void)setupUI {
  28. self.line = [[UILabel alloc] initWithFrame:CGRectZero];
  29. self.line.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8];
  30. [self.contentView addSubview:self.line];
  31. self.timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  32. self.timeLabel.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8];
  33. self.timeLabel.font = FONT_SYS(12*SCREEN_MUTI);
  34. self.timeLabel.textColor = [UIColor KXColorWithHex:0x727171];
  35. self.timeLabel.textAlignment = NSTextAlignmentCenter;
  36. [self.contentView addSubview:self.timeLabel];
  37. self.iconView = [[UIImageView alloc] initWithFrame:CGRectZero];
  38. self.iconView.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8];
  39. [self.contentView addSubview:self.iconView];
  40. self.bgView = [[UIImageView alloc] initWithFrame:CGRectZero];
  41. self.bgView.backgroundColor = [UIColor whiteColor];
  42. [self.contentView addSubview:self.bgView];
  43. self.contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  44. self.contentLabel.backgroundColor = [UIColor whiteColor];
  45. self.contentLabel.font = FONT_SYS(14*SCREEN_MUTI);
  46. self.contentLabel.textColor = [UIColor titleColor];
  47. self.contentLabel.numberOfLines = 0;
  48. [self.contentView addSubview:self.contentLabel];
  49. [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.centerX.equalTo(self.contentView);
  51. make.top.equalTo(self.contentView);
  52. make.width.equalTo(@200);
  53. }];
  54. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.left.equalTo(self.contentView).offset(18+44*SCREEN_MUTI+10);
  56. make.top.equalTo(self.timeLabel.mas_bottom);
  57. }];
  58. [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.left.equalTo(self.contentView).offset(18);
  60. make.centerY.equalTo(self.bgView);
  61. make.size.mas_equalTo(CGSizeMake(44*SCREEN_MUTI, 44*SCREEN_MUTI));
  62. }];
  63. [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.center.equalTo(self.bgView);
  65. }];
  66. [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.left.equalTo(self.contentView);
  68. make.top.equalTo(self.bgView.mas_bottom);
  69. make.right.equalTo(self.contentView);
  70. make.height.equalTo(@14);
  71. }];
  72. }
  73. - (void)refreshCell:(KXChatModel *)model {
  74. CGRect msgRect = [model.msg boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-(18+44+52)*SCREEN_MUTI, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(14*SCREEN_MUTI)} context:nil];
  75. CGFloat msgWidth = ceil(msgRect.size.width);
  76. CGFloat msgHeight = ceil(msgRect.size.height);
  77. if (msgHeight + 20 < 44) {
  78. msgHeight = 24;
  79. }
  80. UIImage *iconImage = nil;
  81. UIImage *bgImage = nil;
  82. if (model.isShowTime) {
  83. self.timeLabel.text = model.time;
  84. } else {
  85. self.timeLabel.text = @"";
  86. [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  87. make.height.equalTo(@0.5);
  88. }];
  89. }
  90. if (model.isLeft) {
  91. [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
  92. make.width.mas_equalTo(msgWidth+20);
  93. make.height.mas_equalTo(msgHeight+20);
  94. }];
  95. iconImage = [UIImage imageNamed:@"mine_borrow"];
  96. bgImage = [UIImage imageNamed:@""];
  97. self.iconView.image = [self circleImage:iconImage];
  98. } else {
  99. [self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
  100. make.right.equalTo(self.contentView).offset(-18);
  101. make.centerY.equalTo(self.bgView);
  102. make.size.mas_equalTo(CGSizeMake(44*SCREEN_MUTI, 44*SCREEN_MUTI));
  103. }];
  104. [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
  105. make.right.equalTo(self.contentView).offset(-18-44*SCREEN_MUTI-10);
  106. make.top.equalTo(self.timeLabel.mas_bottom);
  107. make.width.mas_equalTo(msgWidth+20);
  108. make.height.mas_equalTo(msgHeight+20);
  109. }];
  110. iconImage = [UIImage imageNamed:@"default_custom"];
  111. bgImage = [UIImage imageNamed:@""];
  112. self.iconView.image = iconImage;
  113. }
  114. self.bgView.image = bgImage;
  115. [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  116. make.width.mas_equalTo(msgWidth);
  117. make.height.mas_equalTo(msgHeight);
  118. }];
  119. self.contentLabel.text = model.msg;
  120. }
  121. - (UIImage *)circleImage:(UIImage *)image {
  122. // 开始图形上下文
  123. UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
  124. // 获得图形上下文
  125. CGContextRef ctx = UIGraphicsGetCurrentContext();
  126. // 设置一个范围
  127. CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
  128. // 根据一个rect创建一个椭圆
  129. CGContextAddEllipseInRect(ctx, rect);
  130. // 裁剪
  131. CGContextClip(ctx);
  132. // 将原照片画到图形上下文
  133. [image drawInRect:rect];
  134. // 从上下文上获取剪裁后的照片
  135. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  136. // 关闭上下文
  137. UIGraphicsEndImageContext();
  138. return newImage;
  139. }
  140. @end