UITextView did finish change selection
Asked Answered
A

4

5

I want to catch event when user finish change selection in UITextView. Method textViewDidChangeSelection: call a few times while user pulls cursor. I try to use touchesEnded:withEvent:, but it doesn't call. Try to add pan gesture, but pan handler don't call.

Aquiculture answered 4/2, 2014 at 19:20 Comment(7)
Please explain why you can't use textViewDidChangeSelection:.Robin
Every time when selection changed I send request to server. I get a lot of requests in this case. I can send request with delay, it's plan B =)Aquiculture
It may be a good idea to do after waiting for some time after the last textViewDidChangeSelection: call.Exurbanite
Yes, but I think it will be much much better if I will do it without delay. Response come within 0.1 second, it really fast. With delay users will be waiting ten times more that needs. And I have limit for requests per day. Anyway much better do without delay.Aquiculture
@Aquiculture Did you find a solution?Warila
@Warila I just use timer. If user change selection, invalidate previous timer and start new one. This works fine for my app.Aquiculture
@Warila is right answer not working for you? Id don't remember if I check it.Aquiculture
F
4

UITextInputDelegate's selectionDidChange(_:) is called when the user has finished changing the selection (lifts their finger from the selection handles). On the UITextView, set your class as the inputDelegate. This works for read-only text views, not just editable ones.

Fotheringhay answered 21/2, 2017 at 17:36 Comment(2)
I am using selectionDidChange with a UITextView and it seems to get called everytime i highlight an extra character rather than at the end of a block of text. So how is it different to textViewDidChangeSelection?Brundage
This actually not work, even on the non-editable UITextViewDolores
A
4

I know it's too late to answer. Though.. You can use selectedRange inside textViewDidChangeSelection: and check if selectedRange.length is greater than zero. This will confirm that user has selected something and not just moved the cursor.

Ambush answered 8/11, 2016 at 6:31 Comment(4)
Never too late to answer! StackOverflow is meant to be an archive reference for future searches as well, thank you!Mildamilde
@AlbertRenshaw exactly bro, the same happened to me.Ambush
I later could find the solution and posted it.Ambush
You missunderstand me. I want to handle when user finish select range. Every time when user move start range cursor event is called. Every time length > 0. I need somehow handle when user touch up his finger.Aquiculture
F
4

UITextInputDelegate's selectionDidChange(_:) is called when the user has finished changing the selection (lifts their finger from the selection handles). On the UITextView, set your class as the inputDelegate. This works for read-only text views, not just editable ones.

Fotheringhay answered 21/2, 2017 at 17:36 Comment(2)
I am using selectionDidChange with a UITextView and it seems to get called everytime i highlight an extra character rather than at the end of a block of text. So how is it different to textViewDidChangeSelection?Brundage
This actually not work, even on the non-editable UITextViewDolores
E
0

I think you'll have to subclass UITextView and implement touchesEnded:withEvent: in order to know when the user stops changing the selection.

Exurbanite answered 4/2, 2014 at 19:23 Comment(0)
D
0

It's a little bit of hack.

I've observed that after user lift his finger, system will call delegate querying about menu options, such as:

optional func textView(_ textView: UITextView, editMenuForTextIn range: NSRange, suggestedActions: [UIMenuElement]) -> UIMenu?

We can make good use of it, right?

Notice it only avaliable above iOS16.0

Dolores answered 20/6 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.