iOS 获取label上点击位置的字符位置
2017-03-15 14:04
405 查看
手势点击事件
-(void)dotap:(UITapGestureRecognizer *)tap
{
CGPoint point=[tap
locationInView:self.tableView];
NSIndexPath *indexpath=[self.tableView
indexPathForRowAtPoint:point];
if (indexpath) {
ChatTableViewCell *cell=[self.tableView
cellForRowAtIndexPath:indexpath];
UILabel *lable=nil;
for (UIView *view
in cell.contentView.subviews) {
if ([view
isKindOfClass:[UILabel
class]]) {
lable=(UILabel*)view;
break;
}
}
CGPoint p = [tap
locationInView:cell];
if (CGRectContainsPoint(lable.frame, p)) {
NSError *error =
NULL;
NSDataDetector *detector = [NSDataDetector
dataDetectorWithTypes:NSTextCheckingTypeLink
error:&error];
self.matches = [detector
matchesInString:lable.text
options:0 range:NSMakeRange(0, lable.text.length)];
//CFIndex index = [self characterIndexAtPoint:[tap locationInView:lable]];
CFIndex index=[self
getindexArrayOfStringInLabel:lable
Point:[tap locationInView:lable]];
NSLog(@"index=%ld",index);
for (NSTextCheckingResult *match
in self.matches) {
if ([match
resultType] == NSTextCheckingTypeLink) {
NSRange matchRange = [match
range];
if ([self
isIndex:index inRange:matchRange]) {
// NSLog(@"点击跳转页面");
NSLog(@"matchRange=%@",[lable.text
substringWithRange:matchRange]);
// [[UIApplication sharedApplication] openURL:match.URL];
break;
}
}
}
}
}
}
- (BOOL)isIndex:(CFIndex)index inRange:(NSRange)range {
return index >= range.location && index <= range.location+range.length;
}
#pragma mark -****获取lable中的点击位置的字符的index
-(CFIndex)getindexArrayOfStringInLabel:(UILabel *)label Point:(CGPoint)point
{
NSString *text = [label
text];
UIFont *font = [label
font];
CGRect rect = [label
bounds];
// point = CGPointMake(point.x - rect.origin.x, point.y - rect.origin.y);
// Convert tap coordinates (start at top left) to CT coordinates (start at bottom left)
坐标系反转
point = CGPointMake(point.x, rect.size.height
- point.y);
NSUInteger idx =
NSNotFound;
CTFontRef myFont =
CTFontCreateWithName((CFStringRef)font.fontName,
font.pointSize,
NULL);
NSMutableAttributedString *attStr = [[NSMutableAttributedString
alloc] initWithString:text];
[attStr addAttribute:(NSString *)kCTFontAttributeName
value:(__bridge
id)myFont
range:NSMakeRange(0, attStr.length)];
CTFramesetterRef frameSetter =
CTFramesetterCreateWithAttributedString((__bridge
CFAttributedStringRef)attStr);
CGMutablePathRef path =
CGPathCreateMutable();
CGPathAddRect(path,
NULL,
CGRectMake(0,0,rect.size.width,rect.size.height+20));
CTFrameRef frame =
CTFramesetterCreateFrame(frameSetter,
CFRangeMake(0, 0), path,
NULL);
// NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
CFArrayRef lines =
CTFrameGetLines(frame);
NSInteger numberOfLines =CFArrayGetCount(lines);
NSLog(@"numberOfLines=%ld",(long)numberOfLines);
CGPoint lineOrigins[numberOfLines];
CTFrameGetLineOrigins(frame,
CFRangeMake(0, numberOfLines), lineOrigins);
for (CFIndex lineIndex = 0; lineIndex < numberOfLines; lineIndex++) {
CGPoint lineOrigin = lineOrigins[lineIndex];
CTLineRef line =
CFArrayGetValueAtIndex(lines, lineIndex);
CGFloat ascent, descent, leading, width;
width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
CGFloat yMin =
floor(lineOrigin.y - descent);
CGFloat yMax =
ceil(lineOrigin.y + ascent);
if (point.y > yMax) {
break;
}
if (point.y >= yMin) {
// Check if the point is within this line horizontally
if (point.x >= lineOrigin.x && point.x
<= lineOrigin.x + width) {
// Convert CT coordinates to line-relative coordinates
CGPoint relativePoint =
CGPointMake(point.x - lineOrigin.x, point.y
- lineOrigin.y);
idx = CTLineGetStringIndexForPosition(line, relativePoint);
break;
}
}
}
return idx;
}
-(void)dotap:(UITapGestureRecognizer *)tap
{
CGPoint point=[tap
locationInView:self.tableView];
NSIndexPath *indexpath=[self.tableView
indexPathForRowAtPoint:point];
if (indexpath) {
ChatTableViewCell *cell=[self.tableView
cellForRowAtIndexPath:indexpath];
UILabel *lable=nil;
for (UIView *view
in cell.contentView.subviews) {
if ([view
isKindOfClass:[UILabel
class]]) {
lable=(UILabel*)view;
break;
}
}
CGPoint p = [tap
locationInView:cell];
if (CGRectContainsPoint(lable.frame, p)) {
NSError *error =
NULL;
NSDataDetector *detector = [NSDataDetector
dataDetectorWithTypes:NSTextCheckingTypeLink
error:&error];
self.matches = [detector
matchesInString:lable.text
options:0 range:NSMakeRange(0, lable.text.length)];
//CFIndex index = [self characterIndexAtPoint:[tap locationInView:lable]];
CFIndex index=[self
getindexArrayOfStringInLabel:lable
Point:[tap locationInView:lable]];
NSLog(@"index=%ld",index);
for (NSTextCheckingResult *match
in self.matches) {
if ([match
resultType] == NSTextCheckingTypeLink) {
NSRange matchRange = [match
range];
if ([self
isIndex:index inRange:matchRange]) {
// NSLog(@"点击跳转页面");
NSLog(@"matchRange=%@",[lable.text
substringWithRange:matchRange]);
// [[UIApplication sharedApplication] openURL:match.URL];
break;
}
}
}
}
}
}
- (BOOL)isIndex:(CFIndex)index inRange:(NSRange)range {
return index >= range.location && index <= range.location+range.length;
}
#pragma mark -****获取lable中的点击位置的字符的index
-(CFIndex)getindexArrayOfStringInLabel:(UILabel *)label Point:(CGPoint)point
{
NSString *text = [label
text];
UIFont *font = [label
font];
CGRect rect = [label
bounds];
// point = CGPointMake(point.x - rect.origin.x, point.y - rect.origin.y);
// Convert tap coordinates (start at top left) to CT coordinates (start at bottom left)
坐标系反转
point = CGPointMake(point.x, rect.size.height
- point.y);
NSUInteger idx =
NSNotFound;
CTFontRef myFont =
CTFontCreateWithName((CFStringRef)font.fontName,
font.pointSize,
NULL);
NSMutableAttributedString *attStr = [[NSMutableAttributedString
alloc] initWithString:text];
[attStr addAttribute:(NSString *)kCTFontAttributeName
value:(__bridge
id)myFont
range:NSMakeRange(0, attStr.length)];
CTFramesetterRef frameSetter =
CTFramesetterCreateWithAttributedString((__bridge
CFAttributedStringRef)attStr);
CGMutablePathRef path =
CGPathCreateMutable();
CGPathAddRect(path,
NULL,
CGRectMake(0,0,rect.size.width,rect.size.height+20));
CTFrameRef frame =
CTFramesetterCreateFrame(frameSetter,
CFRangeMake(0, 0), path,
NULL);
// NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
CFArrayRef lines =
CTFrameGetLines(frame);
NSInteger numberOfLines =CFArrayGetCount(lines);
NSLog(@"numberOfLines=%ld",(long)numberOfLines);
CGPoint lineOrigins[numberOfLines];
CTFrameGetLineOrigins(frame,
CFRangeMake(0, numberOfLines), lineOrigins);
for (CFIndex lineIndex = 0; lineIndex < numberOfLines; lineIndex++) {
CGPoint lineOrigin = lineOrigins[lineIndex];
CTLineRef line =
CFArrayGetValueAtIndex(lines, lineIndex);
CGFloat ascent, descent, leading, width;
width = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
CGFloat yMin =
floor(lineOrigin.y - descent);
CGFloat yMax =
ceil(lineOrigin.y + ascent);
if (point.y > yMax) {
break;
}
if (point.y >= yMin) {
// Check if the point is within this line horizontally
if (point.x >= lineOrigin.x && point.x
<= lineOrigin.x + width) {
// Convert CT coordinates to line-relative coordinates
CGPoint relativePoint =
CGPointMake(point.x - lineOrigin.x, point.y
- lineOrigin.y);
idx = CTLineGetStringIndexForPosition(line, relativePoint);
break;
}
}
}
return idx;
}
相关文章推荐
- iOS开发 获取图片点击位置的颜色
- ios实现自动获取label高度、宽度及最后一个位置详解
- ios获取点击屏幕的位置
- iOS开发-获取字符串中多个相同字符的位置index
- iOS中获取用户所点击的位置信息,并通过这个位置信息得到tableView的中的cell的indexPath.section
- iOS-高德地图点击地图获取点击点对应的地理位置,并添加自定义的大头针
- iOS 获取点击位置
- ios tableCell 中同时获取点击的位置和所在的indexPath
- iOS 获取当前点击的cell相对于self.view的位置
- (转)在CRichEditCtrl中如何获取中文字符的位置
- 获取鼠标点击的位置
- 请用charAt获取javascript获取字符串指定位置字符
- 在PV3D中获取鼠标点击位置的空间坐标信息
- iOS利用CoreLocation获取地理位置以及如何在模拟器进行调试
- 网页效果获取鼠标点击的位置
- 点击鼠标在textarea中光标当前位置插入指定字符
- 获取鼠标点击位置
- MFC 获取picture控件的鼠标点击坐标位置的方法
- 精确获取CRicheditctrl中字符的位置
- ANDROID GRIDVIEW 点击某个位置获取某个单元格