Hi,
I need to use some features of the UITextField that are not included in the ui.TextField: specifically, I want to use a text with attributes (attributedText) and set the inputView of an object created with field = objc_util.ObjCInstance(ui.TextField()).
I tried in several ways, but I do not seem able to access the attributedText (or inputView) properties directly. I tried three different techniques:
-
A direct access to the
field.attributedTextproduces no result. I naively thought that there is a mapping between Python and Objective C properties, but this does not seem to be the case. -
I tried then
field.setAttributedText_(...), but in this case objc_util complains with an error. -
Finally, if I try the more involved
setValue_forKey_method, that crashes Pythonista.
Any help is greatly appreciated!
N.B. I managed to create an object with ObjCClass('UITextField') and then add it to a view with ObjCInstance(view).addSubview_, but I presume there must be a way that uses all the nice infrastructure of a ui.TextField without duplicating the work done already in the ui module.
import ui
from objc_util import *
@on_main_thread
def test():
field = ui.TextField()
fieldc = ObjCInstance(field)
red = UIColor.redColor()
mystring = ObjCClass('NSMutableAttributedString').alloc()
mystring.initWithString_('quick test')
mystring.setAttributes_range_({'NSColor': red}, NSRange(1,2))
# The following line produces no result
fieldc.attributedText = mystring
# Pythonista complains that setAttributedText: does not exist
fieldc.setAttributedText_(mystring)
# The following line makes Pythonista crash
fieldc.setValue_forKey_(mystring, 'attributedText')
field.borderStyle = 3
view = ui.View()
view.add_subview(field)
view.present()
test()