MHComment.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // MHComment.m
  3. // MHDevelopExample
  4. //
  5. // Created by CoderMikeHe on 17/2/8.
  6. // Copyright © 2017年 CoderMikeHe. All rights reserved.
  7. //
  8. #import "MHComment.h"
  9. @implementation MHComment
  10. - (instancetype)init
  11. {
  12. self = [super init];
  13. if (self) {
  14. }
  15. return self;
  16. }
  17. - (NSAttributedString *)attributedText
  18. {
  19. if (self.IsUnderstand) {
  20. // 有回复
  21. NSString *textString = [NSString stringWithFormat:@"%@回复%@: %@", self.Name, self.ReplyName, self.Content];;
  22. NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString];
  23. [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, textString.length)];
  24. [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#0A0A0A) range:NSMakeRange(0, textString.length)];
  25. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
  26. [paraStyle setLineSpacing:12];//行间距
  27. paraStyle.alignment = NSTextAlignmentJustified;//两端对齐
  28. [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, textString.length)];
  29. [mutableAttributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, textString.length)];
  30. NSRange fromUserRange = NSMakeRange(0, self.Name.length);
  31. YYTextHighlight *fromUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]];
  32. fromUserHighlight.userInfo = @{MHCommentUserKey:@(self.UserId)};
  33. //// // 设置昵称颜色
  34. [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#1F87DB) range:fromUserRange];
  35. [mutableAttributedString addAttribute:@"YYTextHighlight" value:fromUserHighlight range:fromUserRange];
  36. NSRange toUserRange = [textString rangeOfString:[NSString stringWithFormat:@"%@:",self.ReplyName]];
  37. // 文本高亮模型
  38. YYTextHighlight *toUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]];
  39. // 这里痛过属性的userInfo保存User模型,后期通过获取模型然后获取User模型
  40. toUserHighlight.userInfo = @{MHCommentUserKey:@(self.ReplyUserId)};
  41. [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#1F87DB) range:toUserRange];
  42. [mutableAttributedString addAttribute:@"YYTextHighlight" value:toUserHighlight range:toUserRange];
  43. return mutableAttributedString;
  44. }else{
  45. // 没有回复
  46. NSString *textString = [NSString stringWithFormat:@"%@: %@", self.Name, self.Content];
  47. NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString];
  48. [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, textString.length)];
  49. [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#0A0A0A) range:NSMakeRange(0, textString.length)];
  50. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
  51. paraStyle.alignment = NSTextAlignmentJustified;//两端对齐
  52. [paraStyle setLineSpacing:12];//行间距
  53. [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, textString.length)];
  54. [mutableAttributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, textString.length)];
  55. NSRange fromUserRange = NSMakeRange(0, self.Name.length+1);
  56. // 设置昵称颜色
  57. YYTextHighlight *fromUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]];
  58. fromUserHighlight.userInfo = @{MHCommentUserKey:@(self.UserId)};
  59. [mutableAttributedString addAttribute:@"YYTextHighlight" value:fromUserHighlight range:fromUserRange];
  60. [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#1F87DB) range:fromUserRange];
  61. return mutableAttributedString;
  62. }
  63. return nil;
  64. }
  65. @end