之前公司要做这个功能,然后发现了几个类,
UITextPosition
,UITextRange
,这两个类,UITextView
和UIFieldText
都有,然后UITextView
还有一个特殊的属性,就是selectRange
,这个属性,就是表示UITextView的所选择文字在整个字符串的位置
但是
UITextField
没有这个属性,所以很多人决定给他写一个,我们先通过这个例子学习一下,题目的两个属性,然后再去聊聊第一张图的功能是如何实现的。
我们去看看如何给UITextField
添加一个selectRange
的属性
#import <UIKit/UIKit.h>
@interface UITextField (WXExtention)
/**
生成set,get方法
*/
@property(nonatomic,assign)NSRange selectedRange;
@end
#import "UITextField+WXExtention.h"
@implementation UITextField (WXExtention)
/**
* 假设我们整个文本是 “123456789abcdefg”,我们在textField选中了“34567”
* 我们这个时候,只能获取到UITextRange,但是我们要获取NSRange
* UITextRange 这个是相对于@“UITextView”和@"UITextField"内部的,NSRange是相对于字符串的
*这个分类实际上是UITextRange和NSRange的互相转化
*/
/**
通过我们传递的范围,在textField中选中字符串
@param selectedRange 在字符串中要去选中的范围
*NSRange -> UITextRange
*/
- (void)setSelectedRange:(NSRange)selectedRange{
//beginningOfDocument 文本的开始位置,也就是123456789abcdefg,@“1”这个位置
UITextPosition* beginning = self.beginningOfDocument;
//positionFromPosition 通过这个方法,我们先取得@“3”这个位置
UITextPosition* startPosition = [self positionFromPosition:beginning offset:selectedRange.location];
//获取@“7”这个位置
UITextPosition* endPosition = [self positionFromPosition:beginning offset:selectedRange.location + selectedRange.length];
//通过连个 @“UITextPostion”获取到UITextRage
UITextRange* selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
//让textField选中具体位置
[self setSelectedTextRange:selectionRange];
}
/**
获取当前textField的选中字符串的范围
*UITextRange -> NSRange
*/
- (NSRange)selectedRange{
//获取textField中文本开始点 @"1"这个位置
UITextPosition* beginning = self.beginningOfDocument;
//获取textField选中选中范围 @“UITextRange”
UITextRange* selectedRange = self.selectedTextRange;
//获取开始点@“3”的位置
UITextPosition* selectionStart = selectedRange.start;
//结束点@"7"的位置
UITextPosition* selectionEnd = selectedRange.end;
//计算NSRange
const NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
const NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
//返回范围
return NSMakeRange(location, length);
}
@end
在控制中,我们在代理方法中,选中一个范围
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[self.field setSelectedRange:NSMakeRange(4, 3)];
}
我们熟悉了这几个类之间的关系,那么我们看看如何在
UITextView
中添加特殊文本绑定的功能
一共就两个地方最核心,一个是如何获取长按特殊文本时候,我们如何确定光标应该在他的左变还是右边,另一个就是删除的时候,如何确定textView
的selectedRange
获取绑定范围的代码
- (void)observeValueForKeyPath:(NSString*) path
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
if (context == TextViewObserverSelectedTextRange && [path isEqual:@"selectedTextRange"] && !self.enableEditInsterText){
UITextRange *newContentStr = [change objectForKey:@"new"];
UITextRange *oldContentStr = [change objectForKey:@"old"];
NSRange newRange = [self selectedRange:self selectTextRange:newContentStr];
NSRange oldRange = [self selectedRange:self selectTextRange:oldContentStr];
if (newRange.location != oldRange.location) {
//判断光标移动,光标不能处在特殊文本内
[self.attributedText enumerateAttribute:SPECIAL_TEXT_NUM inRange:NSMakeRange(0, self.attributedText.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
// NSLog(@"range = %@",NSStringFromRange(range));
if (attrs != nil && attrs != 0) {
if (newRange.location > range.location && newRange.location < (range.location+range.length)) {
//光标距离左边界的值
NSUInteger leftValue = newRange.location - range.location;
//光标距离右边界的值
NSUInteger rightValue = range.location+range.length - newRange.location;
if (leftValue >= rightValue) {
self.selectedRange = NSMakeRange(self.selectedRange.location-leftValue, 0);
}else{
self.selectedRange = NSMakeRange(self.selectedRange.location+rightValue, 0);
}
}
}
}];
}
}else{
[super observeValueForKeyPath:path ofObject:object change:change context:context];
}
self.typingAttributes = self.defaultAttributes;
}
删除操作
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
self.typingAttributes = self.defaultAttributes;
if ([text isEqualToString:@""] && !self.enableEditInsterText) {//删除
__block BOOL deleteSpecial = NO;
NSRange oldRange = textView.selectedRange;
[textView.attributedText enumerateAttribute:SPECIAL_TEXT_NUM inRange:NSMakeRange(0, textView.selectedRange.location) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
NSRange deleteRange = NSMakeRange(textView.selectedRange.location-1, 0) ;
if (attrs != nil && attrs != 0) {
if (deleteRange.location > range.location && deleteRange.location < (range.location+range.length)) {
NSMutableAttributedString *textAttStr = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
[textAttStr deleteCharactersInRange:range];
textView.attributedText = textAttStr;
deleteSpecial = YES;
textView.selectedRange = NSMakeRange(oldRange.location-range.length, 0);
*stop = YES;
}
}
}];
return !deleteSpecial;
}
//输入了done
if ([text isEqualToString:@"\n"]) {
if (self.myDelegate && [self.myDelegate respondsToSelector:@selector(SETextViewEnterDone:)]) {
[self.myDelegate SETextViewEnterDone:self];
}
if (self.returnKeyType == UIReturnKeyDone) {
[self resignFirstResponder];
return NO;
}
}
if (self.myDelegate && [self.myDelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
return [self.myDelegate textView:self shouldChangeTextInRange:range replacementText:text];
}
return YES;
}