2010-12-12 48 views
0

我从plist中拉出文本并将其绘制到UIView中。 该文本包含我希望突出显示的用户和用户交互的条款,以便我可以用teem解释弹出字典视图。添加TapRecognizer和不同的颜色到Xcode中的字符串中的单词

我知道如何找到他们在字符串上的位置,并且工作正常。 但我没有找到如何让他们出现在不同的颜色。 对我来说更重要 - 如何为他们添加水龙头识别器。

我将不胜感激任何帮助。

由于 沙尼

回答

0

另外 我发现了一个对我很好的解决方案。

我正在使用UIWebView作为文本的视图,然后添加格式为HTML的文本。 以这种方式,我可以使用CSS来更好地设置文本的格式,甚至可以将一些文本标记为链接,在示例中,我希望辅音字将显示为链接。 (另一个很大的好处是我可以添加对RTL的支持)。

UIWebView *webView =[[UIWebView alloc] initWithFrame:imgViewRect]; 
      webView.backgroundColor = [UIColor clearColor]; 
      webView.opaque=NO; 
      webView.delegate=self; 
      NSString *localizedPath = [NSString stringWithFormat:@"rt%@",pcard.card_number]; 
      NSString *localizedString = NSLocalizedString(localizedPath,@""); 
      NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"style" ofType:@"css"]; 

      //do base url for css 
      NSString *path = [[NSBundle mainBundle] bundlePath]; 
      NSURL *baseURL = [NSURL fileURLWithPath:path]; 

      NSString *html =[NSString stringWithFormat:@"<a href=\"consonant\"><dfn>consonant</dfn></a>", 
          currentLanguage,dir,cssPath,langClass,localizedString]; 
      NSLog(@"%@",html); 
      [webView loadHTMLString:html baseURL:baseURL]; 

      [self addSubview:webView]; 

然后我用这个功能来识别哪些词的点击和午餐字典文字的UIView:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request 
navigationType:(UIWebViewNavigationType)navigationType { 

    if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
     NSURL *URL = [request URL]; 
     NSLog(@"%@",[URL lastPathComponent]); //thats gives me the name of the clicked word. 


     //here you can set any function that you like. 
      //using the data from the url path. 

    } 
    return YES; 
} 

希望这会帮助别人。 shani

0

添加一个透明按钮具有在其中单词出现在前面的UIView相同的字体和文本。如果你愿意,我有一个班级可以用UILabel来完成这个任务吗?它创造了与Facebook应用程序相同的效果。

+0

嗨,谢谢你的回复。是不是有影响文本本身?无论如何,我希望看到你的班级可以为我节省一些工作时间。再次感谢 – shannoga 2010-12-15 07:35:42

+0

肯定......你需要使用属性字符串(你必须下到核心文本级别才能实现)。我会找到我的班级的链接...它不是我个人的,我只是使用它。 :) – 2010-12-16 12:16:16

+0

http://furbo.org/2008/10/07/fancy-uilabels/ – 2010-12-16 12:18:13

相关问题