Skip to content

Commit

Permalink
attribtext now with colors, and on existing textviews. added filter_s…
Browse files Browse the repository at this point in the history
…ubviews
  • Loading branch information
jsbain committed Aug 6, 2015
1 parent 565f307 commit 6e42990
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 21 deletions.
9 changes: 7 additions & 2 deletions apphack.py
Expand Up @@ -3,7 +3,7 @@

from objc_util import *
import ui,console

import weakref

w=ObjCClass('UIApplication').sharedApplication().keyWindow()
main_view=w.rootViewController().view()
Expand All @@ -25,7 +25,12 @@ def get_toolbar(view):
execbtn.image=ui.Image.named('iow:ios7_play_32')
execbtn_obj=ObjCInstance(execbtn)
tb.addSubview_(execbtn_obj)

def make_cleanup(obj):
def cleanup():
if hasattr(obj,'removeFromSuperview' ):
obj.removeFromSuperview()
return cleanup
weakref.ref(execbtn_obj,make_cleanup(execbtn_obj))
def run_script(sender):
import editor
execfile(editor.get_path())
Expand Down
80 changes: 62 additions & 18 deletions attrib_textview_typing.py
Expand Up @@ -2,33 +2,77 @@
# coding: utf-8
# experiments with attributed strings

from objc import *
from objc_util import *
import ctypes

NSMutableAttributedString=ObjCClass('NSMutableAttributedString')
UIColor=ObjCClass('UIColor')
NSFontAttributeName=ns('NSFont')
NSForegroundColorAttributeName=ns('NSColor')
UIFont=ObjCClass('UIFont')


import ui



def buildAttributes():
# d=NSMutableDictionary.new()
d={
NSForegroundColorAttributeName:UIColor.colorWithRed_green_blue_alpha_(tv.R,tv.G,tv.B,1.),
NSFontAttributeName:UIFont.systemFontOfSize_(tv.fontsize)
}
tvobj.setTypingAttributes_(d)
desclbl.text='Font size={} R={:0.2f}, G={:0.2f}, B={:0.2f}'.format(tv.fontsize, tv.R, tv.G, tv.B)
return d

def SizeSliderAction(sender):
tv.fontsize=round(6+sender.value*72.0)
buildAttributes()


def RGBSliderAction(color):
def action(sender):
setattr(tv,color,sender.value)
buildAttributes()
return action
count=0
def textview_should_change(sender,text):
global count
buildAttributes()
count+=1
return True
v=ui.View(frame=(0,0,576,576),bg_color=(0.7,)*3)
txtsize=ui.Slider(bg_color=(1,1,1),frame=(0,50,300,50))
def slideraction(sender):
d=NSMutableDictionary.new()
sz=round(6+sender.value*72.0)
f=UIFont.systemFontOfSize_(sz)
d.setValue_forKey_(f,NSFontAttributeName)
lblobj.setTypingAttributes_(d)
txtsizelbl.text='FontSize={}'.format(sz)
txtsize.action=slideraction
txtsizelbl=ui.Label(frame=(0,0,300,20))
txtsize=ui.Slider(bg_color=(1,1,1),frame=(0,50,300,30))
redslider=ui.Slider(bg_color=(1,0,0),frame=(0,80,300,30))
greenslider=ui.Slider(bg_color=(0,1,0),frame=(0,110,300,30))
blueslider=ui.Slider(bg_color=(0,0,1),frame=(0,140,300,30))

txtsize.action=SizeSliderAction
redslider.action=RGBSliderAction('R')
greenslider.action=RGBSliderAction('G')
blueslider.action=RGBSliderAction('B')


desclbl=ui.Label(frame=(0,0,300,20))

v.add_subview(txtsize)
v.add_subview(txtsizelbl)
lbl=ui.TextView(bg_color='white',frame=(0,150,300,300))
lbl.text='type here'
txtsizelbl.text='Font size={}'.format(lbl.font[1])
txtsize.value=(lbl.font[1]-6)/72.0
v.add_subview(lbl)
v.add_subview(redslider)
v.add_subview(greenslider)
v.add_subview(blueslider)
v.add_subview(desclbl)
tv=ui.TextView(bg_color='white',frame=(0,150,300,300))

tv.textview_should_change=textview_should_change
tv.delegate=tv
tv.fontsize=12
tv.R=0
tv.G=0
tv.B=0
tv.text='type here'

txtsize.value=(tv.font[1]-6)/72.0
v.add_subview(tv)
v.present('sheet')
lblobj=ObjCInstance(lbl._objc_ptr)
tvobj=ObjCInstance(tv)
buildAttributes()
44 changes: 44 additions & 0 deletions attribtext2.py
@@ -0,0 +1,44 @@
# coding: utf-8
# set attributed text dor existing textview
# same works for Label, except dont need to set allowsAttributedTextEditing, or call on main thread
from objc_util import *

mystr='''here are some colors:
red, yellow, blue, magenta, black, cyan
this is also editable
'''
mystro=ObjCClass('NSMutableAttributedString').alloc().initWithString_(mystr)


UIColor=ObjCClass('UIColor')

colors={'red': UIColor.redColor(),
'green':UIColor.greenColor(),
'blue':UIColor.blueColor(),
'cyan':UIColor.cyanColor(),
'magenta':UIColor.magentaColor(),
'black':UIColor.blackColor(),
'yellow':UIColor.yellowColor()}

# go through each thing i want to highlight, and addAttribute to that range
for k,color in colors.items():
sre=re.finditer(k,mystr)
for m in sre:
st,end=m.span()
l=end-st
mystro.addAttribute_value_range_('NSColor',color,NSRange(st,l))

# setup views
import ui

v=ui.View(bg_color='white',frame=(0,0,300,300))
tv=ui.TextView(flex='wh',frame=v.bounds)
v.add_subview(tv)
v.present('sheet')
#set up objc instance
tvo=ObjCInstance(tv)
def setAttribs():
tvo.setAllowsEditingTextAttributes_(True)
tvo.setAttributedText_(mystro)
on_main_thread(setAttribs)() #apparently this must be called on main thread for textview

23 changes: 23 additions & 0 deletions filter_subviews.py
@@ -0,0 +1,23 @@
# coding: utf-8
from objc_util import *
w=ObjCClass('UIApplication').sharedApplication().keyWindow()
main_view=w.rootViewController().view()

def filter_subviews(view,text=None, objcclasstext=None):
matching_svs=[]
sv=view.subviews()
if sv is None:
return matching_svs
for v in sv:
if objcclasstext and objcclasstext in v._get_objc_classname():
matching_svs.append(v)
if text and hasattr(v,'text'):
if str(v.text()) and text in str(v.text()):
matching_svs.append(v)
matching_svs.extend(
filter_subviews(v, text=text, objcclasstext=objcclasstext))
return matching_svs

# don't find editor window!
print 'find'+'me'
console_view=filter_subviews(main_view,'find'+'me')[0]
2 changes: 1 addition & 1 deletion print_objc.py
@@ -1,5 +1,5 @@
# coding: utf-8
from objc import *
from objc_util import *
import ctypes

def parse_encoding(enc):
Expand Down

0 comments on commit 6e42990

Please sign in to comment.