MOTextTools.m 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. //
  2. // MOTextTools.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/12/27.
  6. //
  7. #define kRtmBaseViewHeight 16.0
  8. #import "MOTextTools.h"
  9. #import "NSAttributedString+YYText.h"
  10. #import "UIView+YYAdd.h"
  11. #import "UIImage+YYAdd.h"
  12. #import <SDWebImage/SDWebImage.h>
  13. #import <YYKit/UIImageView+YYWebImage.h>
  14. #import <YYImage.h>
  15. #import "MOFanClubNameplateView.h"
  16. #import "UIImageView+Gift.h"
  17. @implementation MOTextTools
  18. + (NSMutableAttributedString *)creteTextWith:(MORtmEntity *)rtmEntity{
  19. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmEntity.data;
  20. if(jsonEntity && [jsonEntity isKindOfClass:[MORtmJosnEntity class]]){
  21. if(jsonEntity.content.length == 0){
  22. jsonEntity.content = @"";
  23. }
  24. }
  25. else{
  26. jsonEntity = [MORtmJosnEntity new];
  27. jsonEntity.content = @"";
  28. }
  29. NSString *contentStr;
  30. if(rtmEntity.type == MORtmChat){
  31. contentStr = jsonEntity.content;
  32. }
  33. else if (rtmEntity.type == MORtmEnter){
  34. contentStr = [NSString stringWithFormat:@"%@ %@",rtmEntity.user.username,NSLocalString(@"mimo_common_enter_room_anim_tip")];
  35. }
  36. else if (rtmEntity.type == MORoomCleanRtmTip){
  37. contentStr = [NSString stringWithFormat:@"%@ %@",rtmEntity.user.username,NSLocalString(@"mimo_common_live_room_clean_rtm_tip")];
  38. NSMutableAttributedString *tipText = [MOTextTools tipAllWhiteStringWith:contentStr];
  39. return tipText;
  40. }
  41. else if (rtmEntity.type == MORtmRoomTip){
  42. if(jsonEntity.type == 1){
  43. if(jsonEntity.status == 1){
  44. //关注
  45. contentStr = [NSString stringWithFormat:@"%@ %@",rtmEntity.user.username,NSLocalString(@"mimo_common_follow_room_tip")];
  46. }
  47. else{
  48. //取关
  49. contentStr = [NSString stringWithFormat:@"%@ %@",rtmEntity.user.username,NSLocalString(@"mimo_common_no_follow_room_tip")];
  50. }
  51. }
  52. else if (jsonEntity.type == 2){
  53. if(jsonEntity.status == 1){
  54. //胜利
  55. contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_common_pk_room_win_tip"),rtmEntity.user.username];
  56. }
  57. else if (jsonEntity.status == 2){
  58. //自己投降
  59. contentStr = NSLocalString(@"mimo_common_pk_room_surrender_my_tip");
  60. }
  61. else if (jsonEntity.status == 3){
  62. //对方投降
  63. contentStr = NSLocalString(@"mimo_common_pk_room_surrender_other_tip");
  64. }
  65. else if (jsonEntity.status == 4){
  66. //平局
  67. contentStr = NSLocalString(@"mimo_common_pk_room_draw_tip");
  68. }
  69. else{
  70. //失败
  71. contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_common_pk_room_fail_tip"),rtmEntity.user.username];
  72. }
  73. NSMutableAttributedString *tipText = [MOTextTools tipAllWhiteStringWith:contentStr];
  74. return tipText;
  75. }
  76. else if (jsonEntity.type == 3){
  77. if(jsonEntity.status == 1){
  78. //禁言
  79. contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_common_room_silence_tip"),jsonEntity.userName];
  80. }
  81. else{
  82. //解除禁言
  83. contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_common_room_no_silence_tip"),jsonEntity.userName];
  84. }
  85. NSMutableAttributedString *tipText = [MOTextTools tipAllWhiteStringWith:contentStr];
  86. return tipText;
  87. }
  88. else if (jsonEntity.type == 4){
  89. if(jsonEntity.status == 0){
  90. //主播离开
  91. contentStr = NSLocalString(@"mimo_anchor_short_leave_rtm_tip");
  92. }
  93. else{
  94. //主播回来
  95. contentStr = NSLocalString(@"mimo_anchor_short_leave_back_rtm_tip");
  96. }
  97. NSMutableAttributedString *tipText = [MOTextTools tipAllWhiteStringWith:contentStr];
  98. return tipText;
  99. }
  100. else{
  101. contentStr = [NSString stringWithFormat:@"%@: %@",rtmEntity.user.username,jsonEntity.content];
  102. }
  103. }
  104. else if (rtmEntity.type == MOLikeComboTip){
  105. if(jsonEntity.type == 1){
  106. contentStr = [NSString stringWithFormat:@"%@ %@",rtmEntity.user.username,NSLocalString(@"mimo_common_like_first_tip")];
  107. NSMutableAttributedString *tipText = [MOTextTools tipAllWhiteStringWith:contentStr];
  108. return tipText;
  109. }
  110. }
  111. else if (rtmEntity.type == MORoomTipTwo){//房间通用提示 - V2
  112. if(jsonEntity.type == 1){//添加管理员
  113. NSString *contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_2_live_rtm_manager_tip_1"),jsonEntity.userName];
  114. NSMutableAttributedString *tipText = [MOTextTools base_colorfulStringWith:contentStr AndNameStr:jsonEntity.userName];
  115. NSString *fanClubStr = NSLocalString(@"mimo_2_live_rtm_manager_tip_2");
  116. if(fanClubStr.length != 0){
  117. NSRange redRange = [contentStr rangeOfString:fanClubStr];
  118. if(redRange.location != NSNotFound){
  119. // 将最后三个字符的颜色设置为红色
  120. [tipText addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:redRange];
  121. }
  122. }
  123. return tipText;
  124. }
  125. }
  126. else if (rtmEntity.type == MORtmGiftEndNum){
  127. NSMutableAttributedString *giftNumText = [MOTextTools creteGiftNumTextWith:rtmEntity];
  128. return giftNumText;
  129. }
  130. else if (rtmEntity.type == SystemTipGetRed){
  131. NSMutableAttributedString *giftNumText = [MOTextTools creteRedTipWith:rtmEntity];
  132. return giftNumText;
  133. }
  134. else if (rtmEntity.type == SystemOrder){
  135. if(jsonEntity.type == 9 || jsonEntity.type == 10 || jsonEntity.type == 11){
  136. NSMutableAttributedString *giftNumText = [MOTextTools creteRedTipWith:rtmEntity];
  137. return giftNumText;
  138. }
  139. else if (jsonEntity.type == 24){//加入粉丝团
  140. NSString *contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_2_live_rtm_fan_club_tip_1"),jsonEntity.roomUser.nickname];
  141. NSMutableAttributedString *tipText = [MOTextTools base_colorfulStringWith:contentStr AndNameStr:jsonEntity.roomUser.nickname];
  142. NSString *fanClubStr = NSLocalString(@"mimo_2_live_rtm_fan_club_tip_2");
  143. if(fanClubStr.length != 0){
  144. NSRange redRange = [contentStr rangeOfString:fanClubStr];
  145. if(redRange.location != NSNotFound){
  146. // 将最后三个字符的颜色设置为红色
  147. [tipText addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:redRange];
  148. }
  149. }
  150. return tipText;
  151. }
  152. else if (jsonEntity.type == 26){//用户升级通知
  153. MORtmUserLevelUpgrade *userLevel = jsonEntity.levelNotice;
  154. NSMutableAttributedString *baseAttri = [[NSMutableAttributedString alloc] initWithString:@" "];
  155. NSMutableAttributedString *vipAttri = [self toAddLevelInfoWith:baseAttri And:userLevel.afterLevel];
  156. NSString *contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_2_live_rtm_level_up_1"),jsonEntity.roomUser.nickname];
  157. NSMutableAttributedString *tipText = [MOTextTools base_colorfulStringWith:contentStr AndNameStr:jsonEntity.roomUser.nickname];
  158. // 设置字体大小的属性
  159. [tipText addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, tipText.length)];
  160. [tipText appendAttributedString:vipAttri];
  161. return tipText;
  162. } else if (jsonEntity.type == 33) {//盲盒礼物中奖推送
  163. return [self createBlindBoxGiftDrawTextWith:jsonEntity];
  164. }
  165. }
  166. else if (rtmEntity.type == MOChangeRoomType){
  167. if(jsonEntity.type == 2){
  168. contentStr = NSLocalString(@"mimo_ticket_change_success_tip");
  169. NSMutableAttributedString *tipText = [MOTextTools tipAllWhiteStringWith:contentStr];
  170. return tipText;
  171. }
  172. }
  173. else if (rtmEntity.type == SystemTipNormalBlue || rtmEntity.type == SystemTipBlindBox){
  174. contentStr = jsonEntity.content;
  175. NSMutableAttributedString *tipText = [MOTextTools tipAllWhiteStringWith:contentStr];
  176. return tipText;
  177. }
  178. else if (rtmEntity.type == SystemTipHitDiamond){
  179. NSMutableAttributedString *tipText = [MOTextTools creteTheHitDiamondTipWith:rtmEntity];
  180. return tipText;
  181. }
  182. NSMutableAttributedString *plainText = [MOTextTools base_colorfulStringWith:contentStr AndNameStr:rtmEntity.user.username];
  183. if(jsonEntity.atNameList.length != 0){
  184. plainText = [self add_atColorfulStringWith:plainText AndAtString:jsonEntity.atNameList];
  185. [plainText addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, plainText.length)];
  186. return plainText;
  187. }
  188. if(rtmEntity.type == MORtmChat){
  189. [plainText addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, plainText.length)];
  190. return plainText;
  191. }
  192. if(rtmEntity.type == MORtmEnter){
  193. plainText = [MOTextTools tipAllWhiteStringWith:contentStr];
  194. }
  195. if((rtmEntity.type == MORtmRoomTip && jsonEntity.status == 1)||
  196. rtmEntity.type == MORoomCleanRtmTip){
  197. plainText = [MOTextTools tipAllWhiteStringWith:contentStr];
  198. }
  199. // 设置字体大小的属性
  200. [plainText addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, plainText.length)];
  201. //加粉丝团
  202. [self toAddFanClubInfoWith:plainText And:rtmEntity.user.fans];
  203. [plainText insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:0];
  204. //加勋章
  205. // [self toAddMedalInfoWith:plainText And:rtmEntity.user.medalCodeList];
  206. //加vip
  207. [self toAddVipInfoWith:plainText And:rtmEntity.user.vip.type];
  208. //加等级
  209. [self toAddLevelInfoWith:plainText And:rtmEntity.user.level];
  210. [plainText addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, plainText.length)];
  211. return plainText;
  212. }
  213. //盲盒玩法 中奖礼物
  214. + (NSMutableAttributedString *)createBlindBoxGiftDrawTextWith:(MORtmJosnEntity *)jsonEntity {
  215. MORtmBlindboxDrawResult *result = jsonEntity.blindboxDrawResult;
  216. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init];
  217. if (result.isShowGift) {//礼物
  218. NSString *prefix = nil;
  219. NSString *suffix = nil;
  220. if (result.noticeType == 1) {
  221. prefix = [NSString stringWithFormat:@"%@%@", result.nickname, NSLocalString(@"C20017-1")];
  222. suffix = NSLocalString(@"C20017-2");
  223. } else if (result.noticeType == 2) {
  224. prefix = [NSString stringWithFormat:@"%@%@", result.nickname, NSLocalString(@"C20018-1")];
  225. suffix = @"";
  226. } else if (result.noticeType == 3) {
  227. prefix = [NSString stringWithFormat:@"%@%@", result.nickname, NSLocalString(@"C20019-1")];
  228. suffix = @"";
  229. }
  230. // 前缀
  231. [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:prefix
  232. attributes:@{NSForegroundColorAttributeName: UIColor.whiteColor,
  233. NSFontAttributeName: MOTextLabelFont}]];
  234. if (result.nickname.length > 0) {
  235. NSRange nameRange = [prefix rangeOfString:result.nickname];
  236. if (nameRange.location != NSNotFound) {
  237. [attrStr addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:nameRange];
  238. }
  239. }
  240. for (MORtmBlindboxGift *gift in result.list) {
  241. MOGiftInfo *giftInfo = [[MOSvgaSourceManage shareManager] getGiftInfoWithCode:gift.giftCode];
  242. if(giftInfo){
  243. UIImage *cacheImage = [[SDImageCache giftCache] imageFromDiskCacheForKey:giftInfo.giftPath];
  244. if(cacheImage){
  245. CGSize cacheSize;
  246. cacheImage = [cacheImage imageByResizeToSize:CGSizeMake(cacheImage.size.width / 2.0, cacheImage.size.height / 2.0) contentMode:UIViewContentModeScaleAspectFit];
  247. NSTextAttachment *vipAttachment;
  248. vipAttachment = [[NSTextAttachment alloc] init];
  249. vipAttachment.image = cacheImage;
  250. //字体行高
  251. UIFont *font = MOTextLabelFont;
  252. CGFloat attachmentHeight = font.lineHeight;
  253. CGFloat imgSizeHeight = GetParamNotZero(cacheImage.size.height);
  254. CGFloat attachmentWidth = attachmentHeight * cacheImage.size.width/ imgSizeHeight;
  255. vipAttachment.bounds = CGRectMake(0, (font.capHeight-font.lineHeight)/2, attachmentWidth, attachmentHeight);
  256. cacheSize = CGSizeMake(attachmentWidth, attachmentHeight);
  257. NSMutableAttributedString *cacheImageAttri = [NSMutableAttributedString attachmentStringWithContent:cacheImage contentMode:UIViewContentModeScaleAspectFit attachmentSize:cacheSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  258. [attrStr appendAttributedString:cacheImageAttri];
  259. NSString *giftNumStr = [NSString stringWithFormat:@" x%zd",gift.amount];
  260. NSMutableAttributedString *giftText;
  261. giftText = [[NSMutableAttributedString alloc] initWithString:giftNumStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#F5E87F" alpha:1.0], NSFontAttributeName:MOTextLabelFont}];
  262. [attrStr appendAttributedString:giftText];
  263. }
  264. else{
  265. NSMutableAttributedString *giftText;
  266. NSString *giftNumStr = [NSString stringWithFormat:@" %@ x%zd",giftInfo.giftName,gift.amount];
  267. giftText = [[NSMutableAttributedString alloc] initWithString:giftNumStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#F5E87F" alpha:1.0], NSFontAttributeName:MOTextLabelFont}];
  268. [attrStr appendAttributedString:giftText];
  269. }
  270. }
  271. else{
  272. NSMutableAttributedString *giftText;
  273. NSString *giftNumStr = [NSString stringWithFormat:@" x%zd",gift.amount];
  274. giftText = [[NSMutableAttributedString alloc] initWithString:giftNumStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#F5E87F" alpha:1.0]}];
  275. [attrStr appendAttributedString:giftText];
  276. }
  277. }
  278. if (suffix.length > 0) {
  279. // 后缀
  280. [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:suffix
  281. attributes:@{NSForegroundColorAttributeName: UIColor.whiteColor,
  282. NSFontAttributeName: MOTextLabelFont}]];
  283. }
  284. } else {//装扮
  285. NSString *prefix = nil;
  286. NSString *suffix = nil;
  287. if (result.noticeType == 1) {
  288. prefix = [NSString stringWithFormat:@"%@%@", result.nickname, [NSString stringWithFormat:NSLocalString(@"C20020-1"), NSLocalString(@"C20020-0")]];
  289. suffix = [NSString stringWithFormat:NSLocalString(@"C20020-2"), NSLocalString(@"C20020-0")];
  290. } else if (result.noticeType == 2) {
  291. prefix = [NSString stringWithFormat:@"%@%@", result.nickname, [NSString stringWithFormat:NSLocalString(@"C20021-1"), NSLocalString(@"C20020-0")]];
  292. suffix = [NSString stringWithFormat:NSLocalString(@"C20021-2"), NSLocalString(@"C20020-0")];
  293. } else if (result.noticeType == 3) {
  294. prefix = [NSString stringWithFormat:@"%@%@", result.nickname, [NSString stringWithFormat:NSLocalString(@"C20022-1"), NSLocalString(@"C20020-0")]];
  295. suffix = [NSString stringWithFormat:NSLocalString(@"C20022-2"), NSLocalString(@"C20020-0")];
  296. }
  297. // 前缀
  298. [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:prefix
  299. attributes:@{NSForegroundColorAttributeName: UIColor.whiteColor,
  300. NSFontAttributeName: MOTextLabelFont}]];
  301. if (result.nickname.length > 0) {
  302. NSRange nameRange = [prefix rangeOfString:result.nickname];
  303. if (nameRange.location != NSNotFound) {
  304. [attrStr addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:nameRange];
  305. }
  306. }
  307. NSString *propString = [self createBlindBoxDeawResultPropString:result.propNotices];
  308. if (propString.length > 0) {
  309. [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:propString
  310. attributes:@{NSForegroundColorAttributeName:kBaseTextHightlightColor_1,
  311. NSFontAttributeName: MOTextLabelFont}]];
  312. NSRange nameRange = [prefix rangeOfString:NSLocalString(@"C20020-0")];
  313. if (nameRange.location != NSNotFound) {
  314. [attrStr addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:nameRange];
  315. }
  316. }
  317. if (suffix.length > 0) {
  318. // 后缀开始位置
  319. NSUInteger startLocation = attrStr.length;
  320. [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:suffix
  321. attributes:@{NSForegroundColorAttributeName: UIColor.whiteColor,
  322. NSFontAttributeName: MOTextLabelFont}]];
  323. NSRange nameRangeInSuffix = [suffix rangeOfString:NSLocalString(@"C20020-0")];
  324. if (nameRangeInSuffix.location != NSNotFound) {
  325. NSRange adjustedRange = NSMakeRange(startLocation + nameRangeInSuffix.location, nameRangeInSuffix.length);
  326. [attrStr addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:adjustedRange];
  327. }
  328. }
  329. }
  330. return attrStr;
  331. }
  332. + (NSString *)createBlindBoxDeawResultPropString:(NSArray<NSString *> *)propNotices {
  333. NSMutableString *propString = [NSMutableString string];
  334. [propNotices enumerateObjectsUsingBlock:^(NSString * _Nonnull notice, NSUInteger idx, BOOL * _Nonnull stop) {
  335. [propString appendString:notice];
  336. if (idx != (propNotices.count - 1)) {
  337. [propString appendString:@"、"];
  338. }
  339. }];
  340. return propString;
  341. }
  342. //系统消息 26的顶部提示
  343. + (NSMutableAttributedString *)createTheLevelUpNoticeWith:(MORtmEntity *)rtmEntity{
  344. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmEntity.data;
  345. if(jsonEntity && [jsonEntity isKindOfClass:[MORtmJosnEntity class]]){
  346. if(jsonEntity.content.length == 0){
  347. jsonEntity.content = @"";
  348. }
  349. }
  350. else{
  351. jsonEntity = [MORtmJosnEntity new];
  352. jsonEntity.content = @"";
  353. }
  354. MORtmUserLevelUpgrade *userLevel = jsonEntity.levelNotice;
  355. NSMutableAttributedString *baseAttri = [[NSMutableAttributedString alloc] initWithString:@" "];//初始化
  356. //第一个
  357. NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:NSLocalString(@"mimo_2_live_rtm_level_up_2")];//顶部
  358. [title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)];
  359. //头像
  360. if(jsonEntity.roomUser.avatar.length > 0){
  361. [self addTheUserHeadWith:title And:jsonEntity.roomUser.avatar];
  362. }
  363. //后面
  364. NSString *contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_2_live_rtm_level_up_3"),jsonEntity.roomUser.nickname];
  365. NSMutableAttributedString *tipText = [MOTextTools base_colorfulStringWith:contentStr AndNameStr:jsonEntity.roomUser.nickname];
  366. [title appendAttributedString:tipText];
  367. // 设置字体大小的属性
  368. [title addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, title.length)];
  369. //等级
  370. NSMutableAttributedString *vipAttri = [self toAddLevelInfoWith:baseAttri And:userLevel.afterLevel];//等级
  371. [title appendAttributedString:vipAttri];
  372. return title;
  373. }
  374. + (NSMutableAttributedString *)addTheUserHeadWith:(NSMutableAttributedString *)contentStr And:(NSString *)headUrl{
  375. if(headUrl.length == 0){
  376. return contentStr;
  377. }
  378. //这里调整的话, 请同时调整 MOBaseLevelView
  379. CGFloat levelWidth = 17;
  380. CGFloat levelLabelX = 17.0;
  381. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, levelWidth, levelWidth)];
  382. bgView.backgroundColor = [UIColor clearColor];
  383. bgView.layer.cornerRadius = levelWidth / 2.0;
  384. bgView.layer.masksToBounds = YES;
  385. YYAnimatedImageView *levelImg = [[YYAnimatedImageView alloc] init];
  386. levelImg.frame = CGRectMake(0.0, 0.0, levelWidth, levelWidth);
  387. levelImg.contentMode = UIViewContentModeScaleAspectFit;
  388. NSURL *url = [NSURL URLWithString:headUrl];
  389. [levelImg setImageWithURL:url placeholder:[UIImage imageNamed:@"icon_mine_placeHolder"]];
  390. [bgView addSubview:levelImg];
  391. CGSize viewSize = bgView.size;
  392. NSMutableAttributedString *levelImgAttri = [NSMutableAttributedString attachmentStringWithContent:bgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:viewSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  393. [levelImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  394. [contentStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  395. [contentStr appendAttributedString:levelImgAttri];
  396. return contentStr;
  397. }
  398. ///组建 - 普通聊天头部
  399. + (NSMutableAttributedString *)creteTitleTextWith:(MORtmEntity *)rtmEntity{
  400. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmEntity.data;
  401. if(jsonEntity && [jsonEntity isKindOfClass:[MORtmJosnEntity class]]){
  402. if(jsonEntity.content.length == 0){
  403. jsonEntity.content = @"";
  404. }
  405. }
  406. else{
  407. jsonEntity = [MORtmJosnEntity new];
  408. jsonEntity.content = @"";
  409. }
  410. NSString *contentStr;
  411. if(rtmEntity.type == MORtmChat){
  412. if(rtmEntity.user.username.length == 0){
  413. contentStr = @"";
  414. }
  415. else{
  416. contentStr = rtmEntity.user.username;
  417. }
  418. }
  419. NSMutableAttributedString *plainText = [MOTextTools base_colorfulStringWith:contentStr AndNameStr:rtmEntity.user.username];
  420. //加粉丝团
  421. [self toAddFanClubInfoWith:plainText And:rtmEntity.user.fans];
  422. [plainText insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:0];
  423. //加vip
  424. [self toAddVipInfoWith:plainText And:rtmEntity.user.vip.type];
  425. //加等级
  426. [self toAddLevelInfoWith:plainText And:rtmEntity.user.level];
  427. // 设置字体大小的属性
  428. [plainText addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, plainText.length)];
  429. return plainText;
  430. }
  431. + (NSMutableAttributedString *)addTempGifWith:(NSMutableAttributedString *)contentStr{
  432. UIView *giftBgView = [[UIView alloc] init];
  433. giftBgView.backgroundColor = [UIColor redColor];
  434. YYImage *giftImg = [YYImage imageNamed:@"gif_icon_explore.gif"];
  435. YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:giftImg];
  436. imageView.frame = CGRectMake(0.0, 0.0, 16.0, 16.0);
  437. UILabel *levelLab = [[UILabel alloc] init];
  438. levelLab.text = @"66";
  439. levelLab.font = [MOTextTools getTheFontWithSize:12.0 AndFontName:kNormalTitleFontStr];
  440. levelLab.textColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:1.0];
  441. levelLab.frame = CGRectMake(0.0, 0.0, 16.0, 16.0);
  442. [giftBgView addSubview:imageView];
  443. [giftBgView addSubview:levelLab];
  444. giftBgView.frame = CGRectMake(0.0, 0.0, 16.0, 16.0);
  445. CGSize viewSize = giftBgView.frame.size;
  446. NSMutableAttributedString *levelImgAttri = [NSMutableAttributedString attachmentStringWithContent:giftBgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:viewSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  447. [contentStr appendAttributedString:levelImgAttri];
  448. return contentStr;
  449. }
  450. + (NSMutableAttributedString *)creteEnterTextWith:(MORtmEntity *)rtmEntity{
  451. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmEntity.data;
  452. if(jsonEntity && [jsonEntity isKindOfClass:[MORtmJosnEntity class]]){
  453. if(jsonEntity.content.length == 0){
  454. jsonEntity.content = @"";
  455. }
  456. }
  457. else{
  458. jsonEntity = [MORtmJosnEntity new];
  459. jsonEntity.content = @"";
  460. }
  461. NSString *contentStr;
  462. if(rtmEntity.type == MORtmChat){
  463. contentStr = [NSString stringWithFormat:@"%@: %@",rtmEntity.user.username,jsonEntity.content];
  464. }
  465. else if (rtmEntity.type == MORtmEnter){
  466. contentStr = rtmEntity.user.username;
  467. }
  468. NSMutableAttributedString *plainText = [[NSMutableAttributedString alloc] initWithString:contentStr];
  469. // 设置字体大小的属性
  470. [plainText addAttribute:NSFontAttributeName value:[MOTextTools regularFont:10.0] range:NSMakeRange(0, plainText.length)];
  471. NSRange redRange = [contentStr rangeOfString:rtmEntity.user.username];
  472. if(redRange.location != NSNotFound){
  473. // 将最后三个字符的颜色设置为红色
  474. [plainText addAttribute:NSFontAttributeName value:[MOTextTools mediumFont:10.0] range:redRange];
  475. }
  476. //加vip
  477. if(rtmEntity.user.vip.type > 0){
  478. [self toAddSmallVipInfoWith:plainText And:rtmEntity.user.vip.type AndHeight:14.0];
  479. }
  480. //加等级
  481. [self toAddSmallLevelInfoWith:plainText And:rtmEntity.user.level AndHeight:14.0 AndAlignToFont:[MOTextTools regularFont:10.0]];
  482. return plainText;
  483. }
  484. //根据Rtm消息, 生成礼物数量NSMutableAttributedString
  485. + (NSMutableAttributedString *)creteGiftNumTextWith:(MORtmEntity *)rtmEntity{
  486. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmEntity.data;
  487. if(jsonEntity && [jsonEntity isKindOfClass:[MORtmJosnEntity class]]){
  488. if(jsonEntity.content.length == 0){
  489. jsonEntity.content = @"";
  490. }
  491. }
  492. else{
  493. jsonEntity = [MORtmJosnEntity new];
  494. jsonEntity.content = @"";
  495. }
  496. NSString *contentStr = [NSString stringWithFormat:@"%@ Send gift ",rtmEntity.user.username];
  497. NSMutableAttributedString *plainText = [MOTextTools colorfulStringWith:contentStr AndNameStr:rtmEntity.user.username];
  498. NSMutableAttributedString *giftText;
  499. //礼物
  500. MOGiftInfo *giftInfo = [[MOSvgaSourceManage shareManager] getGiftInfoWithCode:jsonEntity.code];
  501. if(giftInfo){
  502. UIImage *cacheImage = [[SDImageCache giftCache] imageFromDiskCacheForKey:giftInfo.giftPath];
  503. if(cacheImage){
  504. CGSize cacheSize;
  505. cacheImage = [cacheImage imageByResizeToSize:CGSizeMake(cacheImage.size.width / 2.0, cacheImage.size.height / 2.0) contentMode:UIViewContentModeScaleAspectFit];
  506. NSTextAttachment *vipAttachment;
  507. vipAttachment = [[NSTextAttachment alloc] init];
  508. vipAttachment.image = cacheImage;
  509. //字体行高
  510. UIFont *font = MOTextLabelFont;
  511. CGFloat attachmentHeight = font.lineHeight;
  512. CGFloat imgSizeHeight = GetParamNotZero(cacheImage.size.height);
  513. CGFloat attachmentWidth = attachmentHeight * cacheImage.size.width/ imgSizeHeight;
  514. vipAttachment.bounds = CGRectMake(0, (font.capHeight-font.lineHeight)/2, attachmentWidth, attachmentHeight);
  515. cacheSize = CGSizeMake(attachmentWidth, attachmentHeight);
  516. NSMutableAttributedString *cacheImageAttri = [NSMutableAttributedString attachmentStringWithContent:cacheImage contentMode:UIViewContentModeScaleAspectFit attachmentSize:cacheSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  517. // NSMutableAttributedString *cacheImageAttri = [[NSAttributedString attributedStringWithAttachment:vipAttachment] mutableCopy];
  518. [plainText appendAttributedString:cacheImageAttri];
  519. NSString *giftNumStr = [NSString stringWithFormat:@" x%zd",jsonEntity.count];
  520. giftText = [[NSMutableAttributedString alloc] initWithString:giftNumStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#EFE85C" alpha:1.0]}];
  521. [plainText appendAttributedString:giftText];
  522. }
  523. else{
  524. NSString *giftNumStr = [NSString stringWithFormat:@" %@ x%zd",giftInfo.giftName,jsonEntity.count];
  525. giftText = [[NSMutableAttributedString alloc] initWithString:giftNumStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#EFE85C" alpha:1.0]}];
  526. [plainText appendAttributedString:giftText];
  527. }
  528. }
  529. else{
  530. NSString *giftNumStr = [NSString stringWithFormat:@" x%zd",jsonEntity.count];
  531. giftText = [[NSMutableAttributedString alloc] initWithString:giftNumStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#EFE85C" alpha:1.0]}];
  532. [plainText appendAttributedString:giftText];
  533. }
  534. //加粉丝团
  535. [self toAddFanClubInfoWith:plainText And:rtmEntity.user.fans];
  536. [plainText insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:0];
  537. //加勋章
  538. // [self toAddMedalInfoWith:plainText And:rtmEntity.user.medalCodeList];
  539. //加vip
  540. [self toAddVipInfoWith:plainText And:rtmEntity.user.vip.type];
  541. //加等级
  542. [self toAddLevelInfoWith:plainText And:rtmEntity.user.level];
  543. // 设置字体大小的属性
  544. [plainText addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, plainText.length)];
  545. return plainText;
  546. }
  547. + (NSMutableAttributedString *)creteTheHitDiamondTipWith:(MORtmEntity *)entity{
  548. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)entity.data;
  549. NSString *contentStr = [NSString stringWithFormat:NSLocalString(@"C40002_2"),jsonEntity.hitDiamond];
  550. NSMutableAttributedString *contentAttri = [[NSMutableAttributedString alloc] initWithString:contentStr];
  551. [self toAddHitDiamondWith:contentAttri];
  552. NSAttributedString *tipOne = [[NSAttributedString alloc] initWithString:NSLocalString(@"C40002_1")];
  553. [contentAttri insertAttributedString:tipOne atIndex:0];
  554. [contentAttri addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:@"#FFFFFF" alpha:1.0] range:NSMakeRange(0, contentAttri.length)];
  555. NSString *userName = entity.user.username;
  556. if(userName.length == 0){
  557. userName = @" ";
  558. }
  559. NSAttributedString *nameAttri = [[NSAttributedString alloc] initWithString:userName attributes:@{NSForegroundColorAttributeName:kBaseTextHightlightColor_1}];
  560. [contentAttri insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:0];
  561. [contentAttri insertAttributedString:nameAttri atIndex:0];
  562. //加粉丝团
  563. [self toAddFanClubInfoWith:contentAttri And:entity.user.fans];
  564. [contentAttri insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:0];
  565. //加vip
  566. [self toAddVipInfoWith:contentAttri And:entity.user.vip.type];
  567. //加等级
  568. [self toAddLevelInfoWith:contentAttri And:entity.user.level];
  569. [contentAttri addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, contentAttri.length)];
  570. return contentAttri;
  571. }
  572. + (NSMutableAttributedString *)toAddHitDiamondWith:(NSMutableAttributedString *)contentStr{
  573. CGFloat theBaseViewHeight = kRtmBaseViewHeight - 1.0;
  574. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, theBaseViewHeight, theBaseViewHeight)];
  575. bgView.backgroundColor = [UIColor clearColor];
  576. UIImageView *medalImg = [[UIImageView alloc] init];
  577. medalImg.frame = CGRectMake(0.0, 0.0, theBaseViewHeight, theBaseViewHeight);
  578. medalImg.contentMode = UIViewContentModeScaleAspectFit;
  579. [medalImg setImage:[UIImage imageNamed:@"v_2_top_zuan_24"]];
  580. [bgView addSubview:medalImg];
  581. NSMutableAttributedString *vipImgAttri = [NSMutableAttributedString attachmentStringWithContent:bgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(theBaseViewHeight, theBaseViewHeight) alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  582. [vipImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  583. [contentStr insertAttributedString:[vipImgAttri copy] atIndex:0];
  584. return contentStr;
  585. }
  586. + (NSMutableAttributedString *)creteRedTipWith:(MORtmEntity *)rtmEntity{
  587. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmEntity.data;
  588. NSString *contentStr;
  589. if(jsonEntity.type == 9 || jsonEntity.type == 10){
  590. contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_red_packet_send_red_rtm_tip"),jsonEntity.redEnvelope.senderNickname];
  591. }
  592. else if (jsonEntity.type == 11){
  593. contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_red_packet_recenved_red_rtm_tip")];
  594. }
  595. else{
  596. contentStr = [NSString stringWithFormat:NSLocalString(@"mimo_red_packet_get_red_rtm_tip"),jsonEntity.redSendName];
  597. }
  598. NSMutableAttributedString *plainText;
  599. if(jsonEntity.type == 9 || jsonEntity.type == 10 ){
  600. plainText = [MOTextTools base_colorfulStringWith:contentStr AndNameStr:jsonEntity.redEnvelope.senderNickname AndBaseColor:@"#FFFFFF" AndNameColor:@"#19FFB2"];
  601. }
  602. else if (rtmEntity.type == SystemTipGetRed){
  603. plainText = [MOTextTools base_colorfulStringWith:contentStr AndNameStr:jsonEntity.redSendName AndBaseColor:@"#FFFFFF" AndNameColor:@"#19FFB2"];
  604. }
  605. else{
  606. plainText = [MOTextTools tipAllWhiteStringWith:contentStr];
  607. }
  608. [plainText addAttribute:NSFontAttributeName value:MOTextLabelFont range:NSMakeRange(0, plainText.length)];
  609. [plainText appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  610. if(!(jsonEntity.type == 9 || jsonEntity.type == 10)){
  611. //非发红包
  612. UIImage *redImg = [UIImage imageNamed:@"icon_red_rtm_tip"];
  613. redImg = [redImg imageByResizeToSize:CGSizeMake(18.0, 18.0) contentMode:UIViewContentModeScaleAspectFit];
  614. NSTextAttachment *redAttachment;
  615. if(redImg){
  616. CGSize redSize;
  617. redAttachment = [[NSTextAttachment alloc] init];
  618. redAttachment.image = redImg;
  619. //字体行高
  620. UIFont *font = MOTextLabelFont;
  621. CGFloat attachmentHeight = font.lineHeight;
  622. CGFloat imgSizeHeight = GetParamNotZero(redImg.size.height);
  623. CGFloat attachmentWidth = attachmentHeight * redImg.size.width/ imgSizeHeight;
  624. redAttachment.bounds = CGRectMake(0, (font.capHeight-font.lineHeight)/2, attachmentWidth, attachmentHeight);
  625. redSize = CGSizeMake(attachmentWidth, attachmentHeight);
  626. //生成NSAttributedString
  627. NSMutableAttributedString *redImgAttri = [NSMutableAttributedString attachmentStringWithContent:redImg contentMode:UIViewContentModeScaleAspectFit attachmentSize:redSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  628. // NSMutableAttributedString *redImgAttri = [[NSAttributedString attributedStringWithAttachment:redAttachment] mutableCopy];
  629. [redImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  630. [plainText appendAttributedString:[redImgAttri copy]];
  631. }
  632. UIImage *rightImg = [UIImage imageNamed:@"icon_rtm_right"];
  633. rightImg = [rightImg imageByResizeToSize:CGSizeMake(8.0, 6.0) contentMode:UIViewContentModeScaleAspectFit];
  634. NSTextAttachment *rightAttachment;
  635. if(rightImg){
  636. CGSize rightSize;
  637. rightAttachment = [[NSTextAttachment alloc] init];
  638. rightAttachment.image = rightImg;
  639. //字体行高
  640. UIFont *font = MOTextLabelFont;
  641. rightAttachment.bounds = CGRectMake(0, 0, 8.0, 6.0);
  642. rightSize = CGSizeMake(8.0, 6.0);
  643. //生成NSAttributedString
  644. NSMutableAttributedString *rightImgAttri = [NSMutableAttributedString attachmentStringWithContent:rightImg contentMode:UIViewContentModeScaleAspectFit attachmentSize:rightSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  645. // NSMutableAttributedString *rightImgAttri = [[NSAttributedString attributedStringWithAttachment:rightAttachment] mutableCopy];
  646. [rightImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  647. [plainText appendAttributedString:[rightImgAttri copy]];
  648. }
  649. }
  650. return plainText;
  651. }
  652. + (NSMutableAttributedString *)toAddVipInfoWith:(NSMutableAttributedString *)contentStr And:(NSInteger)vipType{
  653. CGFloat theBaseViewHeight = kRtmBaseViewHeight - 1.0;
  654. if(vipType != 0){
  655. //VIP 对象
  656. MOBaseVips *vipObject = [[MOSvgaSourceManage shareManager] toGetTheVipObjectWith:vipType];
  657. if(vipObject){
  658. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, theBaseViewHeight, theBaseViewHeight)];
  659. bgView.backgroundColor = [UIColor clearColor];
  660. YYAnimatedImageView *medalImg = [[YYAnimatedImageView alloc] init];
  661. medalImg.frame = CGRectMake(0.0, 0.0, theBaseViewHeight, theBaseViewHeight);
  662. medalImg.contentMode = UIViewContentModeScaleAspectFit;
  663. NSString *urlStr = vipObject.thumbnail;
  664. NSURL *url = [NSURL URLWithString:urlStr];
  665. [medalImg setImageWithURL:url placeholder:nil];
  666. [bgView addSubview:medalImg];
  667. NSMutableAttributedString *vipImgAttri = [NSMutableAttributedString attachmentStringWithContent:bgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(theBaseViewHeight, theBaseViewHeight) alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  668. [vipImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  669. [contentStr insertAttributedString:[vipImgAttri copy] atIndex:0];
  670. }
  671. else{
  672. //旧版 VIP 静态图
  673. UIImage *vipImg = [MOTextTools getVipIconWith:vipType];
  674. NSTextAttachment *vipAttachment;
  675. if(vipImg){
  676. CGSize vipSize;
  677. vipAttachment = [[NSTextAttachment alloc] init];
  678. vipAttachment.image = vipImg;
  679. //字体行高
  680. UIFont *font = MOTextLabelFont;
  681. CGFloat attachmentHeight = font.lineHeight;
  682. CGFloat imgSizeHeight = GetParamNotZero(vipImg.size.height);
  683. CGFloat attachmentWidth = attachmentHeight * vipImg.size.width/ imgSizeHeight;
  684. vipAttachment.bounds = CGRectMake(0, (font.capHeight-font.lineHeight)/2, attachmentWidth, attachmentHeight);
  685. vipSize = CGSizeMake(attachmentWidth, attachmentHeight);
  686. NSMutableAttributedString *vipImgAttri = [NSMutableAttributedString attachmentStringWithContent:vipImg contentMode:UIViewContentModeScaleAspectFit attachmentSize:vipSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  687. //生成NSAttributedString
  688. // NSMutableAttributedString *vipImgAttri = [[NSAttributedString attributedStringWithAttachment:vipAttachment] mutableCopy];
  689. [vipImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  690. [contentStr insertAttributedString:[vipImgAttri copy] atIndex:0];
  691. }
  692. }
  693. }
  694. return contentStr;
  695. }
  696. + (NSMutableAttributedString *)toAddSmallVipInfoWith:(NSMutableAttributedString *)contentStr And:(NSInteger)vipType AndHeight:(CGFloat)viewHeight{
  697. CGFloat theBaseViewHeight = 14;
  698. if(viewHeight > 0){
  699. theBaseViewHeight = viewHeight;
  700. }
  701. if(vipType != 0){
  702. //VIP 对象
  703. MOBaseVips *vipObject = [[MOSvgaSourceManage shareManager] toGetTheVipObjectWith:vipType];
  704. if(vipObject){
  705. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, theBaseViewHeight, theBaseViewHeight)];
  706. bgView.backgroundColor = [UIColor clearColor];
  707. YYAnimatedImageView *medalImg = [[YYAnimatedImageView alloc] init];
  708. medalImg.frame = CGRectMake(0.0, 0.0, theBaseViewHeight, theBaseViewHeight);
  709. medalImg.contentMode = UIViewContentModeScaleAspectFit;
  710. NSString *urlStr = vipObject.thumbnail;
  711. NSURL *url = [NSURL URLWithString:urlStr];
  712. [medalImg setImageWithURL:url placeholder:nil];
  713. [bgView addSubview:medalImg];
  714. NSMutableAttributedString *vipImgAttri = [NSMutableAttributedString attachmentStringWithContent:bgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(theBaseViewHeight, theBaseViewHeight) alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  715. [vipImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  716. [contentStr insertAttributedString:[vipImgAttri copy] atIndex:0];
  717. }
  718. else{
  719. //旧版 VIP 静态图
  720. UIImage *vipImg = [MOTextTools getVipIconWith:vipType];
  721. NSTextAttachment *vipAttachment;
  722. if(vipImg){
  723. CGSize vipSize;
  724. vipAttachment = [[NSTextAttachment alloc] init];
  725. vipAttachment.image = vipImg;
  726. //字体行高
  727. UIFont *font = MOTextLabelFont;
  728. CGFloat attachmentHeight = font.lineHeight;
  729. CGFloat imgSizeHeight = GetParamNotZero(vipImg.size.height);
  730. CGFloat attachmentWidth = attachmentHeight * vipImg.size.width/ imgSizeHeight;
  731. vipAttachment.bounds = CGRectMake(0, (font.capHeight-font.lineHeight)/2, attachmentWidth, attachmentHeight);
  732. vipSize = CGSizeMake(attachmentWidth, attachmentHeight);
  733. NSMutableAttributedString *vipImgAttri = [NSMutableAttributedString attachmentStringWithContent:vipImg contentMode:UIViewContentModeScaleAspectFit attachmentSize:vipSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  734. //生成NSAttributedString
  735. // NSMutableAttributedString *vipImgAttri = [[NSAttributedString attributedStringWithAttachment:vipAttachment] mutableCopy];
  736. [vipImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  737. [contentStr insertAttributedString:[vipImgAttri copy] atIndex:0];
  738. }
  739. }
  740. }
  741. return contentStr;
  742. }
  743. + (NSMutableAttributedString *)toAddLevelInfoWith:(NSMutableAttributedString *)contentStr And:(NSInteger)levelNum{
  744. NSArray *levelInfoArr = [MOSvgaSourceManage shareManager].allLevelSoureArr;
  745. MOStoreInfo *medalInfo;
  746. for (MOStoreInfo *object in levelInfoArr) {
  747. if(levelNum >= object.limit){
  748. medalInfo = object;
  749. break;
  750. }
  751. }
  752. if(medalInfo.res.length > 0){
  753. //这里调整的话, 请同时调整 MOBaseLevelView
  754. CGFloat levelWidth = MOLevelNormalWidth;
  755. CGFloat levelLabelX = 14.0;
  756. CGFloat levelLabexWidth = levelWidth - levelLabelX;
  757. if(levelNum >= 100){
  758. levelWidth = MOLevelMaxWidth;
  759. levelLabexWidth = levelWidth - levelLabelX - 5.0;
  760. }
  761. else if (levelNum < 10){
  762. levelWidth = MOLevelMixWidth;
  763. levelLabelX = 13.0;
  764. levelLabexWidth = levelWidth - levelLabelX;
  765. }
  766. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, levelWidth, kRtmBaseViewHeight)];
  767. bgView.backgroundColor = [UIColor clearColor];
  768. YYAnimatedImageView *levelImg = [[YYAnimatedImageView alloc] init];
  769. levelImg.frame = CGRectMake(0.0, 0.0, levelWidth, 16.0);
  770. levelImg.contentMode = UIViewContentModeScaleAspectFit;
  771. NSURL *url = [NSURL URLWithString:medalInfo.res];
  772. [levelImg setImageWithURL:url placeholder:nil];
  773. [bgView addSubview:levelImg];
  774. UILabel *levelLabel = [[UILabel alloc] initWithFrame:CGRectMake(levelLabelX, 0, levelLabexWidth, kRtmBaseViewHeight)];
  775. levelLabel.font = [MOTextTools poppinsExtraBoldFont:10.0];
  776. levelLabel.textColor = [UIColor whiteColor];
  777. levelLabel.textAlignment = NSTextAlignmentCenter;
  778. levelLabel.text = [NSString stringWithFormat:@"%zd",levelNum];
  779. [bgView addSubview:levelLabel];
  780. CGSize viewSize = bgView.size;
  781. NSMutableAttributedString *levelImgAttri = [NSMutableAttributedString attachmentStringWithContent:bgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:viewSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  782. [levelImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  783. [contentStr insertAttributedString:[levelImgAttri copy] atIndex:0];
  784. return contentStr;
  785. }
  786. else{
  787. UIImage *levelImg = [MOTextTools createLevelImageViewWith:levelNum];
  788. CGSize levelSize;
  789. //创建一个NSTextAttachment
  790. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  791. attachment.image = levelImg;
  792. //字体行高
  793. UIFont *font = MOTextLabelFont;
  794. CGFloat attachmentHeight = font.lineHeight;
  795. CGFloat imgSizeHeight = GetParamNotZero(levelImg.size.height);
  796. CGFloat attachmentWidth = attachmentHeight * levelImg.size.width/ imgSizeHeight;
  797. attachment.bounds = CGRectMake(0, (font.capHeight-font.lineHeight)/2, attachmentWidth, attachmentHeight);
  798. levelSize = CGSizeMake(attachmentWidth, attachmentHeight);
  799. //生成NSAttributedString
  800. NSMutableAttributedString *levelImgAttri = [NSMutableAttributedString attachmentStringWithContent:levelImg contentMode:UIViewContentModeScaleAspectFit attachmentSize:levelSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  801. // NSMutableAttributedString *levelImgAttri = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
  802. [levelImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  803. [contentStr insertAttributedString:[levelImgAttri copy] atIndex:0];
  804. return contentStr;
  805. }
  806. }
  807. + (NSMutableAttributedString *)toAddSmallLevelInfoWith:(NSMutableAttributedString *)contentStr And:(NSInteger)levelNum AndHeight:(CGFloat)viewHeight AndAlignToFont:(UIFont *)theFont{
  808. NSArray *levelInfoArr = [MOSvgaSourceManage shareManager].allLevelSoureArr;
  809. MOStoreInfo *medalInfo;
  810. for (MOStoreInfo *object in levelInfoArr) {
  811. if(levelNum >= object.limit){
  812. medalInfo = object;
  813. break;
  814. }
  815. }
  816. CGFloat levelHeight = 14.0;
  817. if(viewHeight > 0){
  818. levelHeight = viewHeight;
  819. }
  820. if(medalInfo.res.length > 0){
  821. //这里调整的话, 请同时调整 MOBaseLevelView
  822. CGFloat levelWidth = MOLevelNormalWidth;
  823. CGFloat levelLabelX = 13.0;
  824. CGFloat levelLabexWidth = levelWidth - levelLabelX;
  825. if(levelNum >= 100){
  826. levelWidth = MOLevelMaxWidth;
  827. levelLabexWidth = levelWidth - levelLabelX - 5.0;
  828. }
  829. else if (levelNum < 10){
  830. levelWidth = MOLevelMixWidth;
  831. levelLabelX = 12.0;
  832. levelLabexWidth = levelWidth - levelLabelX;
  833. }
  834. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, levelWidth, levelHeight)];
  835. bgView.backgroundColor = [UIColor clearColor];
  836. YYAnimatedImageView *levelImg = [[YYAnimatedImageView alloc] init];
  837. levelImg.frame = CGRectMake(0.0, 0.0, levelWidth, levelHeight);
  838. levelImg.contentMode = UIViewContentModeScaleAspectFit;
  839. NSURL *url = [NSURL URLWithString:medalInfo.res];
  840. [levelImg setImageWithURL:url placeholder:nil];
  841. [bgView addSubview:levelImg];
  842. UILabel *levelLabel = [[UILabel alloc] initWithFrame:CGRectMake(levelLabelX, 0, levelLabexWidth, levelHeight)];
  843. levelLabel.font = [MOTextTools poppinsExtraBoldFont:8.0];
  844. levelLabel.textColor = [UIColor whiteColor];
  845. levelLabel.textAlignment = NSTextAlignmentCenter;
  846. levelLabel.text = [NSString stringWithFormat:@"%zd",levelNum];
  847. [bgView addSubview:levelLabel];
  848. CGSize viewSize = bgView.size;
  849. NSMutableAttributedString *levelImgAttri = [NSMutableAttributedString attachmentStringWithContent:bgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:viewSize alignToFont:theFont alignment:YYTextVerticalAlignmentCenter];
  850. [contentStr insertAttributedString:[levelImgAttri copy] atIndex:0];
  851. return contentStr;
  852. }
  853. else{
  854. UIImage *levelImg = [MOTextTools createLevelImageViewWith:levelNum];
  855. CGSize levelSize;
  856. //创建一个NSTextAttachment
  857. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  858. attachment.image = levelImg;
  859. //字体行高
  860. UIFont *font = theFont;
  861. CGFloat attachmentHeight = font.lineHeight;
  862. CGFloat imgSizeHeight = GetParamNotZero(levelImg.size.height);
  863. CGFloat attachmentWidth = attachmentHeight * levelImg.size.width/ imgSizeHeight;
  864. attachment.bounds = CGRectMake(0, (font.capHeight-font.lineHeight)/2, attachmentWidth, attachmentHeight);
  865. levelSize = CGSizeMake(attachmentWidth, attachmentHeight);
  866. //生成NSAttributedString
  867. NSMutableAttributedString *levelImgAttri = [NSMutableAttributedString attachmentStringWithContent:levelImg contentMode:UIViewContentModeScaleAspectFit attachmentSize:levelSize alignToFont:theFont alignment:YYTextVerticalAlignmentCenter];
  868. // NSMutableAttributedString *levelImgAttri = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
  869. [contentStr insertAttributedString:[levelImgAttri copy] atIndex:0];
  870. return contentStr;
  871. }
  872. }
  873. //增加勋章
  874. + (NSMutableAttributedString *)toAddMedalInfoWith:(NSMutableAttributedString *)contentStr And:(NSArray *)medalList{
  875. if(medalList.count > 0){
  876. NSMutableAttributedString *medalBaseStr = [[NSMutableAttributedString alloc] init];
  877. BOOL isHave = NO;
  878. for (NSNumber *number in medalList) {
  879. NSInteger value = [number integerValue];
  880. MOStoreInfo *medalInfo = [[MOSvgaSourceManage shareManager] getMedalInfoWithCode:value];
  881. if(medalInfo){
  882. isHave = YES;
  883. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 17.0, 17.0)];
  884. bgView.backgroundColor = [UIColor clearColor];
  885. YYAnimatedImageView *medalImg = [[YYAnimatedImageView alloc] init];
  886. medalImg.frame = CGRectMake(0.0, 0.0, 17.0, 17.0);
  887. medalImg.contentMode = UIViewContentModeScaleAspectFit;
  888. NSString *urlStr = medalInfo.thumbnail;
  889. if(urlStr.length == 0){
  890. urlStr = medalInfo.icon;
  891. }
  892. NSURL *url = [NSURL URLWithString:urlStr];
  893. [medalImg setImageWithURL:url placeholder:nil];
  894. [bgView addSubview:medalImg];
  895. NSMutableAttributedString *medalImgAttri = [NSMutableAttributedString attachmentStringWithContent:bgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(17.0, 17.0) alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  896. [medalImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  897. [medalBaseStr appendAttributedString:[medalImgAttri copy]];
  898. }
  899. }
  900. if(isHave){
  901. [contentStr insertAttributedString:[medalBaseStr copy] atIndex:0];
  902. }
  903. return contentStr;
  904. }
  905. else{
  906. return contentStr;
  907. }
  908. }
  909. //增加勋章
  910. + (NSMutableAttributedString *)toCreateMedalInfoWithMedalListAnd:(NSArray *)medalList AndMedalWidth:(CGFloat)medalWidth AndUserBigImage:(BOOL)userBig AndSpaingStr:(NSString *)spacingStr{
  911. NSMutableAttributedString *medalBaseStr = [[NSMutableAttributedString alloc] initWithString:@""];
  912. if(medalList.count > 0){
  913. BOOL isHave = NO;
  914. for (NSNumber *number in medalList) {
  915. NSInteger value = [number integerValue];
  916. MOStoreInfo *medalInfo = [[MOSvgaSourceManage shareManager] getMedalInfoWithCode:value];
  917. if(medalInfo){
  918. isHave = YES;
  919. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, medalWidth, medalWidth)];
  920. bgView.backgroundColor = [UIColor clearColor];
  921. YYAnimatedImageView *medalImg = [[YYAnimatedImageView alloc] init];
  922. medalImg.frame = CGRectMake(0.0, 0.0, medalWidth, medalWidth);
  923. medalImg.contentMode = UIViewContentModeScaleAspectFit;
  924. NSString *urlStr;
  925. if(userBig){
  926. urlStr = medalInfo.icon;
  927. if(urlStr.length == 0){
  928. urlStr = medalInfo.image;
  929. }
  930. }
  931. else{
  932. urlStr = medalInfo.thumbnail;
  933. if(urlStr.length == 0){
  934. urlStr = medalInfo.icon;
  935. }
  936. }
  937. NSURL *url = [NSURL URLWithString:urlStr];
  938. [medalImg setImageWithURL:url placeholder:nil];
  939. [bgView addSubview:medalImg];
  940. NSMutableAttributedString *medalImgAttri = [NSMutableAttributedString attachmentStringWithContent:bgView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(medalWidth, medalWidth) alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  941. [medalImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:spacingStr]];
  942. [medalBaseStr appendAttributedString:[medalImgAttri copy]];
  943. }
  944. }
  945. }
  946. return medalBaseStr;
  947. }
  948. + (NSMutableAttributedString *)toAddFanClubInfoWith:(NSMutableAttributedString *)contentStr And:(MORtmFanBean *)fanData{
  949. if(fanData.name.length > 0){
  950. MOFanClubNameplateView *fanView = [[MOFanClubNameplateView alloc] init];
  951. fanView.grade = fanData.level;
  952. fanView.titleLab.text = fanData.name;
  953. CGFloat gradeWidth = [fanView getNameplateWidth];
  954. fanView.frame = CGRectMake(0.0, 0.0, gradeWidth, 15.0);
  955. CGSize fanViewSize = CGSizeMake(gradeWidth, 15.0);
  956. NSMutableAttributedString *fanAttri = [NSMutableAttributedString attachmentStringWithContent:fanView contentMode:UIViewContentModeScaleAspectFit attachmentSize:fanViewSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  957. [fanAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  958. [contentStr insertAttributedString:[fanAttri copy] atIndex:0];
  959. return contentStr;
  960. }
  961. else{
  962. return contentStr;
  963. }
  964. }
  965. + (NSMutableAttributedString *)base_colorfulStringWith:(NSString *)contentStr AndNameStr:(NSString *)nameStr {
  966. // 创建属性字符串,整体颜色为白色
  967. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#FFFFFF" alpha:1.0]}];
  968. if(nameStr.length != 0){
  969. NSRange redRange = [contentStr rangeOfString:nameStr];
  970. if(redRange.location != NSNotFound){
  971. // 将最后三个字符的颜色设置为红色
  972. [attributedString addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:redRange];
  973. }
  974. }
  975. return attributedString;
  976. }
  977. + (NSMutableAttributedString *)base_colorfulStringWith:(NSString *)contentStr AndNameStr:(NSString *)nameStr AndBaseColor:(NSString *)baseColorStr AndNameColor:(NSString *)nameColorStr {
  978. // 创建属性字符串,整体颜色为白色
  979. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:baseColorStr alpha:1.0]}];
  980. if(nameStr.length != 0){
  981. NSRange redRange = [contentStr rangeOfString:nameStr];
  982. if(redRange.location != NSNotFound){
  983. // 将最后三个字符的颜色设置为红色
  984. [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:nameColorStr alpha:1.0] range:redRange];
  985. }
  986. }
  987. return attributedString;
  988. }
  989. + (NSMutableAttributedString *)base_colorObjectStringWith:(NSString *)contentStr AndNameStr:(NSString *)nameStr AndBaseColor:(UIColor *)baseColor AndNameColor:(UIColor *)nameColor{
  990. // 创建属性字符串,整体颜色为白色
  991. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSForegroundColorAttributeName:baseColor}];
  992. if(nameStr.length != 0){
  993. NSRange redRange = [contentStr rangeOfString:nameStr];
  994. if(redRange.location != NSNotFound){
  995. // 将最后三个字符的颜色设置为红色
  996. [attributedString addAttribute:NSForegroundColorAttributeName value:nameColor range:redRange];
  997. }
  998. }
  999. return attributedString;
  1000. }
  1001. + (NSMutableAttributedString *)base_colorfulStringWith:(NSString *)contentStr AndNameStr:(NSString *)nameStr AndBaseColor:(NSString *)baseColorStr AndNameColor:(NSString *)nameColorStr AndStrokeColor:(NSString *)strokeColorStr AndStrokeWidth:(NSInteger)strokeWidth AndShadowColor:(NSString *)shadowColorStr{
  1002. NSDictionary *attributes = @{
  1003. NSForegroundColorAttributeName: [MOTools colorWithHexString:baseColorStr alpha:1.0],
  1004. NSStrokeWidthAttributeName: @(strokeWidth), // 描边宽度,正数表示描边宽度,负数表示填充宽度
  1005. NSStrokeColorAttributeName: [MOTools colorWithHexString:strokeColorStr alpha:1.0], // 描边颜色
  1006. NSShadowAttributeName: ({
  1007. NSShadow *shadow = [NSShadow new];
  1008. shadow.shadowColor = [MOTools colorWithHexString:shadowColorStr alpha:1.0]; // 阴影颜色
  1009. shadow.shadowOffset = CGSizeMake(1.0, 1.0); // 阴影偏移量
  1010. shadow.shadowBlurRadius = 2.0; // 阴影模糊半径
  1011. shadow;
  1012. })
  1013. };
  1014. // 创建属性字符串,整体颜色为白色
  1015. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:attributes];
  1016. if(nameStr.length != 0){
  1017. NSRange redRange = [contentStr rangeOfString:nameStr];
  1018. if(redRange.location != NSNotFound){
  1019. // 将最后三个字符的颜色设置为红色
  1020. [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:nameColorStr alpha:1.0] range:redRange];
  1021. }
  1022. }
  1023. return attributedString;
  1024. }
  1025. ///变更字体中的颜色 - 按照最后查找原则
  1026. + (NSMutableAttributedString *)base_backwardsSearchColorfulStringWith:(NSString *)contentStr AndNameStr:(NSString *)nameStr AndBaseColor:(NSString *)baseColorStr AndNameColor:(NSString *)nameColorStr{
  1027. // 创建属性字符串,整体颜色为白色
  1028. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:baseColorStr alpha:1.0]}];
  1029. if(nameStr.length != 0){
  1030. NSRange redRange = [contentStr rangeOfString:nameStr options:NSBackwardsSearch];
  1031. if(redRange.location != NSNotFound){
  1032. // 将最后三个字符的颜色设置为红色
  1033. [attributedString addAttribute:NSForegroundColorAttributeName value:[MOTools colorWithHexString:nameColorStr alpha:1.0] range:redRange];
  1034. }
  1035. }
  1036. return attributedString;
  1037. }
  1038. + (NSMutableAttributedString *)add_atColorfulStringWith:(NSMutableAttributedString *)contentStr AndAtString:(NSString *)atNickname{
  1039. NSString *baseStr = [contentStr string];
  1040. NSString *theAtName = atNickname;
  1041. if(atNickname.length != 0){
  1042. if([atNickname containsString:@","]){
  1043. theAtName = [atNickname componentsSeparatedByString:@","].firstObject;
  1044. }
  1045. }
  1046. NSRange redRange = [baseStr rangeOfString:[NSString stringWithFormat:@"@%@",theAtName]];
  1047. if(redRange.location != NSNotFound){
  1048. // 将最后三个字符的颜色设置为红色
  1049. [contentStr addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:redRange];
  1050. }
  1051. return contentStr;
  1052. }
  1053. + (NSMutableAttributedString *)tipAllBlueStringWith:(NSString *)contentStr{
  1054. // 创建属性字符串,整体颜色为白色
  1055. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSForegroundColorAttributeName:kBaseTextHightlightColor_1,NSFontAttributeName:MOTextLabelFont}];
  1056. return attributedString;
  1057. }
  1058. + (NSMutableAttributedString *)tipAllWhiteStringWith:(NSString *)contentStr{
  1059. // 创建属性字符串,整体颜色为白色
  1060. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:MOTextLabelFont}];
  1061. return attributedString;
  1062. }
  1063. + (NSMutableAttributedString *)colorfulStringWith:(NSString *)contentStr AndNameStr:(NSString *)nameStr {
  1064. // 创建属性字符串,整体颜色为白色
  1065. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:@{NSForegroundColorAttributeName:[MOTools colorWithHexString:@"#EFE85C" alpha:1.0]}];
  1066. NSRange redRange = [contentStr rangeOfString:nameStr];
  1067. if(redRange.location != NSNotFound){
  1068. // 将最后三个字符的颜色设置为红色
  1069. [attributedString addAttribute:NSForegroundColorAttributeName value:kBaseTextHightlightColor_1 range:redRange];
  1070. }
  1071. return attributedString;
  1072. }
  1073. + (UIImage *)createLevelImageViewWith:(NSInteger)gradeNum {
  1074. CGFloat iconWidth = 0.0;
  1075. CGFloat labLeft = 12.0;
  1076. UIImage *iconImg = [MOTextTools getLevelIconImgWith:gradeNum];
  1077. if(iconImg){
  1078. iconWidth = 13.0;
  1079. labLeft = 15.0 + 5.0;
  1080. }
  1081. NSString *textStr = [NSString stringWithFormat:@"%zd",gradeNum];
  1082. UIFont *textFont = MOLevelLabelFont;
  1083. CGFloat textWidth = [MOTools getWidthWithString:textStr font:textFont];
  1084. CGFloat viewWidth = textWidth + 10.0 + iconWidth + 5.0;
  1085. UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, kRtmBaseViewHeight)];
  1086. bgView.backgroundColor = [UIColor clearColor];
  1087. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, kRtmBaseViewHeight)];
  1088. view.backgroundColor = [MOTextTools getLevelColorWith:gradeNum];
  1089. if(iconImg){
  1090. UIImageView *iconImgView = [[UIImageView alloc] initWithImage:iconImg];
  1091. iconImgView.frame = CGRectMake(5.0, 2.0, iconWidth, iconWidth);
  1092. iconImgView.contentMode = UIViewContentModeScaleAspectFit;
  1093. [view addSubview:iconImgView];
  1094. }
  1095. UILabel *numLab = [[UILabel alloc] initWithFrame:CGRectMake(labLeft, 1.5, textWidth, 14.0)];
  1096. numLab.text = textStr;
  1097. numLab.textColor = [UIColor whiteColor];
  1098. numLab.font = textFont;
  1099. [view addSubview:numLab];
  1100. view.layer.cornerRadius = 17.0 / 2.0;
  1101. view.layer.masksToBounds = YES;
  1102. [bgView addSubview:view];
  1103. UIImage *resultImg = [MOTextTools snapshotWithView:bgView];
  1104. return resultImg;
  1105. }
  1106. + (UIColor *)getLevelColorWith:(NSInteger)gradeNum {
  1107. UIColor *needColor;
  1108. if(gradeNum >= 135){
  1109. needColor = [MOTools colorWithHexString:@"#FF7B1B" alpha:1.0];
  1110. return needColor;
  1111. }
  1112. if(gradeNum >= 120){
  1113. needColor = [MOTools colorWithHexString:@"#F16582" alpha:1.0];
  1114. return needColor;
  1115. }
  1116. if(gradeNum >= 100){
  1117. needColor = [MOTools colorWithHexString:@"#D58AEF" alpha:1.0];
  1118. return needColor;
  1119. }
  1120. if(gradeNum >= 80){
  1121. needColor = [MOTools colorWithHexString:@"#FB9DC1" alpha:1.0];
  1122. return needColor;
  1123. }
  1124. if(gradeNum >= 65){
  1125. needColor = [MOTools colorWithHexString:@"#F051E0" alpha:1.0];
  1126. return needColor;
  1127. }
  1128. if(gradeNum >= 50){
  1129. needColor = [MOTools colorWithHexString:@"#B165FD" alpha:1.0];
  1130. return needColor;
  1131. }
  1132. if(gradeNum >= 30){
  1133. needColor = [MOTools colorWithHexString:@"#728EF2" alpha:1.0];
  1134. return needColor;
  1135. }
  1136. if(gradeNum >= 15){
  1137. needColor = [MOTools colorWithHexString:@"#55BBDE" alpha:1.0];
  1138. return needColor;
  1139. }
  1140. else{
  1141. needColor = [MOTools colorWithHexString:@"#44CCA7" alpha:1.0];
  1142. return needColor;
  1143. }
  1144. }
  1145. + (nullable UIImage *)getVipIconWith:(NSInteger)vipType{
  1146. if(vipType == 0){
  1147. return nil;
  1148. }
  1149. UIImage *vipImg;
  1150. if(vipType == 1){
  1151. vipImg = [UIImage imageNamed:@"v_2_vip_4_282"];
  1152. vipImg = [vipImg imageByResizeToSize:CGSizeMake(vipImg.size.width / 2.0, vipImg.size.height / 2.0) contentMode:UIViewContentModeScaleAspectFit];
  1153. return vipImg;
  1154. }
  1155. if(vipType == 2){
  1156. vipImg = [UIImage imageNamed:@"v_2_vip_3_282"];
  1157. vipImg = [vipImg imageByResizeToSize:CGSizeMake(vipImg.size.width / 2.0, vipImg.size.height / 2.0) contentMode:UIViewContentModeScaleAspectFit];
  1158. return vipImg;
  1159. }
  1160. if(vipType == 5){
  1161. vipImg = [UIImage imageNamed:@"v_2_vip_5_282"];
  1162. vipImg = [vipImg imageByResizeToSize:CGSizeMake(vipImg.size.width / 2.0, vipImg.size.height / 2.0) contentMode:UIViewContentModeScaleAspectFit];
  1163. return vipImg;
  1164. }
  1165. if(vipType == 6){
  1166. vipImg = [UIImage imageNamed:@"v_2_vip_6_282"];
  1167. vipImg = [vipImg imageByResizeToSize:CGSizeMake(vipImg.size.width / 2.0, vipImg.size.height / 2.0) contentMode:UIViewContentModeScaleAspectFit];
  1168. return vipImg;
  1169. }
  1170. if(vipType == 3){
  1171. vipImg = [UIImage imageNamed:@"v_2_vip_2_282"];
  1172. vipImg = [vipImg imageByResizeToSize:CGSizeMake(vipImg.size.width / 2.0, vipImg.size.height / 2.0) contentMode:UIViewContentModeScaleAspectFit];
  1173. return vipImg;
  1174. }
  1175. else{
  1176. vipImg = [UIImage imageNamed:@"v_2_vip_1_282"];
  1177. vipImg = [vipImg imageByResizeToSize:CGSizeMake(vipImg.size.width / 2.0, vipImg.size.height / 2.0) contentMode:UIViewContentModeScaleAspectFit];
  1178. return vipImg;
  1179. }
  1180. }
  1181. + (UIImage *)getLevelIconImgWith:(NSInteger)gradeNum{
  1182. UIImage *needImg;
  1183. if(gradeNum >= 135){
  1184. needImg = [UIImage imageNamed:@"icon_level_135"];
  1185. return needImg;
  1186. }
  1187. if(gradeNum >= 120){
  1188. needImg = [UIImage imageNamed:@"icon_level_120"];
  1189. return needImg;
  1190. }
  1191. if(gradeNum >= 100){
  1192. needImg = [UIImage imageNamed:@"icon_level_100"];
  1193. return needImg;
  1194. }
  1195. if(gradeNum >= 80){
  1196. needImg = [UIImage imageNamed:@"icon_level_80"];
  1197. return needImg;
  1198. }
  1199. if(gradeNum >= 65){
  1200. needImg = [UIImage imageNamed:@"icon_level_65"];
  1201. return needImg;
  1202. }
  1203. if(gradeNum >= 50){
  1204. needImg = [UIImage imageNamed:@"icon_level_50"];
  1205. return needImg;
  1206. }
  1207. if(gradeNum >= 30){
  1208. needImg = [UIImage imageNamed:@"icon_level_30"];
  1209. return needImg;
  1210. }
  1211. if(gradeNum >= 15){
  1212. needImg = [UIImage imageNamed:@"icon_level_15"];
  1213. return needImg;
  1214. }
  1215. else{
  1216. needImg = [UIImage imageNamed:@"icon_level_1"];
  1217. return needImg;
  1218. }
  1219. }
  1220. + (UIImage *)getBubbleImgWithCodeNum:(NSInteger)code{
  1221. UIImage *bubbleImg;
  1222. switch (code) {
  1223. case 1096:
  1224. {
  1225. bubbleImg = [UIImage imageNamed:@"icon_bubble_weekly_star"];
  1226. }
  1227. break;
  1228. case 1083:
  1229. {
  1230. bubbleImg = [UIImage imageNamed:@"icon_bubble_taohua"];
  1231. }
  1232. break;
  1233. case 1082:
  1234. {
  1235. bubbleImg = [UIImage imageNamed:@"icon_bubble_dasao"];
  1236. }
  1237. break;
  1238. case 1081:
  1239. {
  1240. bubbleImg = [UIImage imageNamed:@"icon_bubble_aixin"];
  1241. }
  1242. break;
  1243. case 1080:
  1244. {
  1245. bubbleImg = [UIImage imageNamed:@"icon_bubble_hai"];
  1246. }
  1247. break;
  1248. case 1079:
  1249. {
  1250. bubbleImg = [UIImage imageNamed:@"icon_bubble_zuqiu"];
  1251. }
  1252. break;
  1253. case 1078:
  1254. {
  1255. bubbleImg = [UIImage imageNamed:@"icon_bubble_jingyu"];
  1256. }
  1257. break;
  1258. case 1077:
  1259. {
  1260. bubbleImg = [UIImage imageNamed:@"icon_bubble_youle"];
  1261. }
  1262. break;
  1263. case 1071:
  1264. {
  1265. //家族3
  1266. bubbleImg = [UIImage imageNamed:@"icon_family_no_3"];
  1267. }
  1268. break;
  1269. case 1070:
  1270. {
  1271. //家族2
  1272. bubbleImg = [UIImage imageNamed:@"icon_family_no_2"];
  1273. }
  1274. break;
  1275. case 1069:
  1276. {
  1277. //家族1
  1278. bubbleImg = [UIImage imageNamed:@"icon_family_no_1"];
  1279. }
  1280. break;
  1281. case 1055:
  1282. case 1244:
  1283. {
  1284. //VIP - k
  1285. bubbleImg = [UIImage imageNamed:@"icon_bubble_vip_k"];
  1286. }
  1287. break;
  1288. case 1054:
  1289. case 1245:
  1290. {
  1291. //VIP - b
  1292. bubbleImg = [UIImage imageNamed:@"icon_bubble_vip_b"];
  1293. }
  1294. break;
  1295. case 1053:
  1296. case 1246:
  1297. {
  1298. //VIP - v
  1299. bubbleImg = [UIImage imageNamed:@"icon_bubble_vip_v"];
  1300. }
  1301. break;
  1302. case 1052:
  1303. case 1247:
  1304. {
  1305. //VIP - m
  1306. bubbleImg = [UIImage imageNamed:@"icon_bubble_vip_m"];
  1307. }
  1308. break;
  1309. case 1248:
  1310. {
  1311. //VIP - m
  1312. bubbleImg = [UIImage imageNamed:@"icon_bubble_vip_5"];
  1313. }
  1314. break;
  1315. case 1249:
  1316. {
  1317. //VIP - m
  1318. bubbleImg = [UIImage imageNamed:@"icon_bubble_vip_6"];
  1319. }
  1320. break;
  1321. case 1202:
  1322. {
  1323. //VIP - m
  1324. bubbleImg = [UIImage imageNamed:@"v_2_new_b_4"];
  1325. }
  1326. break;
  1327. case 1241:
  1328. {
  1329. //VIP - m
  1330. bubbleImg = [UIImage imageNamed:@"v_2_new_b_2"];
  1331. }
  1332. break;
  1333. case 1242:
  1334. {
  1335. //VIP - m
  1336. bubbleImg = [UIImage imageNamed:@"v_2_new_b_3"];
  1337. }
  1338. break;
  1339. case 1243:
  1340. {
  1341. //VIP - m
  1342. bubbleImg = [UIImage imageNamed:@"v_2_new_b_5"];
  1343. }
  1344. break;
  1345. case 1183:
  1346. {
  1347. bubbleImg = [UIImage imageNamed:@"icon_bubble_t_1"];
  1348. }
  1349. break;
  1350. case 1182:
  1351. {
  1352. bubbleImg = [UIImage imageNamed:@"icon_bubble_t_2"];
  1353. }
  1354. break;
  1355. case 1181:
  1356. {
  1357. bubbleImg = [UIImage imageNamed:@"icon_bubble_t_3"];
  1358. }
  1359. break;
  1360. case 1180:
  1361. {
  1362. bubbleImg = [UIImage imageNamed:@"icon_bubble_t_4"];
  1363. }
  1364. break;
  1365. case 1178:
  1366. {
  1367. bubbleImg = [UIImage imageNamed:@"icon_bubble_t_5"];
  1368. }
  1369. break;
  1370. case 1176:
  1371. {
  1372. bubbleImg = [UIImage imageNamed:@"icon_bubble_t_6"];
  1373. }
  1374. break;
  1375. case 1170:
  1376. {
  1377. bubbleImg = [UIImage imageNamed:@"icon_bubble_t_7"];
  1378. }
  1379. break;
  1380. case 1169:
  1381. {
  1382. bubbleImg = [UIImage imageNamed:@"icon_bubble_t_8"];
  1383. }
  1384. break;
  1385. default:
  1386. bubbleImg = nil;
  1387. break;
  1388. }
  1389. return bubbleImg;
  1390. }
  1391. + (CGSize)textMessageCellSizeWith:(MORtmEntity *)rtmEntity
  1392. {
  1393. NSMutableAttributedString *attributedString = [MOTextTools creteTextWith:rtmEntity];
  1394. [attributedString addAttributes:MOTextLabelAttributesDict range:NSMakeRange(0, attributedString.string.length)];
  1395. YYTextContainer *container = [YYTextContainer new];
  1396. container.size = CGSizeMake(MOTextLabelMaxWidth, CGFLOAT_MAX); // 宽度固定,高度无限制
  1397. container.linePositionModifier = nil; // 可自定义行样式
  1398. container.insets = UIEdgeInsetsMake(MOContentBaseTopSpcing, MOContentBaseLeftSpacing, MOContentBaseBottomSpcing, MOContentBaseRightSpacing);
  1399. // 创建文本布局
  1400. YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:attributedString];
  1401. // 获取文本高度
  1402. CGSize textSize = layout.textBoundingSize;
  1403. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmEntity.data;
  1404. if(rtmEntity.type == MORtmChat){
  1405. // 如果是聊天消息,增加顶部间距
  1406. textSize.height += MOTextTitleLabeHeight;
  1407. }
  1408. if(rtmEntity.type == SystemOrder && (jsonEntity.type == 9 || jsonEntity.type == 10)){
  1409. //红包
  1410. textSize.height += MOTextRedEnvelopeViewHeight;
  1411. }
  1412. return textSize;
  1413. }
  1414. + (CGSize)textMessageCellSizeWithString:(NSString *)contentStr
  1415. {
  1416. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr];
  1417. [attributedString addAttributes:MOTextLabelAttributesDict range:NSMakeRange(0, attributedString.string.length)];
  1418. CGSize size = [attributedString boundingRectWithSize:CGSizeMake(MOTextLabelMaxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
  1419. return size;
  1420. }
  1421. + (CGFloat)getWidthWith:(MORtmEntity *)rtmEntity
  1422. {
  1423. NSMutableAttributedString *attributedString = [MOTextTools creteTextWith:rtmEntity];
  1424. [attributedString addAttributes:MOTextLabelAttributesDict range:NSMakeRange(0, attributedString.string.length)];
  1425. YYTextContainer *container = [YYTextContainer new];
  1426. container.size = CGSizeMake(MAXFLOAT, MAXFLOAT); // 宽度无限制,高度无限制
  1427. container.linePositionModifier = nil; // 可自定义行样式
  1428. container.insets = UIEdgeInsetsMake(MOContentBaseTopSpcing, MOContentBaseLeftSpacing, MOContentBaseBottomSpcing, MOContentBaseRightSpacing);
  1429. // 创建文本布局
  1430. YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:attributedString];
  1431. // 获取文本高度
  1432. CGSize textSize = layout.textBoundingSize;
  1433. CGFloat textWidth = textSize.width + 1.0;
  1434. return textWidth;
  1435. }
  1436. + (CGFloat)getTheAttributedTextWidthWith:(NSAttributedString *)contentStr{
  1437. CGFloat width = [contentStr boundingRectWithSize:CGSizeZero options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.width + 1.0;
  1438. return width;
  1439. }
  1440. + (UIImage *)snapshotWithView:(UIView *)view {
  1441. UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
  1442. [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  1443. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  1444. UIGraphicsEndImageContext();
  1445. return image;
  1446. }
  1447. + (NSMutableParagraphStyle *)textLabelParagraphStyle
  1448. {
  1449. NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
  1450. paragraphStyle.lineSpacing = MOTextLabelLineSpacing;
  1451. return paragraphStyle;
  1452. }
  1453. + (UIImage *)getFanLevelImgWith:(NSInteger)gradeNum{
  1454. UIImage *needImg;
  1455. if(gradeNum >= 60){
  1456. needImg = [UIImage imageNamed:@"icon_fan_club_lv_60"];
  1457. return needImg;
  1458. }
  1459. if(gradeNum >= 50){
  1460. needImg = [UIImage imageNamed:@"icon_fan_club_lv_50"];
  1461. return needImg;
  1462. }
  1463. if(gradeNum >= 40){
  1464. needImg = [UIImage imageNamed:@"icon_fan_club_lv_40"];
  1465. return needImg;
  1466. }
  1467. if(gradeNum >= 30){
  1468. needImg = [UIImage imageNamed:@"icon_fan_club_lv_30"];
  1469. return needImg;
  1470. }
  1471. if(gradeNum >= 20){
  1472. needImg = [UIImage imageNamed:@"icon_fan_club_lv_20"];
  1473. return needImg;
  1474. }
  1475. if(gradeNum >= 11){
  1476. needImg = [UIImage imageNamed:@"icon_fan_club_lv_11"];
  1477. return needImg;
  1478. }
  1479. if(gradeNum >= 6){
  1480. needImg = [UIImage imageNamed:@"icon_fan_club_lv_6"];
  1481. return needImg;
  1482. }
  1483. needImg = [UIImage imageNamed:@"icon_fan_club_lv_1"];
  1484. return needImg;
  1485. }
  1486. + (UIImage *)getFanLiveImgWith:(NSInteger)gradeNum{
  1487. UIImage *needImg;
  1488. if(gradeNum >= 60){
  1489. needImg = [UIImage imageNamed:@"icon_fan_club_lv_s_60"];
  1490. return needImg;
  1491. }
  1492. if(gradeNum >= 50){
  1493. needImg = [UIImage imageNamed:@"icon_fan_club_lv_s_50"];
  1494. return needImg;
  1495. }
  1496. if(gradeNum >= 40){
  1497. needImg = [UIImage imageNamed:@"icon_fan_club_lv_s_40"];
  1498. return needImg;
  1499. }
  1500. if(gradeNum >= 30){
  1501. needImg = [UIImage imageNamed:@"icon_fan_club_lv_s_30"];
  1502. return needImg;
  1503. }
  1504. if(gradeNum >= 20){
  1505. needImg = [UIImage imageNamed:@"icon_fan_club_lv_s_20"];
  1506. return needImg;
  1507. }
  1508. if(gradeNum >= 11){
  1509. needImg = [UIImage imageNamed:@"icon_fan_club_lv_s_11"];
  1510. return needImg;
  1511. }
  1512. if(gradeNum >= 6){
  1513. needImg = [UIImage imageNamed:@"icon_fan_club_lv_s_6"];
  1514. return needImg;
  1515. }
  1516. needImg = [UIImage imageNamed:@"icon_fan_club_lv_s_1"];
  1517. return needImg;
  1518. }
  1519. + (UIImage *)getTheFanClubMedalWith:(NSInteger)level{
  1520. UIImage *needImg;
  1521. if(level >= 60){
  1522. needImg = [UIImage imageNamed:@"icon_uesr_fan_60"];
  1523. return needImg;
  1524. }
  1525. if(level >= 50){
  1526. needImg = [UIImage imageNamed:@"icon_uesr_fan_50"];
  1527. return needImg;
  1528. }
  1529. if(level >= 40){
  1530. needImg = [UIImage imageNamed:@"icon_uesr_fan_40"];
  1531. return needImg;
  1532. }
  1533. needImg = [UIImage imageNamed:@"icon_uesr_fan_30"];
  1534. return needImg;
  1535. }
  1536. + (UIColor *)getFanLevelColorWith:(NSInteger)gradeNum {
  1537. UIColor *needColor;
  1538. if(gradeNum >= 60){
  1539. needColor = [MOTools colorWithHexString:@"#F26868" alpha:1.0];
  1540. return needColor;
  1541. }
  1542. if(gradeNum >= 50){
  1543. needColor = [MOTools colorWithHexString:@"#FF7EA0" alpha:1.0];
  1544. return needColor;
  1545. }
  1546. if(gradeNum >= 40){
  1547. needColor = [MOTools colorWithHexString:@"#FC5DBB" alpha:1.0];
  1548. return needColor;
  1549. }
  1550. if(gradeNum >= 30){
  1551. needColor = [MOTools colorWithHexString:@"#C964F7" alpha:1.0];
  1552. return needColor;
  1553. }
  1554. if(gradeNum >= 20){
  1555. needColor = [MOTools colorWithHexString:@"#FC8B71" alpha:1.0];
  1556. return needColor;
  1557. }
  1558. if(gradeNum >= 11){
  1559. needColor = [MOTools colorWithHexString:@"#F5A768" alpha:1.0];
  1560. return needColor;
  1561. }
  1562. if(gradeNum >= 6){
  1563. needColor = [MOTools colorWithHexString:@"#F8C368" alpha:1.0];
  1564. return needColor;
  1565. }
  1566. needColor = [MOTools colorWithHexString:@"#FBD562" alpha:1.0];
  1567. return needColor;
  1568. }
  1569. //单位换算
  1570. + (NSString *)numberToStringWith:(double)num{
  1571. NSString *resultStr = @"";
  1572. if(num < 1000){
  1573. resultStr = [NSString stringWithFormat:@"%.f",num];
  1574. }
  1575. else if (num >= 1000 && num <= 999999){
  1576. double newNum = num / 1000.0;
  1577. resultStr = [NSString stringWithFormat:@"%.1fK",(floorf(newNum * 10) / 10)];
  1578. }
  1579. else if (num >= 1000000 && num <= 999999999){
  1580. double newNum = num / 1000000.0;
  1581. resultStr = [NSString stringWithFormat:@"%.1fM",(floorf(newNum * 10) / 10)];
  1582. }
  1583. else{
  1584. double newNum = num / 1000000000.0;
  1585. resultStr = [NSString stringWithFormat:@"%.1fB",(floorf(newNum * 10) / 10)];
  1586. }
  1587. return resultStr;
  1588. }
  1589. + (NSString *)numberToStringAboutTwoWith:(double)num{
  1590. NSString *resultStr = @"";
  1591. if (num < 10000) {
  1592. resultStr = [NSString stringWithFormat:@"%.2f", num];
  1593. }
  1594. else if (num >= 10000 && num <= 999999) {
  1595. double newNum = num / 1000.0;
  1596. resultStr = [NSString stringWithFormat:@"%.2fK", newNum];
  1597. }
  1598. else if (num >= 1000000 && num <= 999999999) {
  1599. double newNum = num / 1000000.0;
  1600. resultStr = [NSString stringWithFormat:@"%.2fM", newNum];
  1601. }
  1602. else {
  1603. double newNum = num / 1000000000.0;
  1604. resultStr = [NSString stringWithFormat:@"%.2fB", newNum];
  1605. }
  1606. return resultStr;
  1607. }
  1608. + (NSString *)numberToStringNoPointWith:(double)num{
  1609. NSString *resultStr = @"";
  1610. if(num < 1000){
  1611. resultStr = [NSString stringWithFormat:@"%.f",num];
  1612. }
  1613. else if (num >= 1000 && num <= 999999){
  1614. double newNum = num / 1000.0;
  1615. resultStr = [NSString stringWithFormat:@"%.fK",(floorf(newNum * 10) / 10)];
  1616. }
  1617. else if (num >= 1000000 && num <= 999999999){
  1618. double newNum = num / 1000000.0;
  1619. resultStr = [NSString stringWithFormat:@"%.fM",(floorf(newNum * 10) / 10)];
  1620. }
  1621. else{
  1622. double newNum = num / 1000000000.0;
  1623. resultStr = [NSString stringWithFormat:@"%.fB",(floorf(newNum * 10) / 10)];
  1624. }
  1625. return resultStr;
  1626. }
  1627. + (NSString *)minuteStrWith:(double)minute{
  1628. NSString *resultStr = @"";
  1629. if(minute < 60){
  1630. resultStr = [NSString stringWithFormat:@"%.fMin",minute];
  1631. }
  1632. else{
  1633. CGFloat hourNum = minute / 60;
  1634. resultStr = [NSString stringWithFormat:@"%.fH",hourNum];
  1635. }
  1636. return resultStr;
  1637. }
  1638. + (NSString *)formatNumberDecimalValue:(double)value {
  1639. NSString * string = [NSNumberFormatter localizedStringFromNumber:[NSNumber numberWithLongLong:value]
  1640. numberStyle:NSNumberFormatterDecimalStyle];
  1641. return string;
  1642. }
  1643. /// 小数转成百分比,去除小数部分
  1644. + (NSString *)formatPercentage:(double)value {
  1645. NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  1646. formatter.numberStyle = NSNumberFormatterPercentStyle;
  1647. formatter.maximumFractionDigits = 1; // Set to 1 decimal place
  1648. formatter.minimumFractionDigits = 0; // Avoid trailing zeros
  1649. return [formatter stringFromNumber:@(value)];
  1650. }
  1651. /// SF-Pro-Display-HeavyItalic 斜体加粗
  1652. /// @param fontSize 字号
  1653. /// @param bold 加粗
  1654. /// @param italic 斜体
  1655. /// @param weight 加粗量级
  1656. + (UIFont *)MODisplayFontWithSize:(CGFloat)fontSize
  1657. bold:(BOOL)bold itatic:(BOOL)italic weight:(UIFontWeight)weight {
  1658. UIFont *font = [UIFont systemFontOfSize:fontSize weight:weight];
  1659. UIFontDescriptorSymbolicTraits symbolicTraits = 0;
  1660. if (italic) {
  1661. symbolicTraits |= UIFontDescriptorTraitItalic;
  1662. }
  1663. if (bold) {
  1664. symbolicTraits |= UIFontDescriptorTraitBold;
  1665. }
  1666. UIFont *specialFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:symbolicTraits] size:font.pointSize];
  1667. return specialFont;
  1668. }
  1669. + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{
  1670. UIFont *customFont = [UIFont fontWithName:fontName size:fontSize];
  1671. if(!customFont){
  1672. customFont = [MOTextTools MODisplayFontWithSize:fontSize bold:YES itatic:NO weight:UIFontWeightMedium];
  1673. }
  1674. return customFont;
  1675. }
  1676. + (UIImage *)getTheDiamondImageWith:(NSInteger)diamond{
  1677. UIImage *needImg;
  1678. if(diamond >= 100000){
  1679. needImg = [UIImage imageNamed:@"icon_diamond_4"];
  1680. return needImg;
  1681. }
  1682. if(diamond >= 50000){
  1683. needImg = [UIImage imageNamed:@"icon_diamond_3"];
  1684. return needImg;
  1685. }
  1686. if(diamond >= 20000){
  1687. needImg = [UIImage imageNamed:@"icon_diamond_2"];
  1688. return needImg;
  1689. }
  1690. needImg = [UIImage imageNamed:@"icon_diamond_1"];
  1691. return needImg;
  1692. }
  1693. /// 根据选中人 以及 麦位上的数据 生成一个礼物飘屏提示字符串
  1694. + (NSString *)toGetTheGiftTipTextWith:(NSArray *)linkMics And:(MOUserProfile *)anchor And:(NSArray *)sendUserIdArr{
  1695. NSString *resultStr = @"send you a gift";
  1696. NSMutableArray *tempArr = [NSMutableArray array];
  1697. BOOL isAll = YES;
  1698. if([sendUserIdArr containsObject:anchor.id]){
  1699. [tempArr addObject:anchor.nickname];
  1700. }
  1701. else{
  1702. isAll = NO;
  1703. }
  1704. for (MOLinkMic *linkMic in linkMics) {
  1705. if(linkMic.profile.id.length > 0){
  1706. if([sendUserIdArr containsObject:linkMic.profile.id]){
  1707. if(linkMic.profile.nickname){
  1708. [tempArr addObject:linkMic.profile.nickname];
  1709. }
  1710. }
  1711. else{
  1712. isAll = NO;
  1713. }
  1714. }
  1715. else{
  1716. isAll = NO;
  1717. }
  1718. }
  1719. if(tempArr.count > 0){
  1720. if(isAll){
  1721. resultStr = @"send to all";
  1722. }
  1723. else{
  1724. resultStr = [tempArr componentsJoinedByString:@","];
  1725. resultStr = [NSString stringWithFormat:@"send to %@",resultStr];
  1726. }
  1727. }
  1728. return resultStr;
  1729. }
  1730. + (NSMutableAttributedString *)addDeleteLineWith:(NSString *)textStr andLineColor:(UIColor *)lineColor{
  1731. NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:textStr];
  1732. // 设置删除线样式
  1733. [attributedText addAttribute:NSStrikethroughStyleAttributeName
  1734. value:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid)
  1735. range:NSMakeRange(0, textStr.length)];
  1736. // 设置删除线颜色
  1737. [attributedText addAttribute:NSStrikethroughColorAttributeName
  1738. value:lineColor
  1739. range:NSMakeRange(0, textStr.length)];
  1740. return attributedText;
  1741. }
  1742. /// 根据用户信息生成一个富文本(VIP/性别/等级)
  1743. + (NSAttributedString *)createAttributedStringWithUserInfo:(MOUserBase *)userBase{
  1744. NSMutableAttributedString *plainText = [[NSMutableAttributedString alloc] initWithString:@""];
  1745. //加等级
  1746. [self toAddLevelInfoWith:plainText And:userBase.level];
  1747. //加性别
  1748. [self toAddGenderImgWith:userBase.userProfile.gender With:plainText];
  1749. //加vip
  1750. [self toAddVipInfoWith:plainText And:userBase.vipType];
  1751. return plainText;
  1752. }
  1753. + (void)toAddGenderImgWith:(NSInteger)genderNum With:(NSMutableAttributedString *)contentStr{
  1754. UIImage *vipImg;
  1755. if(genderNum == 1){
  1756. vipImg = [UIImage imageNamed:@"icon_contact_man"];
  1757. }
  1758. else if (genderNum == 2){
  1759. vipImg = [UIImage imageNamed:@"icon_contact_woman"];
  1760. }
  1761. else{
  1762. return;
  1763. }
  1764. NSTextAttachment *vipAttachment;
  1765. if(vipImg){
  1766. CGSize vipSize;
  1767. vipAttachment = [[NSTextAttachment alloc] init];
  1768. vipAttachment.image = vipImg;
  1769. //字体行高
  1770. UIFont *font = MOTextLabelFont;
  1771. CGFloat attachmentHeight = font.lineHeight;
  1772. CGFloat imgSizeHeight = GetParamNotZero(vipImg.size.height);
  1773. CGFloat attachmentWidth = attachmentHeight * vipImg.size.width/ imgSizeHeight;
  1774. vipAttachment.bounds = CGRectMake(0, (font.capHeight-font.lineHeight)/2, attachmentWidth, attachmentHeight);
  1775. vipSize = CGSizeMake(attachmentWidth, attachmentHeight);
  1776. NSMutableAttributedString *vipImgAttri = [NSMutableAttributedString attachmentStringWithContent:vipImg contentMode:UIViewContentModeScaleAspectFit attachmentSize:vipSize alignToFont:MOTextLabelFont alignment:YYTextVerticalAlignmentCenter];
  1777. [vipImgAttri appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
  1778. [contentStr insertAttributedString:[vipImgAttri copy] atIndex:0];
  1779. }
  1780. }
  1781. + (CGFloat)getWidthAboutYYWith:(NSAttributedString *)contentStr
  1782. {
  1783. YYTextContainer *container = [YYTextContainer new];
  1784. container.size = CGSizeMake(MAXFLOAT, MAXFLOAT); // 宽度无限制,高度无限制
  1785. container.linePositionModifier = nil; // 可自定义行样式
  1786. // 创建文本布局
  1787. YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:contentStr];
  1788. // 获取文本高度
  1789. CGSize textSize = layout.textBoundingSize;
  1790. CGFloat textWidth = textSize.width + 1.0;
  1791. return textWidth;
  1792. }
  1793. + (void)setStyledTextForLabel:(UILabel *)label
  1794. fullText:(NSString *)text
  1795. targetText:(NSString *)targetText
  1796. color:(UIColor *)color
  1797. hasUnderline:(BOOL)hasUnderline {
  1798. if (!label || !text || !targetText) {
  1799. return;
  1800. }
  1801. NSMutableAttributedString *attributedString;
  1802. // 检查是否已有富文本,如果有则在现有基础上修改,否则创建新的
  1803. if (label.attributedText && label.attributedText.length > 0) {
  1804. // 验证文本内容是否一致
  1805. if ([label.attributedText.string isEqualToString:text]) {
  1806. // 文本一致,在现有富文本基础上修改
  1807. attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
  1808. } else {
  1809. // 文本不一致,创建新的富文本
  1810. attributedString = [[NSMutableAttributedString alloc] initWithString:text];
  1811. // 设置整体文本样式
  1812. [attributedString addAttribute:NSFontAttributeName
  1813. value:[UIFont systemFontOfSize:14.0]
  1814. range:NSMakeRange(0, text.length)];
  1815. [attributedString addAttribute:NSForegroundColorAttributeName
  1816. value:[UIColor blackColor]
  1817. range:NSMakeRange(0, text.length)];
  1818. }
  1819. } else {
  1820. // 没有现有富文本,创建新的
  1821. attributedString = [[NSMutableAttributedString alloc] initWithString:text];
  1822. // 设置整体文本样式
  1823. [attributedString addAttribute:NSFontAttributeName
  1824. value:[UIFont systemFontOfSize:14.0]
  1825. range:NSMakeRange(0, text.length)];
  1826. [attributedString addAttribute:NSForegroundColorAttributeName
  1827. value:[UIColor blackColor]
  1828. range:NSMakeRange(0, text.length)];
  1829. }
  1830. // 查找目标文本的位置
  1831. NSRange targetRange = [attributedString.string rangeOfString:targetText];
  1832. if (targetRange.location != NSNotFound) {
  1833. // 设置目标文本颜色
  1834. [attributedString addAttribute:NSForegroundColorAttributeName
  1835. value:color
  1836. range:targetRange];
  1837. // 添加下划线(如果需要)
  1838. if (hasUnderline) {
  1839. [attributedString addAttribute:NSUnderlineStyleAttributeName
  1840. value:@(NSUnderlineStyleSingle)
  1841. range:targetRange];
  1842. [attributedString addAttribute:NSUnderlineColorAttributeName
  1843. value:color
  1844. range:targetRange];
  1845. } else {
  1846. // 如果不需要下划线,移除可能存在的下划线样式
  1847. [attributedString removeAttribute:NSUnderlineStyleAttributeName range:targetRange];
  1848. [attributedString removeAttribute:NSUnderlineColorAttributeName range:targetRange];
  1849. }
  1850. // 存储目标文本范围信息(支持多个范围)
  1851. NSMutableArray *ranges = objc_getAssociatedObject(label, @"targetRanges");
  1852. if (!ranges) {
  1853. ranges = [[NSMutableArray alloc] init];
  1854. }
  1855. [ranges addObject:[NSValue valueWithRange:targetRange]];
  1856. objc_setAssociatedObject(label, @"targetRanges", ranges, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  1857. }
  1858. label.attributedText = attributedString;
  1859. }
  1860. + (void)addTapGestureToLabel:(UILabel *)label
  1861. target:(id)target
  1862. action:(SEL)action {
  1863. if (!label || !target || !action) {
  1864. return;
  1865. }
  1866. // 确保label可以接收用户交互
  1867. label.userInteractionEnabled = YES;
  1868. // 移除已存在的手势(避免重复添加)
  1869. for (UIGestureRecognizer *gesture in label.gestureRecognizers) {
  1870. if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
  1871. [label removeGestureRecognizer:gesture];
  1872. }
  1873. }
  1874. // 添加点击手势
  1875. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
  1876. [label addGestureRecognizer:tapGesture];
  1877. // 存储相关信息
  1878. objc_setAssociatedObject(label, @"target", target, OBJC_ASSOCIATION_ASSIGN);
  1879. objc_setAssociatedObject(label, @"action", NSStringFromSelector(action), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  1880. }
  1881. + (void)setMultipleStyledTextForLabel:(UILabel *)label
  1882. fullText:(NSString *)fullText
  1883. styleConfigs:(NSArray<NSDictionary *> *)styleConfigs {
  1884. if (!label || !fullText || !styleConfigs || styleConfigs.count == 0) {
  1885. return;
  1886. }
  1887. // 创建初始富文本
  1888. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullText];
  1889. // 设置整体文本样式
  1890. [attributedString addAttribute:NSFontAttributeName
  1891. value:[UIFont systemFontOfSize:14.0]
  1892. range:NSMakeRange(0, fullText.length)];
  1893. [attributedString addAttribute:NSForegroundColorAttributeName
  1894. value:[UIColor blackColor]
  1895. range:NSMakeRange(0, fullText.length)];
  1896. NSMutableArray *ranges = [[NSMutableArray alloc] init];
  1897. // 遍历所有样式配置
  1898. for (NSDictionary *config in styleConfigs) {
  1899. NSString *targetText = config[@"targetText"];
  1900. UIColor *color = config[@"color"];
  1901. NSNumber *hasUnderlineNum = config[@"hasUnderline"];
  1902. if (!targetText || !color || !hasUnderlineNum) {
  1903. continue; // 跳过无效配置
  1904. }
  1905. BOOL hasUnderline = [hasUnderlineNum boolValue];
  1906. // 查找目标文本的位置
  1907. NSRange targetRange = [fullText rangeOfString:targetText];
  1908. if (targetRange.location != NSNotFound) {
  1909. // 设置目标文本颜色
  1910. [attributedString addAttribute:NSForegroundColorAttributeName
  1911. value:color
  1912. range:targetRange];
  1913. // 添加或移除下划线
  1914. if (hasUnderline) {
  1915. [attributedString addAttribute:NSUnderlineStyleAttributeName
  1916. value:@(NSUnderlineStyleSingle)
  1917. range:targetRange];
  1918. [attributedString addAttribute:NSUnderlineColorAttributeName
  1919. value:color
  1920. range:targetRange];
  1921. } else {
  1922. [attributedString removeAttribute:NSUnderlineStyleAttributeName range:targetRange];
  1923. [attributedString removeAttribute:NSUnderlineColorAttributeName range:targetRange];
  1924. }
  1925. // 收集范围信息
  1926. [ranges addObject:[NSValue valueWithRange:targetRange]];
  1927. }
  1928. }
  1929. // 存储所有范围信息
  1930. objc_setAssociatedObject(label, @"targetRanges", ranges, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  1931. // 设置富文本
  1932. label.attributedText = attributedString;
  1933. }
  1934. + (UILabel *)createFeedbackLabelWithText:(NSString *)text
  1935. feedbackText:(NSString *)feedbackText
  1936. target:(id)target
  1937. action:(SEL)action {
  1938. UILabel *label = [[UILabel alloc] init];
  1939. label.numberOfLines = 0;
  1940. // 使用新的分离方法设置样式
  1941. [self setStyledTextForLabel:label
  1942. fullText:text
  1943. targetText:feedbackText
  1944. color:[UIColor blueColor]
  1945. hasUnderline:YES];
  1946. // 使用新的分离方法添加点击事件
  1947. [self addTapGestureToLabel:label target:target action:action];
  1948. return label;
  1949. }
  1950. + (UIFont *)oswaldDemiBoldFont:(CGFloat)size {
  1951. return [self getTheFontWithSize:size AndFontName:kOswaldFontStr];
  1952. }
  1953. + (UIFont *)poppinsLightFont:(CGFloat)size {
  1954. return [self getTheFontWithSize:size AndFontName:kPoppinsLightFontStr];
  1955. }
  1956. + (UIFont *)poppinsRegularFont:(CGFloat)size {
  1957. return [self getTheFontWithSize:size AndFontName:kPoppinsRegularFontStr];
  1958. }
  1959. + (UIFont *)poppinsMediumFont:(CGFloat)size {
  1960. return [self getTheFontWithSize:size AndFontName:kPoppinsMediumFontStr];
  1961. }
  1962. + (UIFont *)poppinsBoldFont:(CGFloat)size {
  1963. return [self getTheFontWithSize:size AndFontName:kPoppinsSemiBoldFontStr];
  1964. }
  1965. + (UIFont *)poppinsSemiBoldFont:(CGFloat)size {
  1966. return [self getTheFontWithSize:size AndFontName:kPoppinsBoldFontStr];
  1967. }
  1968. + (UIFont *)poppinsExtraBoldFont:(CGFloat)size {
  1969. return [self getTheFontWithSize:size AndFontName:kPoppinsExtraBoldFontStr];
  1970. }
  1971. + (UIFont *)poppinskPoppinsMediumItalicFont:(CGFloat)size {
  1972. return [self getTheFontWithSize:size AndFontName:kPoppinsMediumItalicFontStr];
  1973. }
  1974. + (UIFont *)poppinsBoldItalicFont:(CGFloat)size {
  1975. return [self getTheFontWithSize:size AndFontName:kPoppinsBoldItalicFontStr];
  1976. }
  1977. + (UIFont *)lightFont:(CGFloat)size {
  1978. return [UIFont systemFontOfSize:size weight:UIFontWeightLight];
  1979. }
  1980. + (UIFont *)regularFont:(CGFloat)size {
  1981. return [UIFont systemFontOfSize:size weight:UIFontWeightRegular];
  1982. }
  1983. + (UIFont *)mediumFont:(CGFloat)size {
  1984. return [UIFont systemFontOfSize:size weight:UIFontWeightMedium];
  1985. }
  1986. + (UIFont *)boldFont:(CGFloat)size {
  1987. return [UIFont systemFontOfSize:size weight:UIFontWeightBold];
  1988. }
  1989. + (UIFont *)semiboldFont:(CGFloat)size {
  1990. return [UIFont systemFontOfSize:size weight:UIFontWeightSemibold];
  1991. }
  1992. @end