Iphone CoreText、Copy&;粘贴
我想在CoreText中使用UIMenuController,但我无法选择单词,我想我必须计算单词的坐标,但坐标不能与NSRange对应。是否有函数可以解决此问题 对不起,我的英语不好~ 这是我的密码Iphone CoreText、Copy&;粘贴,iphone,ios,copy,paste,core-text,Iphone,Ios,Copy,Paste,Core Text,我想在CoreText中使用UIMenuController,但我无法选择单词,我想我必须计算单词的坐标,但坐标不能与NSRange对应。是否有函数可以解决此问题 对不起,我的英语不好~ 这是我的密码 CFArrayRef lines = CTFrameGetLines(leftFrame); CFIndex i, total = CFArrayGetCount(lines); CGFloat y; for (i = 0; i < total; i++) { CGPoint o
CFArrayRef lines = CTFrameGetLines(leftFrame);
CFIndex i, total = CFArrayGetCount(lines);
CGFloat y;
for (i = 0; i < total; i++) {
CGPoint origins;
CTFrameGetLineOrigins( leftFrame, CFRangeMake(i, 1), &origins);
CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(lines, i);
y = self.bounds.origin.y + self.bounds.size.height - origins.y;
//CTLineDraw(line, UIGraphicsGetCurrentContext());
CFArrayRef runs = CTLineGetGlyphRuns(line);
CFIndex r, runsTotal = CFArrayGetCount(runs);
//NSLog(@"runsTotal = %d",runsTotal);
for (r = 0; r < runsTotal; r++) {
CGRect runBounds = CTRunGetImageBounds(CFArrayGetValueAtIndex(runs, r), context, CFRangeMake(0, 0));
NSLog(@"runBounds.x = %f,runBounds.y = %f",runBounds.origin.x,runBounds.origin.y);
CFIndex index = CTRunGetStringRange(CFArrayGetValueAtIndex(runs, r)).location;
//NSLog(@"%d",index);
}
}
CFArrayRef lines=CTFrameGetLines(leftFrame);
CFIndex i,total=CFArrayGetCount(行);
CGY;
对于(i=0;i
核心文本不支持文本选择,您需要自己选择,请按照示例进行操作您可以使用轻触手势获得轻触位置
还有CTFrame
然后可以计算所需的文本框
下面是一个例子,如何计算两个字符的点击位置帧 传入
CTFrame
和Tap Point
,在CTFrame
+(CGRect)parserRectWithPoint:(CGPoint)point range:(NSRange *)selectRange frameRef:(CTFrameRef)frameRef
{
CFIndex index = -1;
CGPathRef pathRef = CTFrameGetPath(frameRef);
CGRect bounds = CGPathGetBoundingBox(pathRef);
CGRect rect = CGRectZero;
// get lines
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frameRef);
if (!lines) {
return rect;
}
NSInteger lineCount = [lines count];
// prepare for origins of each line
CGPoint *origins = malloc(lineCount * sizeof(CGPoint));
if (lineCount) {
CTFrameGetLineOrigins(frameRef, CFRangeMake(0, 0), origins);
// check tap point in every line
// found, then break
for (int i = 0; i<lineCount; i++) {
CGPoint baselineOrigin = origins[i];
CTLineRef line = (__bridge CTLineRef)[lines objectAtIndex:i];
CGFloat ascent,descent,linegap; //声明字体的上行高度和下行高度和行距
CGFloat lineWidth = CTLineGetTypographicBounds(line, &ascent, &descent, &linegap);
CGRect lineFrame = CGRectMake(baselineOrigin.x, CGRectGetHeight(bounds)-baselineOrigin.y-ascent, lineWidth, ascent+descent+linegap+[LSYReadConfig shareInstance].lineSpace);
if (CGRectContainsPoint(lineFrame,point)){
// line found
CFRange stringRange = CTLineGetStringRange(line);
// glyph found
index = CTLineGetStringIndexForPosition(line, point);
CGFloat xStart = CTLineGetOffsetForStringIndex(line, index, NULL);
CGFloat xEnd;
// choose two words by default
if (index > stringRange.location+stringRange.length-2) {
xEnd = xStart;
xStart = CTLineGetOffsetForStringIndex(line,index-2,NULL);
(*selectRange).location = index-2;
}
else{
xEnd = CTLineGetOffsetForStringIndex(line,index+2,NULL);
(*selectRange).location = index;
}
// choose two character
(*selectRange).length = 2;
rect = CGRectMake(origins[i].x+xStart,baselineOrigin.y-descent,fabs(xStart-xEnd), ascent+descent);
break;
}
}
}
free(origins);
return rect;
}
+(CGRect)parserRectWithPoint:(CGPoint)点范围:(NSRange*)选择范围frameRef:(CTFrameRef)frameRef
{
CFIndex=1;
CGPathRef pathRef=CTFrameGetPath(frameRef);
CGRect bounds=CGPathGetBoundingBox(pathRef);
CGRect rect=CGRectZero;
//排队
NSArray*线条=(uu桥NSArray*)CTFrameGetLines(frameRef);
如果(!行){
返回矩形;
}
NSInteger lineCount=[行数];
//准备好每行的起点
CGPoint*origins=malloc(lineCount*sizeof(CGPoint));
if(行计数){
CTFrameGetLineOriginates(frameRef,CFRangeMake(0,0),Originates);
//检查每行中的分接点
//找到,然后打破
对于(int i=0;i stringRange.location+stringRange.length-2){
xEnd=xStart;
xStart=CTLineGetOffsetForStringIndex(行,索引-2,空);
(*选择范围)。位置=索引-2;
}
否则{
xEnd=CTLineGetOffsetForStringIndex(行,索引+2,空);
(*选择范围)。位置=索引;
}
//选择两个字符
(*选择范围)。长度=2;
rect=CGRectMake(原点[i].x+xStart,基线原点.y-下降,fabs(xStart-xEnd),上升+下降);
打破
}
}
}
自由(起源);
返回矩形;
}