EMMsgTextBubbleView.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // EMMsgTextBubbleView.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/2/14.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMMsgTextBubbleView.h"
  9. @implementation EMMsgTextBubbleView
  10. - (instancetype)initWithDirection:(EMMessageDirection)aDirection
  11. type:(EMMessageType)aType
  12. {
  13. self = [super initWithDirection:aDirection type:aType];
  14. if (self) {
  15. [self _setupSubviews];
  16. }
  17. return self;
  18. }
  19. #pragma mark - Subviews
  20. - (void)_setupSubviews
  21. {
  22. [self setupBubbleBackgroundImage];
  23. self.textLabel = [[UILabel alloc] init];
  24. self.textLabel.font = [UIFont systemFontOfSize:18];
  25. self.textLabel.numberOfLines = 0;
  26. [self addSubview:self.textLabel];
  27. [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.top.equalTo(self.mas_top).offset(10);
  29. make.bottom.equalTo(self.mas_bottom).offset(-10);
  30. }];
  31. if (self.direction == EMMessageDirectionSend) {
  32. self.textLabel.textColor = [UIColor whiteColor];
  33. [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.left.equalTo(self.mas_left).offset(10);
  35. make.right.equalTo(self.mas_right).offset(-15);
  36. }];
  37. } else {
  38. self.textLabel.textColor = [UIColor blackColor];
  39. [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.left.equalTo(self.mas_left).offset(15);
  41. make.right.equalTo(self.mas_right).offset(-10);
  42. }];
  43. }
  44. }
  45. #pragma mark - Setter
  46. - (void)setModel:(EMMessageModel *)model
  47. {
  48. EMTextMessageBody *body = (EMTextMessageBody *)model.emModel.body;
  49. self.textLabel.attributedText = [self setTextWithStr:[EMEmojiHelper convertEmoji:body.text]];
  50. }
  51. - (NSAttributedString *)setTextWithStr:(NSString *)str
  52. {
  53. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
  54. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
  55. paraStyle.alignment = NSTextAlignmentLeft;//两端对齐
  56. [paraStyle setLineSpacing:10];//行间距
  57. [attributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, str.length)];
  58. [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, str.length)];
  59. return attributedString;
  60. }
  61. @end