我想更改我在UITextField控件中设置的占位符文本的颜色,使其变黑.
我更喜欢这样做而不使用普通文本作为占位符,并且必须覆盖所有方法来模仿占位符的行为.
我相信如果我重写这个方法:
- (void)drawPlaceholderInRect:(CGRect)rect
Run Code Online (Sandbox Code Playgroud)
然后我应该能够做到这一点.但我不确定如何从此方法中访问实际的占位符对象.
是否可以在文本中添加阴影UITextField?
我试图在我的文本字段中使用占位符斜体,并且由于我的应用程序是针对iOS 6.0或更新版本,因此决定使用attributedPlaceholder属性而不是滚动更自定义的内容.代码如下:
NSString *plString = @"optional";
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:plString
attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:15]}];
for (UITextField *t in myTextfields){
t.placeholder = plString;
t.attributedPlaceholder = placeholder;
}
Run Code Online (Sandbox Code Playgroud)
然而占位符的样式仍然不是斜体,而是与常规文本相同,只是更暗淡.我缺少什么才能完成这项NSAttributedString工作?
UITextField边框样式的默认高度UITextBorderStyleRoundedRect为31px.对于iPad应用程序,我想制作一个高度为60像素的UITextField,并将文本垂直居中放置以使其看起来很好(默认情况下,文本仅在UITextField的顶部边框下方几个像素处绘制).
如果我做以下......
UITextField *txtLogin = [[UITextField alloc] initWithFrame:CGRectMake(200,200,300,60)];
txtLogin.borderStyle = UITextBorderStyleRoundedRect;
Run Code Online (Sandbox Code Playgroud)
......结果如下:http://grab.by/6jRc
如您所见,输入文本不是垂直居中的.我试图找到一个解决方案,发现我只需从UITextField继承自己的自定义类并覆盖该- (CGRect)textRectForBounds:(CGRect)bounds方法.但是,此方法永远不会被调用,根据此链接,Apple的API中存在错误.因此,我正在寻找有效的替代方案.
什么是解决此问题并为输入文本实现正确垂直对齐的好解决方案?
我有一个自定义,UITextField以便我得到一个自定义占位符文本颜色,如答案建议.但是,我还想在运行时更改占位符文本的颜色,因此我创建了一个属性.
// Overide the placholder text color
- (void) drawPlaceholderInRect:(CGRect)rect
{
[self.placeholderTextColor setFill];
[self.placeholder drawInRect:rect
withFont:self.font
lineBreakMode:UILineBreakModeTailTruncation
alignment:self.textAlignment];
}
- (void) setPlaceholderTextColor:(UIColor *)placeholderTextColor
{
// To verify this is being called and that the placeholder property is set
NSLog(@"placeholder text: %@", self.placeholder);
_placeholderTextColor = placeholderTextColor;
[self setNeedsDisplay]; // This does not trigger drawPlaceholderInRect
}
Run Code Online (Sandbox Code Playgroud)
问题是文档说我不应该直接调用drawPlaceholderInRect,[self setNeedsDisplay];也不行.有任何想法吗?
ios ×4
uitextfield ×4
iphone ×2
objective-c ×2
alignment ×1
cocoa-touch ×1
ipad ×1
shadow ×1
uikit ×1