【问题标题】:How to create invisible uibuttons on top of specific words in a UITextView?如何在 UITextView 中的特定单词之上创建不可见的 uibuttons?
【发布时间】:2012-05-10 08:27:40
【问题描述】:

我正在尝试设计一个向用户显示一些说明的应用。我正在通过 UITextView 显示这些说明,现在我正在尝试添加一个功能,用户可以在其中单击特定单词并在另一个视图中查看它的定义。

具体的词都将来自一个 sqlite 数据库。 所以我的问题是:实现这一目标的最佳方法是什么?

我正在考虑将不可见的 UIButtons 覆盖在单词之上,但我不知道如何将它们放置在 UITextView 内的单词之上。

【问题讨论】:

    标签: ios cocoa-touch uibutton uitextview hyperlink


    【解决方案1】:

    实现所需的最简单方法是使用 UIWebView 而不是 UITextView。您可以将指令文本转换成这样的 HTML 文件:

    <html>
       <head></head>
       <body>
          <div>This is a dummy text with a <a href="definition://id1234">clickable keyword</a> inside the text</div>
       </body>
    </html>
    

    当您将可点击关键字放入 标签时,它们变为可点击,您可以在 UIWebView 的委托方法中拦截此点击。

    所以将你的 HTML 文本加载到 UIWebView 中:

    UIWebView *webView = [[UIWebView alloc] init];
    webView.delegate = self;
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yourdomain.com/text.html"]]];
    

    当您为您的 webview 分配一个委托时(如上面的代码 sn-p 中),每次 UIWebView 发出请求时都会调用以下方法。在这里,您可以拦截对关键字的点击。我为关键字 HTML 链接提供了他们自己的方案(“definition://”),以使拦截更容易和更可靠。 (您以后可以在 HTML 中添加其他链接。)

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
       if ([[request.URL scheme] isEqualToString:@"definition"]) {
          NSString *definitionWordID = [request.URL host];
          NSLog(@"show definition for word with ID: %@", definitionWordID);
          return NO;
       }
       return YES;
    }
    

    这就是我将如何实现您想要实现的目标。使用 HTML 具有很大的优势,您不必担心文本中的关键字位置,因为您使用的是关键字的 HTML 链接。除此之外,使用 HTML 还让您有机会通过 CSS 设置文本样式。

    【讨论】:

    • 这实际上是我的问题的一个非常优雅的解决方案!我想我会朝这个方向前进,因为它似乎比在 UITextView 中找到特定单词的框架/边界要少得多。谢谢!
    【解决方案2】:

    我不确定这是不是最好的方法,但我会按照你的要求回答这个问题:

    // This assumes you have already determined the frame for the button
    // i.e., you know the origin and size of the word in the UITextView
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn setFrame:wordFrame];
    [textView addSubview:btn];
    // Now, you need to also figure out how to handle the user pressing the button
    [btn addTarget:self action:@selector(wordButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
    

    现在,对于不同的建议,您可能想要创建自己的 UIView 子类,自己执行自定义绘图(这还将实现跟踪单词位置的逻辑)然后实现触摸事件处理,交叉检查位置触摸您的单词位置列表。我不确定哪个会更好,但如果您要添加大量 UIButton,您可能会相对较快地遇到性能问题。

    要在自定义 UIView 子类中处理触摸事件,请查看以下方法:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
    

    【讨论】:

    • 我实际上不知道如何确定 UITextView 中特定单词的框架/边界...但是一旦我发现我们的解决方案对我有很大帮助!还是谢谢你!
    猜你喜欢
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多