2011-05-20 69 views

回答

4

我會這樣做的方式是計算每個段落的所有大小。使用sizeWithFont:constrainedToSize:lineBreakMode:

然後,您將能夠從[textView contentOffset]中計算哪些段落可見。

要滾動,不要使用scrollRangeToVisible,只需使用setContentOffset:CGPoint y參數應該是所有高度尺寸與下一個段落的總和,或者只是添加textView.frame.size.height,如果比下一段的開頭更接近。

這有意義嗎?

在回答評論requst代碼波紋管(未經測試):

CGFloat paragraphOffset[MAX_PARAGRAPHS]; 

    CGSize constraint = CGSizeMake(widthOfTextView, 999999 /*arbitrarily large number*/); 
    NSInteger paragraphNo = 0; 
    CGFloat offset = 0; 

    for (NSString* paragraph in paragraphs) { 
     paragraphOffset[paragraphNo++] = offset; 
     CGSize paragraphSize = [paragraph sizeWithFont:textView.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 
     offset += paragraphSize.height; 
    } 

    // find visible paragraph 
    NSInteger visibleParagraph = 0; 
    while (paragraphOffset[visibleParagraph++] < textView.contentOffset.y); 


    // scroll to paragraph 6 
    [textView setContentOffset:CGPointMake(0, paragraphOffset[6]) animated:YES]; 
+0

你能舉一些例子代碼嗎?我不知道如何sizeWithFont:constrainedToSize:lineBreakMode:工作,什麼是constrainedToSize:參數? – 2011-05-24 11:56:00

+0

上面添加了代碼。 FYI段落是段落字符串的一個NSArray – 2011-05-25 11:42:10

+2

請注意,您可以用FLT_MAX替換9999999(默認iPhone變量爲一個較大的值) – Niko 2011-12-14 22:11:48

1

一種選擇是,使用UIWebView而不是UITextView。然後,您可以使用錨點和JavaScript滾動到文本中的相應位置。您可以在每個段落的開頭以編程方式插入錨點,以使這更容易。

+0

我不熟悉javascript和複雜的html的東西,你能給我舉個例子嗎?此外,UIWebView可能比UITextView更難處理 – 2011-05-24 11:59:11

+0

@ xlc0212:如果您不需要編輯,使用UIWebview比每次需要滾動時計算UITextView的文本大小要容易得多(可能更快)。只要看看'html錨'是什麼 - 這並不複雜。 – roop 2011-06-17 06:33:24

7

我在這裏找到另一種解決方案。 這是在我眼中解決這個問題的更好方法。 https://stackoverflow.com/a/9283311/889892

由於UITextView是UIScrollView的子類,其邊界屬性反映了其座標系的可見部分。所以像這樣的東西應該工作:

-(NSRange)visibleRangeOfTextView:(UITextView *)textView { 
    CGRect bounds = textView.bounds; 
    UITextPosition *start = [textView characterRangeAtPoint:bounds.origin].start; 
    UITextPosition *end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))].end; 
    return NSMakeRange([textView offsetFromPosition:textView.beginningOfDocument toPosition:start], 
     [textView offsetFromPosition:start toPosition:end]); 
} 

這假設從上到下,從左到右的文本佈局。如果你想使它適用於其他佈局方向,你將不得不加倍努力。 :)

+0

當您說「這樣的事情應該起作用」時,您真的嘗試過了嗎?這幾乎是我實際可視文本範圍的三倍。 謝謝。 – 2014-08-01 15:21:14

2

如果你想迅速解決我用這個:

public extension UITextView { 

    public var visibleRange: NSRange? { 
     if let start = closestPositionToPoint(contentOffset) { 
      if let end = characterRangeAtPoint(CGPointMake(contentOffset.x + CGRectGetMaxX(bounds), contentOffset.y + CGRectGetMaxY(bounds)))?.end { 
       return NSMakeRange(offsetFromPosition(beginningOfDocument, toPosition: start), offsetFromPosition(start, toPosition: end)) 
      } 
     } 
     return nil 
    } 
} 
0

雨燕3.0/3.1解決方案基於 「麪條死亡的」 的答案。

public extension UITextView { 

    public var visibleRange: NSRange? { 
     if let start = closestPosition(to: contentOffset) { 
      if let end = characterRange(at: CGPoint(x: contentOffset.x + bounds.maxX, y: contentOffset.y + bounds.maxY))?.end { 
       return NSMakeRange(offset(from: beginningOfDocument, to: start), offset(from: start, to: end)) 
      } 
     } 
     return nil 
    } 
} 
相關問題