Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attribtext now with colors, and on existing textviews. added filter_s…
…ubviews
- Loading branch information
jsbain
committed
Aug 6, 2015
1 parent
565f307
commit 6e42990
Showing
5 changed files
with
137 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# coding: utf-8 | ||
from objc import * | ||
from objc_util import * | ||
import ctypes | ||
|
||
def parse_encoding(enc): | ||
|