Get Current Visible Text in Textview

TextView - get Visible text, and how to divide string to pages?

It might make more sense to approach the problem from the other direction: You have a long String of text. Then you decide how much you can fit on a page, and break the String into an array of multiple substrings, one for each page. Then you can simply store a page index someplace, and use that as an index into the array of String that you made when you broke the entire text into substrings.

This assumes that there's an algorithm to find how much text you can fit in a page. Take a look at this question.

Is it possible to know the number of visible words? not full length?

I don't know WHY you need this, but here it goes:

int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);
int visibleWords = displayed.split(" ").length

Get the NSRange for the visible text after scroll in UITextView

I haven't tested this thoroughly but I believe the following should work. The APIs you need are documented in the UITextInput protocol, which UITextView adopts.

You first need to get the UITextPosition that corresponds to a given point inside the view. You'd then convert this value into a UTF-16 character offset. For example, here I print the visible text range (in terms of UTF-16 code units) of a textView every time the view is scrolled:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
let topLeft = CGPoint(x: textView.bounds.minX, y: textView.bounds.minY)
let bottomRight = CGPoint(x: textView.bounds.maxX, y: textView.bounds.maxY)
guard let topLeftTextPosition = textView.closestPosition(to: topLeft),
let bottomRightTextPosition = textView.closestPosition(to: bottomRight)
else {
return
}
let charOffset = textView.offset(from: textView.beginningOfDocument, to: topLeftTextPosition)
let length = textView.offset(from: topLeftTextPosition, to: bottomRightTextPosition)
let visibleRange = NSRange(location: charOffset, length: length)
print("Visible range: \(visibleRange)")
}

In my tests, UITextView tended to count lines that were barely included in the visible range (e.g. by only one pixel), so the reported visible range tended to be one or two lines larger than what a human user would say. You may have to experiment with the exact CGPoint you pass into closesPosition(to:) to get the results you want.



Related Topics



Leave a reply



Submit