firstRectForRange无法正常工作

firstRectForRange无法正常工作

本文介绍了当混合中有新行字符时,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:

这样不太好:

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无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:07