2012-12-15 49 views
1

我想出瞭如何在覈心文本中製作一個可點選的網址,但我無法弄清楚如何讓其他單詞可以像「我的名字是@george」一樣可點擊。我希望@george在覈心文本中可點擊。有沒有辦法?我想這樣使它可點擊:nsattributedstring tappable iOS

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; 
       } 
} 
} 
+0

你的代碼在哪裏可以讓URL可以被點擊?請發佈它! – Anton

+0

我已更新使網址可點擊的代碼 – stefanosn

回答