Skip to content

Commit 6e42990

Browse files
author
jsbain
committed
attribtext now with colors, and on existing textviews. added filter_subviews
1 parent 565f307 commit 6e42990

File tree

5 files changed

+137
-21
lines changed

5 files changed

+137
-21
lines changed

apphack.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from objc_util import *
55
import ui,console
6-
6+
import weakref
77

88
w=ObjCClass('UIApplication').sharedApplication().keyWindow()
99
main_view=w.rootViewController().view()
@@ -25,7 +25,12 @@ def get_toolbar(view):
2525
execbtn.image=ui.Image.named('iow:ios7_play_32')
2626
execbtn_obj=ObjCInstance(execbtn)
2727
tb.addSubview_(execbtn_obj)
28-
28+
def make_cleanup(obj):
29+
def cleanup():
30+
if hasattr(obj,'removeFromSuperview' ):
31+
obj.removeFromSuperview()
32+
return cleanup
33+
weakref.ref(execbtn_obj,make_cleanup(execbtn_obj))
2934
def run_script(sender):
3035
import editor
3136
execfile(editor.get_path())

attrib_textview_typing.py

+62-18
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,77 @@
22
# coding: utf-8
33
# experiments with attributed strings
44

5-
from objc import *
5+
from objc_util import *
66
import ctypes
77

88
NSMutableAttributedString=ObjCClass('NSMutableAttributedString')
9+
UIColor=ObjCClass('UIColor')
910
NSFontAttributeName=ns('NSFont')
11+
NSForegroundColorAttributeName=ns('NSColor')
1012
UIFont=ObjCClass('UIFont')
1113

1214

1315
import ui
16+
17+
18+
19+
def buildAttributes():
20+
# d=NSMutableDictionary.new()
21+
d={
22+
NSForegroundColorAttributeName:UIColor.colorWithRed_green_blue_alpha_(tv.R,tv.G,tv.B,1.),
23+
NSFontAttributeName:UIFont.systemFontOfSize_(tv.fontsize)
24+
}
25+
tvobj.setTypingAttributes_(d)
26+
desclbl.text='Font size={} R={:0.2f}, G={:0.2f}, B={:0.2f}'.format(tv.fontsize, tv.R, tv.G, tv.B)
27+
return d
28+
29+
def SizeSliderAction(sender):
30+
tv.fontsize=round(6+sender.value*72.0)
31+
buildAttributes()
32+
33+
34+
def RGBSliderAction(color):
35+
def action(sender):
36+
setattr(tv,color,sender.value)
37+
buildAttributes()
38+
return action
39+
count=0
40+
def textview_should_change(sender,text):
41+
global count
42+
buildAttributes()
43+
count+=1
44+
return True
1445
v=ui.View(frame=(0,0,576,576),bg_color=(0.7,)*3)
15-
txtsize=ui.Slider(bg_color=(1,1,1),frame=(0,50,300,50))
16-
def slideraction(sender):
17-
d=NSMutableDictionary.new()
18-
sz=round(6+sender.value*72.0)
19-
f=UIFont.systemFontOfSize_(sz)
20-
d.setValue_forKey_(f,NSFontAttributeName)
21-
lblobj.setTypingAttributes_(d)
22-
txtsizelbl.text='FontSize={}'.format(sz)
23-
txtsize.action=slideraction
24-
txtsizelbl=ui.Label(frame=(0,0,300,20))
46+
txtsize=ui.Slider(bg_color=(1,1,1),frame=(0,50,300,30))
47+
redslider=ui.Slider(bg_color=(1,0,0),frame=(0,80,300,30))
48+
greenslider=ui.Slider(bg_color=(0,1,0),frame=(0,110,300,30))
49+
blueslider=ui.Slider(bg_color=(0,0,1),frame=(0,140,300,30))
50+
51+
txtsize.action=SizeSliderAction
52+
redslider.action=RGBSliderAction('R')
53+
greenslider.action=RGBSliderAction('G')
54+
blueslider.action=RGBSliderAction('B')
55+
56+
57+
desclbl=ui.Label(frame=(0,0,300,20))
2558

2659
v.add_subview(txtsize)
27-
v.add_subview(txtsizelbl)
28-
lbl=ui.TextView(bg_color='white',frame=(0,150,300,300))
29-
lbl.text='type here'
30-
txtsizelbl.text='Font size={}'.format(lbl.font[1])
31-
txtsize.value=(lbl.font[1]-6)/72.0
32-
v.add_subview(lbl)
60+
v.add_subview(redslider)
61+
v.add_subview(greenslider)
62+
v.add_subview(blueslider)
63+
v.add_subview(desclbl)
64+
tv=ui.TextView(bg_color='white',frame=(0,150,300,300))
65+
66+
tv.textview_should_change=textview_should_change
67+
tv.delegate=tv
68+
tv.fontsize=12
69+
tv.R=0
70+
tv.G=0
71+
tv.B=0
72+
tv.text='type here'
73+
74+
txtsize.value=(tv.font[1]-6)/72.0
75+
v.add_subview(tv)
3376
v.present('sheet')
34-
lblobj=ObjCInstance(lbl._objc_ptr)
77+
tvobj=ObjCInstance(tv)
78+
buildAttributes()

attribtext2.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# coding: utf-8
2+
# set attributed text dor existing textview
3+
# same works for Label, except dont need to set allowsAttributedTextEditing, or call on main thread
4+
from objc_util import *
5+
6+
mystr='''here are some colors:
7+
red, yellow, blue, magenta, black, cyan
8+
this is also editable
9+
'''
10+
mystro=ObjCClass('NSMutableAttributedString').alloc().initWithString_(mystr)
11+
12+
13+
UIColor=ObjCClass('UIColor')
14+
15+
colors={'red': UIColor.redColor(),
16+
'green':UIColor.greenColor(),
17+
'blue':UIColor.blueColor(),
18+
'cyan':UIColor.cyanColor(),
19+
'magenta':UIColor.magentaColor(),
20+
'black':UIColor.blackColor(),
21+
'yellow':UIColor.yellowColor()}
22+
23+
# go through each thing i want to highlight, and addAttribute to that range
24+
for k,color in colors.items():
25+
sre=re.finditer(k,mystr)
26+
for m in sre:
27+
st,end=m.span()
28+
l=end-st
29+
mystro.addAttribute_value_range_('NSColor',color,NSRange(st,l))
30+
31+
# setup views
32+
import ui
33+
34+
v=ui.View(bg_color='white',frame=(0,0,300,300))
35+
tv=ui.TextView(flex='wh',frame=v.bounds)
36+
v.add_subview(tv)
37+
v.present('sheet')
38+
#set up objc instance
39+
tvo=ObjCInstance(tv)
40+
def setAttribs():
41+
tvo.setAllowsEditingTextAttributes_(True)
42+
tvo.setAttributedText_(mystro)
43+
on_main_thread(setAttribs)() #apparently this must be called on main thread for textview
44+

filter_subviews.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coding: utf-8
2+
from objc_util import *
3+
w=ObjCClass('UIApplication').sharedApplication().keyWindow()
4+
main_view=w.rootViewController().view()
5+
6+
def filter_subviews(view,text=None, objcclasstext=None):
7+
matching_svs=[]
8+
sv=view.subviews()
9+
if sv is None:
10+
return matching_svs
11+
for v in sv:
12+
if objcclasstext and objcclasstext in v._get_objc_classname():
13+
matching_svs.append(v)
14+
if text and hasattr(v,'text'):
15+
if str(v.text()) and text in str(v.text()):
16+
matching_svs.append(v)
17+
matching_svs.extend(
18+
filter_subviews(v, text=text, objcclasstext=objcclasstext))
19+
return matching_svs
20+
21+
# don't find editor window!
22+
print 'find'+'me'
23+
console_view=filter_subviews(main_view,'find'+'me')[0]

print_objc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding: utf-8
2-
from objc import *
2+
from objc_util import *
33
import ctypes
44

55
def parse_encoding(enc):

0 commit comments

Comments
 (0)