如何找到位置或获取rect的任何单词在textview和地方按钮在那? [英] How to find position or get rect of any word in textview and place buttons over that?

查看:160
本文介绍了如何找到位置或获取rect的任何单词在textview和地方按钮在那?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个故事应用程序。我们需要提供quizes。
现在我有一个故事,有一些空白或隐藏的话。
每当我点击那个隐藏的单词,我会得到4个选项来回答。
我试图通过放置按钮的话,但这将是只有当我使用一些静态
位置。
我只是想知道如何得到那个词的框架,我需要隐藏,这样
我可以放一些按钮,可以隐藏这个。



您可以看到下面的图片..


$ b code> - (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView
{
UITextPosition * beginning = textView.beginningOfDocument; //错误=:请求成员'beginOfDocument'在不是结构或联合的东西

UITextPosition * start = [textView positionFromPosition:beginning offset:range.location];
UITextPosition * end = [textView positionFromPosition:start offset:range.length];
UITextRange * textRange = [textView textRangeFromPosition:start toPosition:end];
CGRect rect = [textView firstRectForRange:textRange]; //错误:无效的初始化

return [textView convertRect:rect fromView:textView.textInputView]; //错误:在不是结构或联合的成员中请求成员textInputView



}

感谢



请参阅以下图片以了解错误:

解决方案

UITextView 采用 UITextInput 协议。该协议有方法 -firstRectForRange:,它会告诉你矩形覆盖一个字符范围。如果字符范围跨越多行,您必须重复调用它。第一次它会给你第一个rect,告诉你覆盖的范围。然后,您调整要求的范围,将其作为您感兴趣的其余字符并重复。


I am working on a story app.Where We need to provide quizes. Now I am having a story and there are some blanks or hidden words over that story. Whenever I click over that hidden word,I will get the 4 options to answer that. I tried by placing button over the words,But that would be only when I am using some static position. I just want to know how can I get the frame of that word,which I need to hide ,so that I can place some button over that and can hide this.

You can see the image below.. All answers would be appreciated

This will work after iOS 5 only not below that !

- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView
{
    UITextPosition *beginning = textView.beginningOfDocument; //Error=: request for member 'beginningOfDocument' in something not a structure or union

    UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
    UITextPosition *end = [textView positionFromPosition:start offset:range.length];
    UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
    CGRect rect = [textView firstRectForRange:textRange];  //Error: Invalid Intializer

    return [textView convertRect:rect fromView:textView.textInputView]; // Error: request for member 'textInputView' in something not a structure or union



}

Thanks

See one more image for errors:

解决方案

UITextView adopts the UITextInput protocol. That protocol has the method -firstRectForRange: which will tell you the rectangle covering a range of characters. If the range of characters spans multiple lines, you have to call it repeatedly. The first time it will give you the first rect and tell you what range that covers. Then, you adjust the range you're asking about to be the remaining characters you're interested in and repeat.

这篇关于如何找到位置或获取rect的任何单词在textview和地方按钮在那?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆