ada*_*dam 550 uitextfield ios
我想更改我在UITextField控件中设置的占位符文本的颜色,使其变黑.
我更喜欢这样做而不使用普通文本作为占位符,并且必须覆盖所有方法来模仿占位符的行为.
我相信如果我重写这个方法:
- (void)drawPlaceholderInRect:(CGRect)rect
Run Code Online (Sandbox Code Playgroud)
然后我应该能够做到这一点.但我不确定如何从此方法中访问实际的占位符对象.
use*_*136 798
自从在iOS 6中的UIViews中引入属性字符串以来,可以为占位符文本指定颜色,如下所示:
if ([textField respondsToSelector:@selector(setAttributedPlaceholder:)]) {
UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
} else {
NSLog(@"Cannot set placeholder text's color, because deployment target is earlier than iOS 6.0");
// TODO: Add fall-back code to set placeholder color.
}
Run Code Online (Sandbox Code Playgroud)
Jac*_*ack 239
容易和无痛,可能是一些容易的替代品.
_placeholderLabel.textColor
Run Code Online (Sandbox Code Playgroud)
不建议生产,Apple可能会拒绝您的提交.

ada*_*dam 194
您可以这样覆盖drawPlaceholderInRect:(CGRect)rect以手动呈现占位符文本:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
Run Code Online (Sandbox Code Playgroud)
小智 168
您可以使用以下代码将占位符文本颜色更改为您想要的任何颜色.
UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"PlaceHolder Text" attributes:@{NSForegroundColorAttributeName: color}];
Run Code Online (Sandbox Code Playgroud)
dig*_*dog 159
也许您想尝试这种方式,但Apple可能会警告您访问私有ivar:
[self.myTextField setValue:[UIColor darkGrayColor]
forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)
注:
根据MartinAlléus的说法,这不再适用于iOS 7.
Tom*_*ino 142
这适用于Swift <3.0:
myTextField.attributedPlaceholder =
NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
Run Code Online (Sandbox Code Playgroud)
在iOS 8.2和iOS 8.3 beta 4中测试过.
斯威夫特3:
myTextfield.attributedPlaceholder =
NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName : UIColor.red])
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
myTextfield.attributedPlaceholder =
NSAttributedString(string: "placeholder text", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
Run Code Online (Sandbox Code Playgroud)
Swift 4.2:
myTextfield.attributedPlaceholder =
NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
Run Code Online (Sandbox Code Playgroud)
Ben*_*o85 69
在Swift中:
if let placeholder = yourTextField.placeholder {
yourTextField.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSForegroundColorAttributeName: UIColor.blackColor()])
}
Run Code Online (Sandbox Code Playgroud)
在Swift 4.0中:
if let placeholder = yourTextField.placeholder {
yourTextField.attributedPlaceholder = NSAttributedString(string:placeholder,
attributes: [NSAttributedStringKey.foregroundColor: UIColor.black])
}
Run Code Online (Sandbox Code Playgroud)
Nik*_*Kov 54
要在故事板中更改占位符颜色,请使用下一个代码创建扩展名.(随意更新此代码,如果您认为,它可以更清晰,更安全).
extension UITextField {
@IBInspectable var placeholderColor: UIColor {
get {
guard let currentAttributedPlaceholderColor = attributedPlaceholder?.attribute(NSForegroundColorAttributeName, at: 0, effectiveRange: nil) as? UIColor else { return UIColor.clear }
return currentAttributedPlaceholderColor
}
set {
guard let currentAttributedString = attributedPlaceholder else { return }
let attributes = [NSForegroundColorAttributeName : newValue]
attributedPlaceholder = NSAttributedString(string: currentAttributedString.string, attributes: attributes)
}
}
}
Run Code Online (Sandbox Code Playgroud)
extension UITextField {
@IBInspectable var placeholderColor: UIColor {
get {
return attributedPlaceholder?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor ?? .clear
}
set {
guard let attributedPlaceholder = attributedPlaceholder else { return }
let attributes: [NSAttributedStringKey: UIColor] = [.foregroundColor: newValue]
self.attributedPlaceholder = NSAttributedString(string: attributedPlaceholder.string, attributes: attributes)
}
}
}
Run Code Online (Sandbox Code Playgroud)
Gau*_*ani 43
以下仅适用于iOS6 +(如Alexander W的评论中所示):
UIColor *color = [UIColor grayColor];
nameText.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Full Name"
attributes:@{NSForegroundColorAttributeName:color}];
Run Code Online (Sandbox Code Playgroud)
Uma*_*avi 32
有了这个,我们可以在iOS中更改textfield的占位符文本的颜色
[self.userNameTxt setValue:[UIColor colorWithRed:41.0/255.0 green:91.0/255.0 blue:106.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)
Ash*_*shu 28
我已经遇到过这个问题.在我的情况下,代码是正确的.
目标C.
[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)
对于Swift 4.X
tf_mobile.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
Run Code Online (Sandbox Code Playgroud)
希望,这可能会对你有所帮助.
Val*_*der 19
你为什么不用UIAppearance方法:
[[UILabel appearanceWhenContainedIn:[UITextField class], nil] setTextColor:[UIColor whateverColorYouNeed]];
Run Code Online (Sandbox Code Playgroud)
Pet*_*rov 18
同样在你的故事板中,没有单行代码
Fah*_*kar 12
[textfield setValue:your_color forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
注意: Apple可能会拒绝(0.01%几率)您的应用,因为我们正在访问私有API.我在两年后的所有项目中都使用了这个,但Apple没有要求这个.
Man*_*iao 10
对于Xamarin.iOS开发人员,我在本文档中找到了它 https://developer.xamarin.com/api/type/Foundation.NSAttributedString/
textField.AttributedPlaceholder = new NSAttributedString ("Hello, world",new UIStringAttributes () { ForegroundColor = UIColor.Red });
Run Code Online (Sandbox Code Playgroud)
Swift版本.可能它会帮助某人.
class TextField: UITextField {
override var placeholder: String? {
didSet {
let placeholderString = NSAttributedString(string: placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
self.attributedPlaceholder = placeholderString
}
}
}
Run Code Online (Sandbox Code Playgroud)
迅速3.X
textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes:[NSForegroundColorAttributeName: UIColor.black])
Run Code Online (Sandbox Code Playgroud)
迅速5
textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor : UIColor.black])
Run Code Online (Sandbox Code Playgroud)
这个 Swift 4.1 的解决方案
textName.attributedPlaceholder = NSAttributedString(string: textName.placeholder!, attributes: [NSAttributedStringKey.foregroundColor : UIColor.red])
Run Code Online (Sandbox Code Playgroud)
iOS 6或更高版本提供attributedPlaceholder的UITextField.的iOS 3.2和更高版本提供setAttributes:range:的NSMutableAttributedString.
您可以执行以下操作:
NSMutableAttributedString *ms = [[NSMutableAttributedString alloc] initWithString:self.yourInput.placeholder];
UIFont *placeholderFont = self.yourInput.font;
NSRange fullRange = NSMakeRange(0, ms.length);
NSDictionary *newProps = @{NSForegroundColorAttributeName:[UIColor yourColor], NSFontAttributeName:placeholderFont};
[ms setAttributes:newProps range:fullRange];
self.yourInput.attributedPlaceholder = ms;
Run Code Online (Sandbox Code Playgroud)
在iOS7中处理垂直和水平对齐以及占位符的颜色.drawInRect和drawAtPoint不再使用当前上下文fillColor.
OBJ-C
@interface CustomPlaceHolderTextColorTextField : UITextField
@end
@implementation CustomPlaceHolderTextColorTextField : UITextField
-(void) drawPlaceholderInRect:(CGRect)rect {
if (self.placeholder) {
// color of placeholder text
UIColor *placeHolderTextColor = [UIColor redColor];
CGSize drawSize = [self.placeholder sizeWithAttributes:[NSDictionary dictionaryWithObject:self.font forKey:NSFontAttributeName]];
CGRect drawRect = rect;
// verticially align text
drawRect.origin.y = (rect.size.height - drawSize.height) * 0.5;
// set alignment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = self.textAlignment;
// dictionary of attributes, font, paragraphstyle, and color
NSDictionary *drawAttributes = @{NSFontAttributeName: self.font,
NSParagraphStyleAttributeName : paragraphStyle,
NSForegroundColorAttributeName : placeHolderTextColor};
// draw
[self.placeholder drawInRect:drawRect withAttributes:drawAttributes];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
类别FTW.可以进行优化以检查有效的颜色变化.
#import <UIKit/UIKit.h>
@interface UITextField (OPConvenience)
@property (strong, nonatomic) UIColor* placeholderColor;
@end
#import "UITextField+OPConvenience.h"
@implementation UITextField (OPConvenience)
- (void) setPlaceholderColor: (UIColor*) color {
if (color) {
NSMutableAttributedString* attrString = [self.attributedPlaceholder mutableCopy];
[attrString setAttributes: @{NSForegroundColorAttributeName: color} range: NSMakeRange(0, attrString.length)];
self.attributedPlaceholder = attrString;
}
}
- (UIColor*) placeholderColor {
return [self.attributedPlaceholder attribute: NSForegroundColorAttributeName atIndex: 0 effectiveRange: NULL];
}
@end
Run Code Online (Sandbox Code Playgroud)
带有警告的斯威夫特 5。
let attributes = [ NSAttributedString.Key.foregroundColor: UIColor.someColor ]
let placeHolderString = NSAttributedString(string: "DON'T_DELETE", attributes: attributes)
txtField.attributedPlaceholder = placeHolderString
Run Code Online (Sandbox Code Playgroud)
需要注意的是,您必须String在“DON'T_DELETE”所在的位置输入非空,即使该字符串是在其他地方的代码中设置的。可能会为您节省五分钟的头疼时间。
| 归档时间: |
|
| 查看次数: |
335760 次 |
| 最近记录: |