当前位置:  开发笔记 > 编程语言 > 正文

如何在UILabel中选择特定单词(Swift 2.0)?

如何解决《如何在UILabel中选择特定单词(Swift2.0)?》经验,为你挑选了1个好方法。



1> rushisangani..:

您可以使用UITextview并添加点按手势,而不是使用UILabel.这是一个例子,通过你可以实现相同的结果.

/* Set Tap gesture on Text view */

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapResponse:)];
tapGesture.numberOfTapsRequired = 1;
[yourTextView addGestureRecognizer:tapGesture];

- (void) tapResponse:(UITapGestureRecognizer *)recognizer
{

UITextView *textView =  (UITextView *)recognizer.view;
CGPoint location = [recognizer locationInView:textView];

CGPoint position = CGPointMake(location.x, location.y);

//get location in text from textposition at point
UITextPosition *tapPosition = [textView closestPositionToPoint:position];

//fetch the word at this position (or nil, if not available)
UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSString *tappedWord = [textView textInRange:textRange];

NSLog(@"tapped word : %@", tappedWord);    
}

推荐阅读
Chloemw
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有