为融云聊天页面的输入框添加 Placeholder

112 阅读1分钟

产品要求给输入框加个Placeh,其实挺简单一功能,寻遍他们的官网www.rongcloud.cn/和文档docs.rongcloud.cn/v4/都没有找到相关资料,现实很残酷,SDK 木有这个接口,只能自己实现了,思来想去,用了个笨办法,加个 UILabel 一试,还真行,有需要的您请往下看。

其实就是给输入框价格 UILabel,在该显示的时候显示,该隐藏的时候隐藏就完事儿了,代码如下:

  1. 在聊天页面添加一个 UILabel 属性

    @property(nonatomic, strong) UILabel *placeholderLabel;

  2. 初始化并添加 placeholderLabel 对象

    • (void)configPlaceholder { //初始化和设置 self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 20)]; [self.chatSessionInputBarControl.inputTextView addSubview:self.placeholderLabel]; self.placeholderLabel.text = @"测试 Placeholder"; self.placeholderLabel.textColor = [UIColor grayColor]; self.placeholderLabel.userInteractionEnabled = YES; //添加点击手势 UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPlaceholderLabel)]; [self.placeholderLabel addGestureRecognizer:tapLabel];} - (void)tapPlaceholderLabel { [self.chatSessionInputBarControl updateStatus:KBottomBarKeyboardStatus animated:YES];}
  3. 在内容发生变化和点击发送后,设置 placeholder 效果的显示

    • (void)inputTextView:(UITextView *)inputTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { //在内容发生变化和点击发送后,设置 placeholder 效果的显示 if ((range.location == 0 && [text isEqualToString:@""]) || [text isEqualToString:@""]) { self.placeholderLabel.hidden = NO; } else { self.placeholderLabel.hidden = YES; }}
  4. 还要提一下,融云的 SDK 有撤回消息以后的“重新编辑”功能,在这个时候,要关闭 placeholder 效果的显示

    • (void)didTapReedit:(RCMessageModel *)model { self.placeholderLabel.hidden = YES; [super didTapReedit:model];}

到这儿功能就完成了,placeholderLabel 的具体文字效果可以自行修改调整,希望上面的代码可以帮到你,喜欢的话,点个赞吧。