// // MHComment.m // MHDevelopExample // // Created by CoderMikeHe on 17/2/8. // Copyright © 2017年 CoderMikeHe. All rights reserved. // #import "MHComment.h" @implementation MHComment - (instancetype)init { self = [super init]; if (self) { } return self; } - (NSAttributedString *)attributedText { if (self.IsUnderstand) { // 有回复 NSString *textString = [NSString stringWithFormat:@"%@回复%@: %@", self.Name, self.ReplyName, self.Content];; NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString]; [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, textString.length)]; [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#0A0A0A) range:NSMakeRange(0, textString.length)]; NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init]; [paraStyle setLineSpacing:12];//行间距 paraStyle.alignment = NSTextAlignmentJustified;//两端对齐 [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, textString.length)]; [mutableAttributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, textString.length)]; NSRange fromUserRange = NSMakeRange(0, self.Name.length); YYTextHighlight *fromUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]]; fromUserHighlight.userInfo = @{MHCommentUserKey:@(self.UserId)}; //// // 设置昵称颜色 [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#1F87DB) range:fromUserRange]; [mutableAttributedString addAttribute:@"YYTextHighlight" value:fromUserHighlight range:fromUserRange]; NSRange toUserRange = [textString rangeOfString:[NSString stringWithFormat:@"%@:",self.ReplyName]]; // 文本高亮模型 YYTextHighlight *toUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]]; // 这里痛过属性的userInfo保存User模型,后期通过获取模型然后获取User模型 toUserHighlight.userInfo = @{MHCommentUserKey:@(self.ReplyUserId)}; [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#1F87DB) range:toUserRange]; [mutableAttributedString addAttribute:@"YYTextHighlight" value:toUserHighlight range:toUserRange]; return mutableAttributedString; }else{ // 没有回复 NSString *textString = [NSString stringWithFormat:@"%@: %@", self.Name, self.Content]; NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString]; [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, textString.length)]; [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#0A0A0A) range:NSMakeRange(0, textString.length)]; NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init]; paraStyle.alignment = NSTextAlignmentJustified;//两端对齐 [paraStyle setLineSpacing:12];//行间距 [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, textString.length)]; [mutableAttributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, textString.length)]; NSRange fromUserRange = NSMakeRange(0, self.Name.length+1); // 设置昵称颜色 YYTextHighlight *fromUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]]; fromUserHighlight.userInfo = @{MHCommentUserKey:@(self.UserId)}; [mutableAttributedString addAttribute:@"YYTextHighlight" value:fromUserHighlight range:fromUserRange]; [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#1F87DB) range:fromUserRange]; return mutableAttributedString; } return nil; } @end