Pythonista-Issues icon indicating copy to clipboard operation
Pythonista-Issues copied to clipboard

Ui.textfield > no built in way to change the placeholder text color

Open 1dancook opened this issue 8 years ago • 7 comments

When presenting a view with editor.present_themed() it should change. If the theme is dark, you can't see the placeholder text.

1dancook avatar Jul 28 '16 00:07 1dancook

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.

Phuket2 avatar Jul 28 '16 14:07 Phuket2

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 avatar Jul 28 '16 15:07 jsbain

@jsbain one word: ridiculous.

1dancook avatar Jul 28 '16 15:07 1dancook

@controversial Should write a python interface to attributedString. It looks bad but is mostly boilerplate

jsbain avatar Jul 28 '16 15:07 jsbain

@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

Phuket2 avatar Nov 06 '16 16:11 Phuket2

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.

dgelessus avatar Nov 06 '16 16:11 dgelessus

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.

AustinAres2007 avatar Oct 24 '22 22:10 AustinAres2007