我想出了如何在核心文本中制作一个可点击的 url,但我不知道如何让其他单词可以点击,例如 'my name is @george' 。我希望@george 在核心文本中可以点击。有办法吗?我正在尝试这样使其可点击:
CFAttributedStringSetAttribute(string, CFRangeMake( 0, mystring.length ), kCTFontAttributeName, ctFontBold );
CFAttributedStringSetAttribute(string, CFRangeMake( mystring.length, linestring.length ), kCTFontAttributeName, ctFont);
CFAttributedStringSetAttribute(string, CFRangeMake( mystring.length, linestring.length ), kCTParagraphStyleAttributeName, paragraphStyle);
CFAttributedStringSetAttribute(string, CFRangeMake(0, mystring.length), kCTForegroundColorAttributeName, [UIColor blackColor].CGColor);
CFAttributedStringSetAttribute(string, CFRangeMake(0, mystring.length),
(CFStringRef)@"CustomLink",mystring);
然后当我点击这个词时,我会像这样检索它:
NSString* myString = [attributes objectForKey:@"CustomLink"];
但我一直得到(空)。当它是 URL 时不会发生这种情况!
任何帮助表示赞赏!
谢谢。
我通过手势识别器检测到触摸:
CTFrameRef ctFrame = CTFramesetterCreateFrame( framesetter, CFRangeMake(0, text.length),path, NULL );
CFArrayRef lines = CTFrameGetLines(ctFrame);
CGPoint* lineOrigins = malloc(sizeof(CGPoint)*CFArrayGetCount(lines));
NSInteger index=0;
int ii=0;
for(CFIndex i = 0; i < CFArrayGetCount(lines); i++)
{
CGFloat y;
CTFrameGetLineOrigins( ctFrame, CFRangeMake(0, 0), lineOrigins);
CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(lines, i);
CGPoint origin = lineOrigins[i];
y = bottomLabel.bounds.origin.y + bottomLabel.bounds.size.height - origin.y;
ii=i;
if (reversePoint.y > origin.y) {
index = CTLineGetStringIndexForPosition(line, reversePoint);
CFArrayRef runs = CTLineGetGlyphRuns(line);
for(CFIndex j = 0; j < CFArrayGetCount(runs); j++)
{
CTRunRef run = CFArrayGetValueAtIndex(runs, j);
CGRect runBounds;
CGFloat ascent;//height above the baseline
CGFloat descent;//height below the baseline
runBounds.size.width = CTRunGetTypographicBounds(run, CFRangeMake(0, 0), &ascent, &descent, NULL);
runBounds.size.height = ascent + descent;
CGFloat xOffset = CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL);
runBounds.origin.x = origin.x + rect.origin.x + xOffset;
runBounds.origin.y = y;//+ rect.origin.y;
runBounds.origin.y -= (descent+ascent)-5;
NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run);
NSString* urlString = [attributes objectForKey:@"CustomLink"];
if(urlString && ![urlString isEqualToString:@""])
{
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showwebview:) userInfo:urlString repeats:NO];
return;
}
}
}