I came up with a solution that, admittedly, is a bit of a hack. It estimates the start and end index of the visible text using the content offset, content height and content frame height of the textView. The answer is only an estimate because word wraps are unpredictable. The result is usually ±10 characters of the actual visible text. You could compensate for this by adding/subtracting a buffer of several characters to the start/end offsets, which would ensure that you have a substring of your textView text which definitely contains the visible text, with only a few extra characters at the beginning and end.
I hope this answer helps, or at least inspires you (or someone else) to come up with a solution that solves your exact needs.
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var textView: UITextView!
let textViewText = "Here's to the crazy ones. The misfits. The rebels. The trouble-makers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules, and they have no respect for the status-quo. You can quote them, disagree with them, glorify, or vilify them. But the only thing you can't do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do."
override func viewDidLoad() {
super.viewDidLoad()
textView.text = textViewText
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let textViewContentHeight = Double(textView.contentSize.height)
let textViewFrameHeight = Double(textView.frame.size.height)
let textViewContentOffset = Double(textView.contentOffset.y)
let textViewCharacterCount = textViewText.characters.count
let startOffset = Int((textViewContentOffset / textViewContentHeight) * Double(textViewCharacterCount))
// If the user scrolls quickly to the bottom so that the text is completely off the screen, we don't want to proceed
if startOffset < textViewCharacterCount {
let endIndex = Int(((textViewContentOffset + textViewFrameHeight) / textViewContentHeight) * Double(textViewCharacterCount))
var endOffset = endIndex - textViewCharacterCount
if endIndex > textViewCharacterCount {
endOffset = 0
}
let visibleString = textViewText.substringWithRange(textViewText.startIndex.advancedBy(startOffset)..<textViewText.endIndex.advancedBy(endOffset))
print(visibleString)
}
}
}