// // EMMsgTextBubbleView.m // ChatDemo-UI3.0 // // Created by XieYajie on 2019/2/14. // Copyright © 2019 XieYajie. All rights reserved. // #import "EMMsgTextBubbleView.h" @implementation EMMsgTextBubbleView - (instancetype)initWithDirection:(EMMessageDirection)aDirection type:(EMMessageType)aType { self = [super initWithDirection:aDirection type:aType]; if (self) { [self _setupSubviews]; } return self; } #pragma mark - Subviews - (void)_setupSubviews { [self setupBubbleBackgroundImage]; self.textLabel = [[UILabel alloc] init]; self.textLabel.font = [UIFont systemFontOfSize:18]; self.textLabel.numberOfLines = 0; [self addSubview:self.textLabel]; [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.mas_top).offset(10); make.bottom.equalTo(self.mas_bottom).offset(-10); }]; if (self.direction == EMMessageDirectionSend) { self.textLabel.textColor = [UIColor whiteColor]; [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.mas_left).offset(10); make.right.equalTo(self.mas_right).offset(-15); }]; } else { self.textLabel.textColor = [UIColor blackColor]; [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.mas_left).offset(15); make.right.equalTo(self.mas_right).offset(-10); }]; } } #pragma mark - Setter - (void)setModel:(EMMessageModel *)model { EMTextMessageBody *body = (EMTextMessageBody *)model.emModel.body; self.textLabel.attributedText = [self setTextWithStr:[EMEmojiHelper convertEmoji:body.text]]; } - (NSAttributedString *)setTextWithStr:(NSString *)str { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str]; NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init]; paraStyle.alignment = NSTextAlignmentLeft;//两端对齐 [paraStyle setLineSpacing:10];//行间距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, str.length)]; [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, str.length)]; return attributedString; } @end