0

我正在从 plist 中提取文本并将其绘制到 UIView 中。文本包含我想要突出显示并启用用户交互的术语,因此我可以弹出一个带有 teem 解释的字典视图。

我知道如何在字符串上找到它们的位置,而且效果很好。但我没有找到如何让它们以不同的颜色出现。对我来说更重要的是——如何为它们添加点击识别器。

我将不胜感激任何帮助。

谢谢沙妮

4

2 回答 2

0

在 UIView 中出现单词的位置添加一个具有相同字体和文本的透明按钮。如果你愿意,我有一门课可以用 UILabel 做到这一点?它创建与 Facebook 应用程序相同的效果。

于 2010-12-14T14:24:57.687 回答
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;
}

希望它会帮助某人。沙尼

于 2010-12-24T12:24:58.917 回答