TCShowLiveInputView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // TCShowLiveInputView.m
  3. // TCShow
  4. //
  5. // Created by AlexiChen on 15/11/16.
  6. // Copyright © 2015年 AlexiChen. All rights reserved.
  7. //
  8. #import "TCShowLiveInputView.h"
  9. @implementation TCShowLiveInputView
  10. - (void)dealloc
  11. {
  12. [[NSNotificationCenter defaultCenter] removeObserver:self];
  13. }
  14. /**
  15. 请求完接口后,刷新直播间相关信息
  16. @param liveItem 视频Item
  17. @param liveInfo get_video2接口获取下来的数据实体
  18. */
  19. - (void)refreshLiveItem:(id<FWShowLiveRoomAble>)liveItem liveInfo:(CurrentLiveInfo *)liveInfo
  20. {
  21. _liveInfo = liveInfo;
  22. }
  23. - (NSString *)text
  24. {
  25. return _textField.text;
  26. }
  27. - (void)setText:(NSString *)text
  28. {
  29. _textField.text = text;
  30. }
  31. - (instancetype)init
  32. {
  33. self = [super init];
  34. if (self) {
  35. [self addOwnViews];
  36. }
  37. return self;
  38. }
  39. - (void)setPlacehoholder:(NSString *)placeholder
  40. {
  41. if (!placeholder || placeholder.length == 0)
  42. {
  43. _textField.placeholder = nil;
  44. return;
  45. }
  46. _textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName:kWhiteColor}];
  47. }
  48. - (void)addOwnViews
  49. {
  50. self.backgroundColor = [UIColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.00];
  51. _textField = [[UITextField alloc] init];
  52. _textField.textColor = kAppGrayColor1;
  53. _textField.font = kAppMiddleTextFont;
  54. _textField.returnKeyType = UIReturnKeySend;
  55. _textField.delegate = self;
  56. // _textField.layer.cornerRadius = kCornerRadius;
  57. _textField.backgroundColor = kWhiteColor;
  58. _textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 10)];
  59. _textField.leftViewMode = UITextFieldViewModeAlways;
  60. _textField.layer.borderColor = [UIColor colorWithRed:0.97 green:0.96 blue:0.98 alpha:1.00].CGColor;
  61. _textField.layer.borderWidth = 1;
  62. _textField.layer.cornerRadius = kMyBtnWidth1/2;
  63. [self addSubview:_textField];
  64. // FWWeakify(self)
  65. //是否打开弹幕
  66. // _barrageSwitch = [[KLSwitch alloc]initWithFrame:CGRectMake(0, 0, 50, 30) didChangeHandler:^(BOOL isOn) {
  67. //
  68. // FWStrongify(self)
  69. // if (isOn && !self.isHost)
  70. // {
  71. // self.textField.placeholder = [NSString stringWithFormat:ASLocalizedString(@"开启大喇叭,%ld%@/条"),(long)[GlobalVariables sharedInstance].appModel.bullet_screen_diamond,self.BuguLive.appModel.diamond_name];
  72. // }
  73. // else
  74. // {
  75. // self.textField.placeholder = ASLocalizedString(@"和大家说点什么");
  76. // }
  77. // }];
  78. // [_barrageSwitch setOnTintColor:kAppMainColor];
  79. // [_barrageSwitch setOnImage:[UIImage imageNamed:@"ic_send_pop_msg_disable"]];
  80. // [_barrageSwitch setOffImage:[UIImage imageNamed:@"ic_send_pop_msg_enable"]];
  81. _barrageBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 75, 33)];
  82. [_barrageBtn setImage:[UIImage imageNamedL:@"lr_barrage_btn_nor"] forState:UIControlStateNormal];
  83. [_barrageBtn setImage:[UIImage imageNamedL:@"lr_barrage_btn_sel"] forState:UIControlStateSelected];
  84. [_barrageBtn addTarget:self action:@selector(barrageBtnAction) forControlEvents:UIControlEventTouchUpInside];
  85. [self addSubview:_barrageBtn];
  86. _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
  87. _confirmButton.layer.cornerRadius = 33/2;
  88. _confirmButton.clipsToBounds = YES;
  89. [_confirmButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
  90. _confirmButton.titleLabel.font = kAppMiddleTextFont;
  91. [_confirmButton setTitle:ASLocalizedString(@"发送")forState:UIControlStateNormal];
  92. // [_confirmButton setBackgroundImage:[BGUtils imageWithColor:[UIColor colorWithRed:0.99 green:0.22 blue:0.45 alpha:1.00]] forState:UIControlStateNormal];
  93. // [_confirmButton setBackgroundImage:[BGUtils imageWithColor:[[UIColor colorWithRed:0.99 green:0.22 blue:0.45 alpha:1.00] colorWithAlphaComponent:0.6]] forState:UIControlStateHighlighted];
  94. [_confirmButton setBackgroundImage:[UIImage imageNamed:@"发送按钮"] forState:UIControlStateNormal];
  95. [_confirmButton addTarget:self action:@selector(onClickSend) forControlEvents:UIControlEventTouchUpInside];
  96. [self addSubview:_confirmButton];
  97. [self changeSendMsgState];
  98. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSendMsgState) name:kLiveRoomCanSendMessage object:nil];
  99. }
  100. - (void)barrageBtnAction{
  101. _barrageBtn.selected = !_barrageBtn.isSelected;
  102. if (_barrageBtn.isSelected && !self.isHost)
  103. {
  104. self.textField.placeholder = [NSString stringWithFormat:ASLocalizedString(@"开启大喇叭,%ld%@/条"),(long)[GlobalVariables sharedInstance].appModel.bullet_screen_diamond,self.BuguLive.appModel.diamond_name];
  105. }
  106. else
  107. {
  108. self.textField.placeholder = ASLocalizedString(@"和大家说点什么");
  109. }
  110. }
  111. - (void)onClickSend
  112. {
  113. if (!_isHost && [[IMAPlatform sharedInstance].host getUserRank] < [GlobalVariables sharedInstance].appModel.send_msg_lv)
  114. {
  115. [FanweMessage alert:ASLocalizedString(@"您当前等级不能发言!")];
  116. return;
  117. }
  118. NSString *tmpSendMsgStr = [_textField.text trim];
  119. if ([BGUtils isBlankString:tmpSendMsgStr])
  120. {
  121. return;
  122. }
  123. // 弹幕不做限制
  124. if ([_barrageBtn isSelected])
  125. {
  126. if (_delegate && [_delegate respondsToSelector:@selector(sendMsg:)])
  127. {
  128. [_delegate sendMsg:self];
  129. }
  130. }
  131. else
  132. {
  133. if (_canSendMsg)
  134. {
  135. if ([_sendMsgStr isEqualToString:tmpSendMsgStr])
  136. {
  137. _sendSameMsgTime ++;
  138. }
  139. else
  140. {
  141. _sendSameMsgTime = 0;
  142. }
  143. if (_sendSameMsgTime > 2)
  144. {
  145. [FanweMessage alert:ASLocalizedString(@"请勿刷屏")];
  146. return;
  147. }
  148. if (_delegate && [_delegate respondsToSelector:@selector(sendMsg:)])
  149. {
  150. _sendMsgStr = tmpSendMsgStr;
  151. _canSendMsg = NO;
  152. [self performSelector:@selector(changeSendMsgState) withObject:nil afterDelay:2];
  153. [_delegate sendMsg:self];
  154. }
  155. }
  156. else
  157. {
  158. [FanweMessage alert:ASLocalizedString(@"请勿频繁发言")];
  159. }
  160. }
  161. }
  162. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  163. {
  164. if (!_isHost && [[IMAPlatform sharedInstance].host getUserRank] < [GlobalVariables sharedInstance].appModel.send_msg_lv)
  165. {
  166. [FanweMessage alert:ASLocalizedString(@"您当前等级不能发言!")];
  167. return NO;
  168. }
  169. if ([BGUtils isBlankString:textField.text])
  170. {
  171. return NO;
  172. }
  173. if (_canSendMsg)
  174. {
  175. [self onClickSend];
  176. return YES;
  177. }
  178. else
  179. {
  180. [FanweMessage alert:ASLocalizedString(@"请勿频繁发言")];
  181. return NO;
  182. }
  183. }
  184. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  185. {
  186. if (_barrageBtn.isSelected && !_isHost)
  187. {
  188. textField.placeholder = [NSString stringWithFormat:ASLocalizedString(@"开启大喇叭,%ld%@/条"),(long)[GlobalVariables sharedInstance].appModel.bullet_screen_diamond,self.BuguLive.appModel.diamond_name];
  189. }
  190. else
  191. {
  192. _textField.placeholder = ASLocalizedString(@"和大家说点什么");
  193. }
  194. _isInputViewActive = YES;
  195. return YES;
  196. }
  197. - (void)textFieldDidBeginEditing:(UITextField *)textField
  198. {
  199. _isInputViewActive = YES;
  200. }
  201. - (void)textFieldDidBeginEditing
  202. {
  203. _isInputViewActive = YES;
  204. }
  205. - (void)setLimitLength:(NSInteger)limitLength
  206. {
  207. if (limitLength > 0)
  208. {
  209. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTextFiledEditChanged:) name:@"UITextFieldTextDidChangeNotification" object:nil];
  210. }
  211. else
  212. {
  213. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:nil];
  214. }
  215. _limitLength = limitLength;
  216. }
  217. // 监听字符变化,并处理
  218. - (void)onTextFiledEditChanged:(NSNotification *)obj
  219. {
  220. if (_limitLength > 0)
  221. {
  222. UITextField *textField = _textField;
  223. NSString *toBeString = textField.text;
  224. //获取高亮部分
  225. UITextRange *selectedRange = [textField markedTextRange];
  226. UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
  227. // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
  228. if (!position)
  229. {
  230. if (toBeString.length > _limitLength)
  231. {
  232. [textField shake];
  233. NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:_limitLength];
  234. if (rangeIndex.length == 1)
  235. {
  236. textField.text = [toBeString substringToIndex:_limitLength];
  237. }
  238. else
  239. {
  240. NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, _limitLength)];
  241. textField.text = [toBeString substringWithRange:rangeRange];
  242. }
  243. }
  244. }
  245. }
  246. }
  247. - (void)relayoutFrameOfSubViews
  248. {
  249. _barrageBtn.frame = CGRectMake(0, (CGRectGetHeight(self.frame)-33)/2, 75, 33);
  250. _confirmButton.frame = CGRectMake(CGRectGetWidth(self.frame)-75, (CGRectGetHeight(self.frame)-33)/2, 70, 33);
  251. _textField.frame = CGRectMake(CGRectGetMaxX(_barrageBtn.frame)+kDefaultMargin-2, (CGRectGetHeight(self.frame)-kMyBtnWidth1)/2, kScreenW - 145 - kDefaultMargin * 2 + 4, kMyBtnWidth1);
  252. }
  253. - (void)setIsHost:(BOOL)isHost
  254. {
  255. _isHost = isHost;
  256. [self changeSendMsgState];
  257. }
  258. - (void)changeSendMsgState
  259. {
  260. if (_isHost)
  261. {
  262. _canSendMsg = YES;
  263. }
  264. else if ([[IMAPlatform sharedInstance].host getUserRank] >= [GlobalVariables sharedInstance].appModel.send_msg_lv)
  265. {
  266. _canSendMsg = YES;
  267. }
  268. }
  269. - (BOOL)isInputViewActive
  270. {
  271. return _isInputViewActive;
  272. }
  273. - (BOOL)resignFirstResponder
  274. {
  275. _isInputViewActive = NO;
  276. [super resignFirstResponder];
  277. return [_textField resignFirstResponder];
  278. }
  279. - (BOOL)becomeFirstResponder
  280. {
  281. _isInputViewActive = YES;
  282. return [_textField becomeFirstResponder];
  283. }
  284. @end