2013-12-11 38 views
0

我想使Facebook的標題UILabel。
我想要做的事情是在UILabel或UITextView中多次觸摸。我應該使用什麼?
我該如何製作這樣的http://tool.keepitreal.jp/screen.png
在這種情況下,如果您觸摸,每個名稱將轉到不同的頁面。我怎麼能把多個觸摸像UILabel/UITextView的Facebook標題?

NSString* parts = @"this is test. you know?"; 

    NSError *error = nil; 
    NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"test" options:0 error:&error]; 
    if (error == nil) { 
     NSArray *res = [regexp matchesInString:parts.string options:0 range:NSMakeRange(0, parts.length)]; 
     for (NSTextCheckingResult *match in res) { 
      // change color 
      [parts addAttribute:NSForegroundColorAttributeName 
          value:[UIColor redColor] 
          range:[match rangeAtIndex:0]]; 
     } 
    } 
+0

使用[NSMutableAttributedString](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html)。 –

+0

我試過NSMutableAttributedString。我可以更改字體重量,但是我怎樣才能在每個名字上設置事件。 – naohide

+0

@naofide:顯示你試過的東西?你有使用'addAttribute'屬性嗎? –

回答

1

只需設置雙擊手勢上的UITextView和事件處理方法:

CGPoint location = [gestureRecognizer locationInView:theTextView]; 
UITextPosition *tapPos = [self.textViewBelowPopup closestPositionToPoint:location]; 
UITextRange *wr = [self.textViewBelowPopup.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight]; 
NSString *word = [self.textViewBelowPopup textInRange:wr]; 

你有話進行比較,並顯示其相應的屏幕/頁。

+0

謝謝。我試過這個ñ我得到的話。但是,我有很多話。我想在更短的範圍內得到這個詞。我可以嗎? – naohide

+0

對不起,我可以通過withGranularity:UITextGranularityWord來做到這一點。我使用了UITextGranularitySentence。非常感謝! – naohide

0

時尚秀,

它不可能在不同的位置上的一個控制分配兩個敲擊手勢。您可以使用兩個控件,一個是普通文本,另一個是屬性文本,然後分配輕擊手勢。

有一個幸福的一天提前