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.
pythonista UI
-
@shinya.ta if you want that the script reads your text, try
import speech b_speech = ui.Button() b_speech.frame = (10,250,100,32) b_speech.title = 'speech' b_speech.background_color = 'white' b_speech.border_width = 1 def b_speech_action(sender): tv = sender.superview['TextField'] #print(speech.get_synthesis_languages()) speech.say(tv.text,'en-US') #speech.say(tv.text,'ja-JP') b_speech.action = b_speech_action v.add_subview(b_speech)```
-
-
@shinya.ta French butEnglish is good
-
@shinya.ta if you want it reads one character at the time
for c in tv.text: speech.say(c,'en-US') # or other language
But that is not VoiceOver but the speech module of Pythonista
-
Dans le programme précédent, un message d'erreur était émis.
-
-
Dans les applications précédentes ne comportant que des chiffres, ils lisaient le champ de texte un caractère à la fois.
-
I need the glove button on the iOS keyboard first.
-
This program reads out.
I don't know the difference between this and the last application.
import ui
from objc_util import *v = ui.View()
v.frame = (0,0,500,300)tf = ui.TextField()
tf.name = 'TextField'
tf.frame = (10,250,480,32)
tf.text = 'this is the sentence'
v.add_subview(tf)b_left = ui.Button()
b_left.frame = (10,10,100,32)
b_left.title = 'left'
b_left.background_color = 'white'
b_left.border_width = 1
def b_left_action(sender):
tf = sender.superview['TextField']
tfo = ObjCInstance(tf).textField()
begin = tfo.beginningOfDocument()
tfo.selectedTextRange = tfo.textRangeFromPosition_toPosition_(begin,begin)
b_left.action = b_left_action
v.add_subview(b_left)b_right = ui.Button()
b_right.frame = (10,50,100,32)
b_right.title = 'right'
b_right.background_color = 'white'
b_right.border_width = 1
def b_right_action(sender):
tf = sender.superview['TextField']
tfo = ObjCInstance(tf).textField()
begin = tfo.beginningOfDocument()
l = len(tf.text)
end = tfo.positionFromPosition_offset_(begin, l)
tfo.selectedTextRange = tfo.textRangeFromPosition_toPosition_(end,end)
b_right.action = b_right_action
v.add_subview(b_right)v.present('sheet')
-
@shinya.ta Sorry but I don't know VoiceOver ad I can't help you in this matter, but it seems normal that if the text only contains digits it reads one character or digit at a time
-
Let's think about improving the above code and moving the cursor left and right one word at a time.
-
@shinya.ta try
b_left_word = ui.Button() b_left_word.frame = (10,90,100,32) b_left_word.title = 'left word' b_left_word.background_color = 'white' b_left_word.border_width = 1 def b_left_word_action(sender): tf = sender.superview['TextField'] tfo = ObjCInstance(tf).textField() i = tfo.offsetFromPosition_toPosition_(tfo.beginningOfDocument(), tfo.selectedTextRange().start()) i = i - 1 blank = False while i >= 0: if tf.text[i] == ' ': if blank: # second blank befor cursor i = i + 1 # index to previous word break else: blank = True # first blank before cursor i = i - 1 cursor_position = tfo.positionFromPosition_offset_(tfo.beginningOfDocument(), i) tfo.selectedTextRange = tfo.textRangeFromPosition_toPosition_(cursor_position, cursor_position) b_left_word.action = b_left_word_action v.add_subview(b_left_word) b_right_word = ui.Button() b_right_word.frame = (10,130,100,32) b_right_word.title = 'right word' b_right_word.background_color = 'white' b_right_word.border_width = 1 def b_right_word_action(sender): tf = sender.superview['TextField'] tfo = ObjCInstance(tf).textField() i = tfo.offsetFromPosition_toPosition_(tfo.beginningOfDocument(), tfo.selectedTextRange().start()) i = i + 1 while i < len(tf.text): if tf.text[i] == ' ': if i < len(tf.text): i = i + 1 break i = i + 1 cursor_position = tfo.positionFromPosition_offset_(tfo.beginningOfDocument(), i) tfo.selectedTextRange = tfo.textRangeFromPosition_toPosition_(cursor_position, cursor_position) b_right_word.action = b_right_word_action v.add_subview(b_right_word)```
-
Dear.@cvp
Good morning. This is 7:38 minutes in the morning, so I'd like to try it later.
Thank you.
-
Dear.@cvp
When I tried this, VoiceOver got the sound properly.
I found out the cause.The VoiceOver does not read Aloud on the existing keyboard of iPhone.
When I used an external keyboard, I heard a sound from VoiceOver.
The following code is fine.
It is completed if you can connect with an existing keyboard of iPhone.
import ui
from objc_util import *v = ui.View()
v.frame = (0,0,500,320)
v.name = 'Move cursor in TextView'tv = ui.TextView()
tv.name = 'TextView'
tv.frame = (120,10,370,300)
tv.font = ('Arial Rounded MT Bold',24)
tv.text = 'this is the sentence'
v.add_subview(tv)b_top = ui.Button()
b_top.frame = (10,10,100,32)
b_top.title = 'begin'
b_top.background_color = 'white'
b_top.border_width = 1
def b_top_action(sender):
tv = sender.superview['TextView']
tv.selected_range = (0,0)
b_top.action = b_top_action
v.add_subview(b_top)b_left = ui.Button()
b_left.frame = (10,50,100,32)
b_left.title = 'left'
b_left.background_color = 'white'
b_left.border_width = 1
def b_left_action(sender):
tv = sender.superview['TextView']
i = tv.selected_range[0] - 1
if i < 0:
i = 0
tv.selected_range = (i,i)
b_left.action = b_left_action
v.add_subview(b_left)b_right = ui.Button()
b_right.frame = (10,90,100,32)
b_right.title = 'right'
b_right.background_color = 'white'
b_right.border_width = 1
def b_right_action(sender):
tv = sender.superview['TextView']
i = tv.selected_range[0] + 1
l = len(tv.text)
if i > l:
i = l
#tv.selected_range = (i,i) # refused if i = len(tv.text)
tvo = ObjCInstance(tv)
p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), i)
p2 = p1
tvo.selectedTextRange = tvo.textRangeFromPosition_toPosition_(p1,p2)
b_right.action = b_right_action
v.add_subview(b_right)b_bottom = ui.Button()
b_bottom.frame = (10,130,100,32)
b_bottom.title = 'end'
b_bottom.background_color = 'white'
b_bottom.border_width = 1
def b_bottom_action(sender):
tv = sender.superview['TextView']
l = len(tv.text)
#tv.selected_range = (l,l) # refused if l = len(tv.text)
tvo = ObjCInstance(tv)
p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), l)
p2 = p1
tvo.selectedTextRange = tvo.textRangeFromPosition_toPosition_(p1,p2)
b_bottom.action = b_bottom_action
v.add_subview(b_bottom)def get_xy(tv):
tvo = ObjCInstance(tv)
x_y = []
for i in range(0,len(tv.text)+1): # x,y of each character
p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), i)
rge = tvo.textRangeFromPosition_toPosition_(p1,p1)
rect = tvo.firstRectForRange_(rge) # CGRect
x,y = rect.origin.x,rect.origin.y
if i == len(tv.text):
if i > 0:
x,y = x_y[i-1]
else:
# text is empty
x,y = 0,0
x_y.append((x,y))
return x_yb_up = ui.Button()
b_up.frame = (10,170,100,32)
b_up.title = 'up'
b_up.background_color = 'white'
b_up.border_width = 1
def b_up_action(sender):
tv = sender.superview['TextView']
x_y = get_xy(tv)
c = tv.selected_range[0]
xc,yc = x_y[c]
i = c - 1
while i >= 0:
x,y = x_y[i]
if y < yc:
# previous row
if x <= xc:
tv.selected_range = (i,i)
return
i = i - 1
b_up.action = b_up_action
v.add_subview(b_up)b_clear = ui.Button()
b_clear.frame = (10,290,100,32)
b_clear.title = 'clear'
b_clear.background_color = 'white'
b_clear.border_width = 1
def b_clear_action(sender):
tv = sender.superview['TextView']
tv.text = ''
b_clear.action = b_clear_action
v.add_subview(b_clear)b_del = ui.Button()
b_del.frame = (10,250,100,32)
b_del.title = 'del'
b_del.background_color = 'white'
b_del.border_width = 1
def b_del_action(sender):
tv = sender.superview['TextView']
i = tv.selected_range[0]
if i < len(tv.text):
tv.text = tv.text[:i] + tv.text[i+1:]
#tv.selected_range = (i,i) # refused if i = len(tv.text)
tvo = ObjCInstance(tv)
p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), i)
p2 = p1
tvo.selectedTextRange = tvo.textRangeFromPosition_toPosition_(p1,p2)
b_del.action = b_del_action
v.add_subview(b_del)b_down = ui.Button()
b_down.frame = (10,210,100,32)
b_down.title = 'down'
b_down.background_color = 'white'
b_down.border_width = 1
def b_down_action(sender):
tv = sender.superview['TextView']
print(tv.selected_range,len(tv.text))
x_y = get_xy(tv)
c = tv.selected_range[0]
#print(x_y,c)
xc,yc = x_y[c]
i = c - 1
while i < len(tv.text):
x,y = x_y[i]
if y > yc:
# next row
if x >= xc:
tv.selected_range = (i,i)
return
else:
if (i+1) < len(tv.text):
if x_y[i+1][1] > y: # i = last character of row under cursor
tv.selected_range = (i,i)
return
else:
pass # try next x
else:
# last character of last row
tv.selected_range = (i,i)
return
i = i + 1
b_down.action = b_down_action
v.add_subview(b_down)v.present('sheet')
tv.selected_range = (0,0)
tv.begin_editing() -
@shinya.ta Happy for you
For the future, please, try to insert your code between two lines of ```
Only to keep the indentation and have a good visibility.
Or use the button above with </> and insert your code between the two lines of 3 back quotes -
Sorry, I don't understand the meaning below.
ombine the upper button with </> and insert the code between two lines of the three lines.
-
-
import ui
from objc_util import *v = ui.View()
v.frame = (0,0,500,320)
v.name = 'Move cursor in TextView'tv = ui.TextView()
tv.name = 'TextView'
tv.frame = (120,10,370,300)
tv.font = ('Arial Rounded MT Bold',24)
tv.text = 'this is the sentence'
v.add_subview(tv)b_top = ui.Button()
b_top.frame = (10,10,100,32)
b_top.title = 'begin'
b_top.background_color = 'white'
b_top.border_width = 1
def b_top_action(sender):
tv = sender.superview['TextView']
tv.selected_range = (0,0)
b_top.action = b_top_action
v.add_subview(b_top)b_left = ui.Button()
b_left.frame = (10,50,100,32)
b_left.title = 'left'
b_left.background_color = 'white'
b_left.border_width = 1
def b_left_action(sender):
tv = sender.superview['TextView']
i = tv.selected_range[0] - 1
if i < 0:
i = 0
tv.selected_range = (i,i)
b_left.action = b_left_action
v.add_subview(b_left)b_right = ui.Button()
b_right.frame = (10,90,100,32)
b_right.title = 'right'
b_right.background_color = 'white'
b_right.border_width = 1
def b_right_action(sender):
tv = sender.superview['TextView']
i = tv.selected_range[0] + 1
l = len(tv.text)
if i > l:
i = l
#tv.selected_range = (i,i) # refused if i = len(tv.text)
tvo = ObjCInstance(tv)
p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), i)
p2 = p1
tvo.selectedTextRange = tvo.textRangeFromPosition_toPosition_(p1,p2)
b_right.action = b_right_action
v.add_subview(b_right)b_bottom = ui.Button()
b_bottom.frame = (10,130,100,32)
b_bottom.title = 'end'
b_bottom.background_color = 'white'
b_bottom.border_width = 1
def b_bottom_action(sender):
tv = sender.superview['TextView']
l = len(tv.text)
#tv.selected_range = (l,l) # refused if l = len(tv.text)
tvo = ObjCInstance(tv)
p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), l)
p2 = p1
tvo.selectedTextRange = tvo.textRangeFromPosition_toPosition_(p1,p2)
b_bottom.action = b_bottom_action
v.add_subview(b_bottom)def get_xy(tv):
tvo = ObjCInstance(tv)
x_y = []
for i in range(0,len(tv.text)+1): # x,y of each character
p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), i)
rge = tvo.textRangeFromPosition_toPosition_(p1,p1)
rect = tvo.firstRectForRange_(rge) # CGRect
x,y = rect.origin.x,rect.origin.y
if i == len(tv.text):
if i > 0:
x,y = x_y[i-1]
else:
# text is empty
x,y = 0,0
x_y.append((x,y))
return x_yb_up = ui.Button()
b_up.frame = (10,170,100,32)
b_up.title = 'up'
b_up.background_color = 'white'
b_up.border_width = 1
def b_up_action(sender):
tv = sender.superview['TextView']
x_y = get_xy(tv)
c = tv.selected_range[0]
xc,yc = x_y[c]
i = c - 1
while i >= 0:
x,y = x_y[i]
if y < yc:
# previous row
if x <= xc:
tv.selected_range = (i,i)
return
i = i - 1
b_up.action = b_up_action
v.add_subview(b_up)b_clear = ui.Button()
b_clear.frame = (10,290,100,32)
b_clear.title = 'clear'
b_clear.background_color = 'white'
b_clear.border_width = 1
def b_clear_action(sender):
tv = sender.superview['TextView']
tv.text = ''
b_clear.action = b_clear_action
v.add_subview(b_clear)b_del = ui.Button()
b_del.frame = (10,250,100,32)
b_del.title = 'del'
b_del.background_color = 'white'
b_del.border_width = 1
def b_del_action(sender):
tv = sender.superview['TextView']
i = tv.selected_range[0]
if i < len(tv.text):
tv.text = tv.text[:i] + tv.text[i+1:]
#tv.selected_range = (i,i) # refused if i = len(tv.text)
tvo = ObjCInstance(tv)
p1 = tvo.positionFromPosition_offset_(tvo.beginningOfDocument(), i)
p2 = p1
tvo.selectedTextRange = tvo.textRangeFromPosition_toPosition_(p1,p2)
b_del.action = b_del_action
v.add_subview(b_del)b_down = ui.Button()
b_down.frame = (10,210,100,32)
b_down.title = 'down'
b_down.background_color = 'white'
b_down.border_width = 1
def b_down_action(sender):
tv = sender.superview['TextView']
print(tv.selected_range,len(tv.text))
x_y = get_xy(tv)
c = tv.selected_range[0]
#print(x_y,c)
xc,yc = x_y[c]
i = c - 1
while i < len(tv.text):
x,y = x_y[i]
if y > yc:
# next row
if x >= xc:
tv.selected_range = (i,i)
return
else:
if (i+1) < len(tv.text):
if x_y[i+1][1] > y: # i = last character of row under cursor
tv.selected_range = (i,i)
return
else:
pass # try next x
else:
# last character of last row
tv.selected_range = (i,i)
return
i = i + 1
b_down.action = b_down_action
v.add_subview(b_down)v.present('sheet')
tv.selected_range = (0,0)
tv.begin_editing()```Insert Code Here
-
-
@shinya.ta This short post, yes, but not your last script in the post just before.
You have to replace the text "import code here" by your script