无法在 UITextView 中显示 select 文本
Cannot select text in UITextView
我有一个 UITextView。然后我向 UITextView 添加了双击手势识别器。因此,当我在 UITextView 中双击时,我无法在 UITextView 中输入 select 文本。
我如何才能 select 发短信并同时获得双击事件?
提前致谢
这是我的代码:
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondToTapGesture:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
[textMeaning addGestureRecognizer:doubleTapRecognizer];
如果添加手势 -> 事件 select 不可用
所以,你应该改变你的流程!
试试这个:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped:)];
[tap setNumberOfTapsRequired:2];
[_txtVw addGestureRecognizer:tap];
}
-(void)tapped:(UITapGestureRecognizer *)recognizer
{
UITextView *textView_New = (UITextView *)recognizer.view;
CGPoint pos = [recognizer locationInView:textView_New];
NSLog(@"Tap Gesture Coordinates: %.2f %.2f", pos.x, pos.y);
UITextPosition *tapPos = [textView_New closestPositionToPoint:pos];
UITextRange * word = [textView_New.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSLog(@"WORD: %@", [textView_New textInRange:word]);
}
在您当前的代码中试试这个:
.h文件
UITextField *txtField;
.m 文件
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[txtField addGestureRecognizer:tapGesture];
}
- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateRecognized) {
// Perform your operation which you want to do
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[txtField addGestureRecognizer:tapGesture];
}
- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateRecognized) {
[self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]];
}
}
试试这个
到select范围内的文本
int start = 2;
int end = 5;
UITextPosition *startPosition = [self positionFromPosition:self.beginningOfDocument offset:start];
UITextPosition *endPosition = [self positionFromPosition:self.beginningOfDocument offset:end];
UITextRange *selection = [self textRangeFromPosition:startPosition toPosition:endPosition];
self.selectedTextRange = selection;
我不希望用户能够更改文本,因此在 UITextView 的界面生成器中取消选择 "Editable"。
将 UITextView 的委托设置为此 class !!!
然后使用
-(void)textViewDidChangeSelection:(UITextView *)textView{
[textView setSelectedTextRange:[textView textRangeFromPosition:textView.beginningOfDocument toPosition:textView.endOfDocument]]; }
我有一个 UITextView。然后我向 UITextView 添加了双击手势识别器。因此,当我在 UITextView 中双击时,我无法在 UITextView 中输入 select 文本。 我如何才能 select 发短信并同时获得双击事件? 提前致谢
这是我的代码:
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondToTapGesture:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
[textMeaning addGestureRecognizer:doubleTapRecognizer];
如果添加手势 -> 事件 select 不可用
所以,你应该改变你的流程!
试试这个:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped:)];
[tap setNumberOfTapsRequired:2];
[_txtVw addGestureRecognizer:tap];
}
-(void)tapped:(UITapGestureRecognizer *)recognizer
{
UITextView *textView_New = (UITextView *)recognizer.view;
CGPoint pos = [recognizer locationInView:textView_New];
NSLog(@"Tap Gesture Coordinates: %.2f %.2f", pos.x, pos.y);
UITextPosition *tapPos = [textView_New closestPositionToPoint:pos];
UITextRange * word = [textView_New.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSLog(@"WORD: %@", [textView_New textInRange:word]);
}
在您当前的代码中试试这个:
.h文件
UITextField *txtField;
.m 文件
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[txtField addGestureRecognizer:tapGesture];
}
- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateRecognized) {
// Perform your operation which you want to do
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[txtField addGestureRecognizer:tapGesture];
}
- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateRecognized) {
[self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]];
}
}
试试这个
到select范围内的文本
int start = 2;
int end = 5;
UITextPosition *startPosition = [self positionFromPosition:self.beginningOfDocument offset:start];
UITextPosition *endPosition = [self positionFromPosition:self.beginningOfDocument offset:end];
UITextRange *selection = [self textRangeFromPosition:startPosition toPosition:endPosition];
self.selectedTextRange = selection;
我不希望用户能够更改文本,因此在 UITextView 的界面生成器中取消选择 "Editable"。 将 UITextView 的委托设置为此 class !!!
然后使用
-(void)textViewDidChangeSelection:(UITextView *)textView{
[textView setSelectedTextRange:[textView textRangeFromPosition:textView.beginningOfDocument toPosition:textView.endOfDocument]]; }