当混合中有新行字符时,UITextView firstRectForRange无法正常工作 [英] UITextView firstRectForRange not working when there's new line characters in the mix

查看:398
本文介绍了当混合中有新行字符时,UITextView firstRectForRange无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此方法将NSRange转换为CGRect,因为它与UITextView相关:

I'm using this method for converting a NSRange to a CGRect as it relates to a UITextView:

- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView
{
    UITextPosition *beginning = textView.beginningOfDocument;
    UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
    UITextPosition *end = [textView positionFromPosition:beginning offset:range.location+range.length];
    UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
    CGRect rect = [textView firstRectForRange:textRange];
    return rect;
}

这个问题的回答让有人问我同样的问题。上述方法适用于此:

This question's answer has someone asking the same question as me. The above method works great for this:


这是一个很棒的文本字段是的,它是findthistoken

this is a great text field yes it is findthistoken

这样不太好:

this is a great one
(new line)
(new line)
findthistoken

似乎简单地忽略了有2个新行字符的事实? :/

It seems to simply ignore the fact that there are 2 new lines characters? :/

如何使用新行字符?

推荐答案

您可以强制self.textView立即确保textView的布局

You can force self.textView to ensure the layout of textView immediately with

 [self.textView.layoutManager ensureLayoutForTextContainer:self.textView.textContainer];

在你的frameOfTextRange:call之前放置它会给你正确的矩形。

Placing that before your frameOfTextRange: call will gives you right rect.

这篇关于当混合中有新行字符时,UITextView firstRectForRange无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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