Pythonista-Issues
Pythonista-Issues copied to clipboard
Ui.textfield > no built in way to change the placeholder text color
When presenting a view with editor.present_themed() it should change. If the theme is dark, you can't see the placeholder text.
I don't want to mention it because it's trivial. But I will. This is sort of the same thing. I think it would be good if the placeholder text could have a different alignment from the the presenting field. This may sound strange. But there are cases were you have a left aligned field, but want the placeholder text to be centered. Look I understand that if this is not already built into iOS framework, it's not worth thinking about.
Code that creates a centered red placeholder. Does not solve the theming issue, but hey...
import ui
from objc_util import *
v=ui.TextField()
o=ObjCInstance(v)
root=ui.View()
root.add_subview(v)
# create attributed atring placeholder
attribStr=ObjCClass('NSAttributedString')
colorkey=str(ObjCInstance(c_void_p.in_dll(c,'NSForegroundColorAttributeName')))
stylekey=str(ObjCInstance(c_void_p.in_dll(c,'NSParagraphStyleAttributeName')))
style=ObjCClass('NSMutableParagraphStyle').defaultParagraphStyle().mutableCopy()
style.alignment=ui.ALIGN_CENTER
color=UIColor.colorWithComponentRGBA_((1,0,0,1))
attributedPlaceholder=attribStr.alloc().initWithString_attributes_('hello',
{colorkey:color, stylekey:style})
o.textField().attributedPlaceholder=attributedPlaceholder
v.present('popover')
@jsbain one word: ridiculous.
@controversial Should write a python interface to attributedString. It looks bad but is mostly boilerplate
@jsbain , the above centered placeholder code has stopped working. Not sure what has changed. I am sure I tested this when you posted it. The last traceback line is AttributeError: No class method found for selector "colorWithComponentRGBA:"
This is why I am always hesitant about using objc code, if it goes wrong, I am pretty well screwed , as I can't solve it
Looks like +[UIColor colorWithComponentRGBA:]
is an undocumented method added by SpriteKit (see this header dump). There is a documented method +[UIColor colorWithRed:green:blue:alpha:]
which does basically the same thing, you might want to use that instead @jsbain.
Here is how to change the placeholder text color.
import ui
from objc_util import ObjCClass, ObjCInstance
text_field = ui.TextField(placeholder="Password:", height=100, width=100)
text_field_objc = ObjCInstance(text_field).textField()
text_field_objc._placeholderLabel().textColor = ObjCClass('UIColor').colorWithRed_green_blue_alpha_(1, 0, 0, 1) # Red
text_field.present()
Wow, I am late.