闪退问题
scrollViewDidScroll
改为
scrollViewWillBeginDragging
禁止编辑
[text setEditable:NO];
/**
* 光标位置输入
*
* @param emoji 要输入的内容emoji和字符
*/
- (void)insertStringWithGuangBiaotext:(UITextView *)textView emoji:(NSString *)emoji {
NSRange range = textView.selectedRange;
NSLog(@"%zd-光标位置输入-%zd",range.length,range.location);
NSUInteger location = textView.selectedRange.location;
NSString * textStr = textView.text;
NSString *resultStr = [NSString stringWithFormat:@"%@%@%@",[textStr substringToIndex:location],emoji,[textStr substringFromIndex:location]];
textView.text = resultStr;
range.location = range.location + [emoji length];
NSLog(@"%zd=%zd+%zd",range.location,range.location,[emoji length]);
textView.selectedRange = NSMakeRange(range.location,0);
}
/**
* 光标位置删除
*/
- (void)deleteString:(UITextView *)textView {
NSRange range = textView.selectedRange;
NSLog(@"%zd-表情删除-%zd",range.length,range.location);
if (textView.text.length > 0) {
NSUInteger location = textView.selectedRange.location;
NSString *head = [textView.text substringToIndex:location];
if (range.length ==0) {
}else{
NSLog(@"文字为全选");
textView.text =@"";
}
if (location > 0) {
// NSUInteger location = self.inputView.toolBar.textView.selectedRange.location;
NSMutableString *str = [NSMutableString stringWithFormat:@"%@",textView.text];
[self lastRange:head];
NSLog(@"%zd===%zd",[self lastRange:head].location,[self lastRange:head].length);
NSLog(@"%@",str);
[str deleteCharactersInRange:[self lastRange:head]];
NSLog(@"%@",str);
textView.text = str;
textView.selectedRange = NSMakeRange([self lastRange:head].location,0);
} else {
textView.selectedRange = NSMakeRange(0,0);
}
}
}
/**
* 计算选中的最后一个是字符还是表情所占长度
*
* @param str 要计算的字符串
*
* @return 返回一个 NSRange
*/
- (NSRange)lastRange:(NSString *)str {
NSRange lastRange = [str rangeOfComposedCharacterSequenceAtIndex:str.length-1];
return lastRange;
}
1.个数判断
- (void)textViewDidChange:(UITextView *)textView{
NSLog(@"%lu",(unsigned long)textView.text.length);
_numTipLabel.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)textView.text.length];
if (![textView.text isEqualToString:@""]) {
_textViewTipLabel.hidden = YES;
}else{
_textViewTipLabel.hidden = NO;
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (range.location >=200 || text.length >= 200)
{
return NO;
}
return YES;
}
2.光标高度修改
创建子类
- (CGRect)caretRectForPosition:(UITextPosition *)position {
CGRect originalRect = [super caretRectForPosition:position];
originalRect.size.height = 19;
originalRect.size.width = 2;
originalRect.origin.y = 8;
originalRect.origin.x = 0;
return originalRect;
}
3.return键收起键盘
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if([text isEqualToString:@"\n"]){
[textView resignFirstResponder];
return NO;
}
return YES;
}
4.屏幕上移
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(@"开始编辑");
//设置动画的名字
[UIView beginAnimations:@"Animation" context:nil];
//设置动画的间隔时间
[UIView setAnimationDuration:0.20];
//??使用当前正在运行的状态开始下一段动画
[UIView setAnimationBeginsFromCurrentState: YES];
//设置视图移动的位移
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 200, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束
[UIView commitAnimations];
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
NSLog(@"结束编辑");
//设置动画的名字
[UIView beginAnimations:@"Animation" context:nil];
//设置动画的间隔时间
[UIView setAnimationDuration:0.20];
//??使用当前正在运行的状态开始下一段动画
[UIView setAnimationBeginsFromCurrentState: YES];
//设置视图移动的位移
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 200, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束
[UIView commitAnimations];
return YES;
}