WeiboReplyData.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // WeiboReplyData.m
  3. // wq8
  4. //
  5. // Created by weqia on 13-9-5.
  6. // Copyright (c) 2013年 Weqia. All rights reserved.
  7. //
  8. #import "WeiboReplyData.h"
  9. #import "NSStrUtil.h"
  10. @implementation WeiboReplyData
  11. @synthesize height,title,messageId,toUserId,toNickName,toBody,body,userId,userNickName,giftCount,giftId,giftName,giftPrice,addHeight,replyId,createTime,height2;
  12. #pragma -mark 接口方法
  13. -(id)init{
  14. self = [super init];
  15. self.userNickName = @"";
  16. addHeight = 0;
  17. return self;
  18. }
  19. +(NSString *)getPrimaryKey
  20. {
  21. return @"replyId";
  22. }
  23. +(NSString *)getTableName
  24. {
  25. return @"WeiboReplyData";
  26. }
  27. +(NSCache*)shareCacheForReply
  28. {
  29. static NSCache * cache=nil;
  30. static dispatch_once_t onceToken;
  31. dispatch_once(&onceToken, ^{
  32. cache=[[NSCache alloc]init];
  33. cache.totalCostLimit=0.3*1024*1024;
  34. });
  35. return cache;
  36. }
  37. -(MatchParser*)getMatch
  38. {
  39. if (_match) {
  40. _match.data=self;
  41. self.height=_match.height;
  42. return _match;
  43. }
  44. NSString *key=[NSString stringWithFormat:@"%@+%f+type:%d",self.body,self.createTime,self.type];
  45. MatchParser *parser=[[WeiboReplyData shareCacheForReply] objectForKey:key];
  46. if (parser) {
  47. _match=parser;
  48. self.height=parser.height;
  49. parser.data=self;
  50. return parser;
  51. }else{
  52. MatchParser* parser=nil;
  53. parser=[self createMatchType1];
  54. if (parser) {
  55. [[WeiboReplyData shareCacheForReply] setObject:parser forKey:key];
  56. }
  57. return parser;
  58. }
  59. }
  60. -(MatchParser*)getMatch:(void(^)(MatchParser *parser,id data))complete data:(id)data
  61. {
  62. if (_match) {
  63. _match.data=self;
  64. self.height=_match.height;
  65. return _match;
  66. }
  67. NSString *key=[NSString stringWithFormat:@"%@+%f+type:%d",self.body,self.createTime,self.type];
  68. MatchParser *parser=[[WeiboReplyData shareCacheForReply] objectForKey:key];
  69. if (parser) {
  70. _match=parser;
  71. self.height=parser.height;
  72. parser.data=self;
  73. return parser;
  74. }else{
  75. __block MatchParser* parser=nil;
  76. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  77. parser=[self createMatchType1];
  78. if (parser) {
  79. _match=parser;
  80. [[WeiboReplyData shareCacheForReply] setObject:parser forKey:key];
  81. complete(parser,data);
  82. }
  83. });
  84. return nil;
  85. }
  86. }
  87. -(void)setMatch
  88. {
  89. if (_match&&[_match isKindOfClass:[MatchParser class]]&&self.title!=nil&&[self.title isKindOfClass:[NSAttributedString class]]) {
  90. return;
  91. }else{
  92. NSString *key=[NSString stringWithFormat:@"%@+%f+type:%d",self.body,self.createTime,self.type];
  93. MatchParser *parser=[[WeiboReplyData shareCacheForReply] objectForKey:key];
  94. if (parser&&self.title!=nil&&[self.title isKindOfClass:[NSAttributedString class]]) {
  95. _match=parser;
  96. self.height=parser.height;
  97. parser.data=self;
  98. }else{
  99. MatchParser* parser=nil;
  100. parser=[self createMatchType1];
  101. if (parser) {
  102. [[WeiboReplyData shareCacheForReply] setObject:parser forKey:key];
  103. }
  104. }
  105. }
  106. }
  107. -(void)setMatch:(MatchParser *)match
  108. {
  109. _match=match;
  110. }
  111. -(MatchParser*)createMatchType1
  112. {
  113. // if([NSStrUtil notEmptyOrNull:self.mid]){
  114. // UIFont*font=[UIFont systemFontOfSize:13];
  115. // UIFont*font2=[UIFont systemFontOfSize:13];
  116. // CTFontRef fontRef=CTFontCreateWithName((__bridge CFStringRef)(font.fontName),font.pointSize,NULL);
  117. // CTFontRef sfontRef=CTFontCreateWithName((__bridge CFStringRef)(font2.fontName),font2.pointSize,NULL);
  118. // NSMutableAttributedString * strings=nil;
  119. // strings=[[NSMutableAttributedString alloc]init];
  120. // ContactData *contact = [[WeqiaAppDelegate App].dbUtil searchSingle:[ContactData class]where:[NSString stringWithFormat:@"mid='%@'",self.mid]orderBy:nil];
  121. // if([NSStrUtil notEmptyOrNull:contact.mName]){
  122. // NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,kCTFontAttributeName,[UIColor colorWithIntegerValue:HEIGHT_TEXT_COLOR alpha:1].CGColor,kCTForegroundColorAttributeName,nil];
  123. // [strings appendAttributedString:[[NSAttributedString alloc] initWithString:contact.mName attributes:dic]];
  124. // if (contact) {
  125. // if([NSStrUtil notEmptyOrNull:self.up_mid]){
  126. // ContactData *up_contact = [[WeqiaAppDelegate App].dbUtil searchSingle:[ContactData class]where:[NSString stringWithFormat:@"mid='%@'",self.up_mid] orderBy:nil];
  127. // if([NSStrUtil notEmptyOrNull:up_contact.mName]){
  128. // NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)sfontRef,kCTFontAttributeName,[UIColor blackColor].CGColor,kCTForegroundColorAttributeName,nil];
  129. // [strings appendAttributedString:[[NSAttributedString alloc] initWithString:@"回复" attributes:dic]];
  130. // dic=[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,kCTFontAttributeName,[UIColor colorWithIntegerValue:HEIGHT_TEXT_COLOR alpha:1].CGColor,kCTForegroundColorAttributeName,nil];
  131. // [strings appendAttributedString:[[NSAttributedString alloc] initWithString:up_contact.mName attributes:dic]];
  132. // }
  133. // }
  134. // }
  135. // }
  136. // NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,kCTFontAttributeName,[UIColor blackColor].CGColor,kCTForegroundColorAttributeName,nil];
  137. // [strings appendAttributedString:[[NSAttributedString alloc] initWithString:@":" attributes:dic]];
  138. // CFRelease(fontRef);
  139. // CFRelease(sfontRef);
  140. // }
  141. UIFont*font=g_factory.font14;
  142. CTFontRef fontRef=CTFontCreateWithName((__bridge CFStringRef)(font.fontName),font.pointSize,NULL);
  143. NSString* s ;
  144. if (toNickName) {
  145. s = [NSString stringWithFormat:@"%@%@%@",userNickName,Localized(@"JX_Reply"),toNickName];
  146. NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]initWithString:s];
  147. [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(0x576b95) range:NSMakeRange(0, [userNickName length])];
  148. [attributedString addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(0x576b95) range:NSMakeRange([userNickName length] +2, [toNickName length])];
  149. [attributedString addAttribute:NSFontAttributeName value:(__bridge id)fontRef range:NSMakeRange(0, [s length])];
  150. self.title = attributedString;
  151. }else{
  152. s = [NSString stringWithFormat:@"%@",userNickName];//回复者的名字
  153. NSMutableAttributedString * strings=[[NSMutableAttributedString alloc]initWithString:s attributes:[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fontRef,kCTFontAttributeName,HEXCOLOR(0x576b95).CGColor,kCTForegroundColorAttributeName,nil]];
  154. self.title=strings;
  155. }
  156. CFRelease(fontRef);
  157. #pragma mark - 点赞换行
  158. //点赞换行宽度-----------
  159. return [self createMatch:JX_SCREEN_WIDTH -117];
  160. }
  161. -(MatchParser*)createMatch:(float)width
  162. {
  163. if(_match==nil||![_match isKindOfClass:[MatchParser class]]){
  164. MatchParser * parser=[[MatchParser alloc]init];
  165. parser.keyWorkColor= [UIColor blueColor];
  166. parser.font=g_factory.font14;
  167. parser.width=width;
  168. NSString* s;
  169. if(self.type == reply_data_praise)
  170. s = self.body;
  171. else
  172. s = [NSString stringWithFormat:@":%@",self.body];
  173. [parser match:s atCallBack:^BOOL(NSString * string) {
  174. return NO;
  175. }title:self.title];
  176. _match=parser;
  177. parser.data=self;
  178. self.height=parser.height+addHeight;
  179. return parser;
  180. }
  181. return nil;
  182. }
  183. -(void)updateMatch:(void(^)(NSMutableAttributedString * string, NSRange range))link
  184. {
  185. if(_match){
  186. NSString* s = [NSString stringWithFormat:@":%@",self.body];
  187. [_match match:s atCallBack:^BOOL(NSString * string) {
  188. return NO;
  189. } title:self.title link:link];
  190. }
  191. }
  192. -(void)getDataFromDict:(NSDictionary*)dict{
  193. self.userId= [[dict objectForKey:@"userId"] stringValue];
  194. self.userNickName = [dict objectForKey:@"nickname"];
  195. self.createTime = [[dict objectForKey:@"time"] longLongValue];
  196. if(self.type == reply_data_praise){
  197. // self.body= [NSString stringWithFormat:@"给了一个赞"];
  198. self.replyId = [dict objectForKey:@"praiseId"];
  199. }
  200. if(self.type == reply_data_gift){
  201. self.replyId = [dict objectForKey:@"giftId"];
  202. self.giftCount = [[dict objectForKey:@"count"] stringValue];
  203. self.giftId = [[dict objectForKey:@"id"] stringValue];
  204. self.giftPrice = [dict objectForKey:@"price"];
  205. self.giftName = [dict objectForKey:@"giftId"];
  206. self.body= [NSString stringWithFormat:@"%@%@",Localized(@"JXLiveVC_Give"),giftName];
  207. }
  208. if(self.type == reply_data_comment){
  209. // self.body= [NSString stringWithFormat:@":%@",[dict objectForKey:@"body"]];
  210. self.body= [dict objectForKey:@"body"];
  211. self.replyId= [dict objectForKey:@"commentId"];
  212. self.toUserId = [[dict objectForKey:@"toUserId"] stringValue];
  213. self.toBody = [dict objectForKey:@"toBody"];
  214. self.toNickName = [dict objectForKey:@"toNickname"];
  215. }
  216. [self setMatch];
  217. }
  218. -(void)getHeight2{
  219. if(height2>0)
  220. return;
  221. height2 = 15;
  222. JXEmoji* p = [[JXEmoji alloc]initWithFrame:CGRectMake(0, 0, 220, 15)];
  223. p.font = g_factory.font11;
  224. p.offset = -12;
  225. p.text = self.body;
  226. height2 += p.frame.size.height;
  227. if([toNickName length]>0 && [toUserId length]>0 && [toBody length]>0){
  228. p.frame = CGRectMake(20, 0, 235,15);
  229. p.font = g_factory.font11;
  230. p.offset = -15;
  231. p.text = self.toBody;
  232. height2 += p.frame.size.height;
  233. height2 += 20;
  234. }
  235. if(height2<60)
  236. height2 = 60;
  237. }
  238. @end