在 Xcode 中的字符串内向单词添加 TapRecognizer 和不同的颜色

发布于 2024-10-07 08:45:23 字数 199 浏览 0 评论 0原文

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

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

我将不胜感激任何帮助。

谢谢 沙尼

I am pulling a text from a plist and draw it into a UIView.
The text contains terms that I want to be highlighted and user interaction enabled so I could pop a dictionary view with the teem explanation.

I know how to find their location on the string and that works fine.
But I did not find how to make them a appear in different color.
And even more important to me - how to add a tap recognizer to them.

I would appreciate any help.

Thanks
Shani

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风疏影 2024-10-14 08:45:23

在 UIView 中出现的单词前面添加一个具有相同字体和文本的透明按钮。我有一个类可以使用 UILabel 来完成此操作,如果您愿意的话?它产生与 Facebook 应用程序相同的效果。

Add a transparent button with the same font and text in front of where the word appears in the UIView. I have a class that will do this with a UILabel if you'd like it? It creates the same effect as with the Facebook application.

九厘米的零° 2024-10-14 08:45:23

好的
我找到了一个非常适合我的解决方案。

我使用 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;
}

希望它能帮助别人。
沙尼

O.k
i found a solution that works great for me.

i am using UIWebView as The view for the text and then adds the text formatted as HTML.
in this way i can use CSS to format the text better and even tag some of the text as link ,in the example i want that the word consonant will appear as a link.
(an other great benefit is that I can add support to RTL languishes).

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];

then i use this function to recognize what word clicks and lunch a UIView with the dictionary text:

- (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;
}

hope it will help someone.
shani

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文