Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Get iPad cursor position.
-
When i use
ui
library, is there a way to know these things:- where is the cursor with x, y ?
- is the cursor on top of a view ?
Is these things possible with
ui
? Or at least is there a library in pythonista can do one of these things ? -
@Raymond if by cursor, you mean your finger, try
import ui class MyView(ui.View): def __init__(self, *args, **kwargs): super().__init__(self, *args, **kwargs) self.background_color = 'white' def touch_moved(self,touch): x,y = touch.location self.name = f'x={x}, y={y}' v = MyView() v.present('fullscreen')
Édit: I just see that I had answered to a similar question in August 2022 here, thus I guess that you don't want the finger position but the x,y of a cursor in a TextView?
Thus, please check this small script and extract part you need, if I have correctly understood
import ui from objc_util import * tv = ui.TextView() tv.font = ('<System-Bold>',32) tv.text = 'this is a sample but could be longer' tv.frame = (0,0,200,200) tv.present('sheet') tvo = ObjCInstance(tv) #print(dir(tvo)) txt = 'ampl' i = tv.text.find(txt) p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), i) p2 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), i+len(txt)) rge = tvo.textRangeFromPosition_toPosition_(p1,p2) rect = tvo.firstRectForRange_(rge) # CGRect x,y = rect.origin.x,rect.origin.y w,h = rect.size.width,rect.size.height #print(x,y,w,h) l = ui.Button() l.frame = (x,y,w,h) l.background_color = (1,0,0,0.5) l.corner_radius = 10 l.border_width = 1 def button_action(sender): if l.background_color == (1,0,0,0.5): sender.background_color = (0,0,1,0.5) else: sender.background_color = (1,0,0,0.5) l.action = button_action tv.add_subview(l)
If you want to play with cursor, insert line
tv.selected_range = (i,i+len(txt))
after i = ...
-
@cvp
No, I mean position of mouse cursor. Not touch position. -