Forum Archive

is there a way to do this with TextView ?

Raymond

I saw the old Question like this, but i did not understand how to use that Answer of the question, so i will ask the same:

How to change a Specific word Property like color or size in TextView ?

i want real example if there is a way.

cvp

@Raymond not sure that I understand your request. Please, try this little script, and for once, give some feedback, thanks

# coding: utf-8
# set attributed text dor existing textview
# same works for Label, except dont need to set allowsAttributedTextEditing, or call on main thread
# https://github.com/khilnani/pythonista-scripts/blob/master/thirdparty/ObjC%20Tools/Jsbain-objc_hacks/attribtext2.py
from __future__ import absolute_import
from objc_util import *
import re

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 list(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)
#set up objc instance
tvo=ObjCInstance(tv)
tvo.setAllowsEditingTextAttributes_(True)
@on_main_thread
def th():
    tvo.setAttributedText_(mystro)
th()
v.present('sheet')

cvp

@Raymond you can also try richlabel module of @mikael here and its doc to see which attributes are possible

Raymond

@cvp I was mean i want some thing like code editer, that changed the color of word by the word type, like:

import : pink
def : red
normal : white

i hope you understand me.

cvp

@Raymond Not yet sure that I understand, sorry for that.
Do you want an ui.TextView where typed words are automatically colored in function of their content? Ex, if you type the word "import" it is immediately colored as pink.
If it is what you want, I think it should be possible via the textview_did_change delegate but you would have to code that.

@omz has written something like that, see here and test it, but not so easy to understand

cvp

@Raymond try this

import re
import ui
from objc_util import *

UIColor=ObjCClass('UIColor')

colors={'def': UIColor.redColor(),
'import':UIColor.greenColor(),
'normal':UIColor.blueColor()}
bigfont = ObjCClass('UIFont').fontWithName_size_('Courier',20)

class MyTextViewDelegate(object):
    def textview_did_change(textview):
        t = textview.text
        tvo = ObjCInstance(textview)
        mystro = ObjCClass('NSMutableAttributedString').alloc().initWithString_(t)
        # go through each thing i want to highlight, and addAttribute to that range
        for k,color in list(colors.items()):
            sre=re.finditer(k,t)
            for m in sre:
                st,end=m.span()
                l=end-st
                mystro.addAttribute_value_range_('NSColor',color,NSRange(st,l)) 
                mystro.addAttribute_value_range_('NSFont', bigfont, NSRange(st,l))
        @on_main_thread
        def th():
            tvo.setAttributedText_(mystro)
        th()                

tv = ui.TextView()
tv.name = 'only for @Raymond'
tv.frame = (0,0,400,500)
tv.delegate = MyTextViewDelegate
tv.present('sheet')

ccc

https://github.com/pyparsing/pyparsing/blob/master/examples/pythonGrammarParser.py
would give you a jump on recognizing Python syntax. pyparsing==2.0.1 is already installed in Pythonista.

cvp

@ccc I know and it was pointed by my link to @omz ยดs script.
But if he wants his particular own words, my very short script could eventually help him, at least if I correctly understood his request, what is less than sure, knowing me.
I'll wait for his feedback.

Raymond

@cvp
yes i was mean this! thank you so much man !!!โค๏ธ

cvp

@Raymond ๐Ÿ˜… I had understood correctly, that is unusual

cvp

@avensis said

I didn't know it was possible to do that.

Almost all is possible with this app ๐Ÿ˜€