ChatBarTextView.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // ChatBarTextView.m
  3. // BuguLive
  4. //
  5. // Created by 朱庆彬 on 2017/8/23.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "ChatBarTextView.h"
  9. @implementation ZWNSTextAttachment
  10. - (nullable UIImage *)imageForBounds:(CGRect)imageBounds textContainer:(nullable NSTextContainer *)textContainer characterIndex:(NSUInteger)charIndex
  11. {
  12. return self.mFaceImg;
  13. }
  14. - (CGRect)attachmentBoundsForTextContainer:(nullable NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
  15. {
  16. CGRect fff = CGRectMake(position.x, 0 - (self.mFontH / 3.34f), self.mFontH, self.mFontH);
  17. return fff;
  18. }
  19. @end
  20. @implementation ChatBarTextView
  21. - (void)paste:(id)sender
  22. {
  23. [super paste:sender];
  24. NSString *text = self.attributedText.string;
  25. NSRange searchFrom;
  26. searchFrom.length = text.length;
  27. searchFrom.location = 0;
  28. NSMutableArray *findarr = NSMutableArray.new;
  29. do
  30. {
  31. NSRange find = [text rangeOfString:@"\\[[a-z]+[0-9]+\\]" options:NSRegularExpressionSearch range:searchFrom];
  32. if (find.location == NSNotFound)
  33. { //最好一次么有找到..
  34. break;
  35. }
  36. //找到了
  37. [findarr addObject:@{ @"l": @(find.location),
  38. @"s": @(find.length),
  39. @"yy": [text substringWithRange:find] }];
  40. searchFrom.location = find.location + find.length;
  41. searchFrom.length = text.length - searchFrom.location;
  42. } while (searchFrom.location < text.length);
  43. NSMutableAttributedString *finalstr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  44. NSInteger fix = 0;
  45. for (NSDictionary *one in findarr)
  46. {
  47. NSRange r;
  48. r.location = [[one objectForKey:@"l"] integerValue] - fix;
  49. r.length = [[one objectForKey:@"s"] integerValue];
  50. fix += r.length - 1;
  51. NSString *facename = [one objectForKey:@"yy"];
  52. NSString *facenamepng = [facename substringWithRange:NSMakeRange(1, facename.length - 2)];
  53. facenamepng = [facename stringByReplacingOccurrencesOfString:@"[" withString:@""];
  54. facenamepng = [facenamepng stringByReplacingOccurrencesOfString:@"]" withString:@""];
  55. facenamepng = [facenamepng stringByAppendingString:@".png"];
  56. ZWNSTextAttachment *faceattachment = [[ZWNSTextAttachment alloc] initWithData:nil ofType:nil];
  57. faceattachment.mFontH = self.font.lineHeight;
  58. faceattachment.mFaceImg = [UIImage imageNamed:facenamepng];
  59. faceattachment.mFaceNmae = facename;
  60. NSAttributedString *faceattr = [NSAttributedString attributedStringWithAttachment:faceattachment];
  61. [finalstr replaceCharactersInRange:r withAttributedString:faceattr];
  62. }
  63. [finalstr addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, finalstr.length)];
  64. self.attributedText = finalstr;
  65. }
  66. // 这个要把attribstr 反过来解释处理
  67. - (NSString *)getSendTextStr
  68. {
  69. NSAttributedString *nowattr = self.attributedText;
  70. NSMutableString *retstr = [[NSMutableString alloc] initWithString:nowattr.string];
  71. NSMutableArray *tttt = NSMutableArray.new;
  72. for (int j = 0; j < nowattr.length; j++)
  73. {
  74. NSRange itrang;
  75. NSDictionary *one = [nowattr attributesAtIndex:j effectiveRange:&itrang];
  76. ZWNSTextAttachment *zwattchement = [one objectForKey:@"NSAttachment"];
  77. if (zwattchement)
  78. {
  79. //[retstr replaceCharactersInRange:itrang withString:zwattchement.mFaceNmae];
  80. [tttt addObject:@{ @"l": @(itrang.location),
  81. @"s": @(itrang.length),
  82. @"yy": zwattchement.mFaceNmae }];
  83. }
  84. }
  85. NSInteger fix = 0;
  86. for (NSDictionary *one in tttt)
  87. {
  88. NSRange r;
  89. r.location = [[one objectForKey:@"l"] integerValue];
  90. r.length = [[one objectForKey:@"s"] integerValue] + fix;
  91. fix += r.length - 1;
  92. NSString *sss = [one objectForKey:@"yy"];
  93. NSRange sssrr = [retstr rangeOfString:@"\U0000fffc"];
  94. [retstr replaceCharactersInRange:sssrr withString:sss];
  95. }
  96. return retstr;
  97. }
  98. - (void)appendFace:(NSInteger)faceIndex
  99. {
  100. NSAttributedString *nowattrstr = self.attributedText;
  101. NSMutableAttributedString *finalstr = nil;
  102. if (nowattrstr)
  103. finalstr = [[NSMutableAttributedString alloc] initWithAttributedString:nowattrstr];
  104. else
  105. finalstr = NSMutableAttributedString.new;
  106. ZWNSTextAttachment *faceattachment = [[ZWNSTextAttachment alloc] initWithData:nil ofType:nil];
  107. faceattachment.mFontH = self.font.lineHeight;
  108. NSString *facePicname = [NSString stringWithFormat:@"face%ld.png", faceIndex];
  109. faceattachment.mFaceImg = [UIImage imageNamed:facePicname];
  110. faceattachment.mFaceNmae = [NSString stringWithFormat:@"[face%ld]", faceIndex];
  111. NSAttributedString *faceattr = [NSAttributedString attributedStringWithAttachment:faceattachment];
  112. [finalstr appendAttributedString:faceattr];
  113. [finalstr addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, finalstr.length)];
  114. self.attributedText = finalstr;
  115. }
  116. @end