iPhone的UITextField-更改占位符文本颜色

我想改变占位符文本我在我的UITextField控件设置的颜色,使其变黑。 我宁愿做这个正常的文本占位符,不得不重写所有模仿一个占位符的行为。 我相信,如果我重写

- (void)drawPlaceholderInRect:(CGRect)rect

..那么我应该能够做到这一点。但我不能确定如何从内部访问实际的占位符对象
本文地址 :CodeGo.net/78370/
-------------------------------------------------------------------------------------------------------------------------
1. 您可以覆盖drawPlaceholderInRect:(的CGRect)RECT因此手动呈现占位符文本:

- (void) drawPlaceholderInRect:(CGRect)rect {
 [[UIColor blueColor] setFill];
 [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}


2. 因为在引入归因串UIViewS IN的iOS 6,有可能的颜色分配给这样的占位符文本:

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.
}


3. 也许你想尝试这种方式,但苹果可能会警告您访问私有伊娃:

[self.myTextField setValue:[UIColor darkGrayColor] 
    forKeyPath:@"_placeholderLabel.textColor"];

注这不是工作在iOS上7了,根据马丁Alléus。
4. 您可以更改的占位符文本颜色您要下面的代码的任何颜色。

UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"PlaceHolder Text" attributes:@{NSForegroundColorAttributeName: color}];


5. 覆盖drawPlaceholderInRect:将正确的方法,但它并不因API中的错误的工作(或 永远不被调用上UITextField。 另请参阅 你digdog的解决方案。由于我不知道那些通过了苹果的审核,我选择了不同的解决方案:用我自己的标签模仿占位符的行为叠加文本字段。 这是一个虽然。该代码如下所示(请注意,我的TextField类的子类里面做这个):

@implementation PlaceholderChangingTextField
- (void) changePlaceholderColor:(UIColor*)color
{ 
 // Need to place the overlay placeholder exactly above the original placeholder
 UILabel *overlayPlaceholderLabel = [[[UILabel alloc] initWithFrame:CGRectMake(self.frame.origin.x + 8, self.frame.origin.y + 4, self.frame.size.width - 16, self.frame.size.height - 8)] autorelease];
 overlayPlaceholderLabel.backgroundColor = [UIColor whiteColor];
 overlayPlaceholderLabel.opaque = YES;
 overlayPlaceholderLabel.text = self.placeholder;
 overlayPlaceholderLabel.textColor = color;
 overlayPlaceholderLabel.font = self.font;
 // Need to add it to the superview, as otherwise we cannot overlay the buildin text label.
 [self.superview addSubview:overlayPlaceholderLabel];
 self.placeholder = nil;
}


6. 荫新的Xcode CodeGo.net,我找到了一种方法来左右的效果。 我把一个UILabel到位占位与所需的格式,并把它藏在

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
 switch (textField.tag)
 {
  case 0:
   lblUserName.hidden=YES;
   break;
  case 1:
   lblPassword.hidden=YES;
   break;
  default:
   break;
 }
}

我同意它周围的工作,而不是一个真正的解决方案,但效果是从这个链接得到它
7. 的iOS 6和更高版本提供了“attributedPlaceholder”上的UITextField。的iOS 3.2和更高版本提供“setAttributes:范围:”上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;


8. 保留占位符的空白,并把标签上的编辑按钮的上方-不需要子类化的另一个选择。管理就像你将管理的占位符的标签(清除输入任何东西..)
9. 我需要保持占位符所以亚当的回答是不够的 为了解决这个小的变化,我希望能帮助你吧:

- (void) drawPlaceholderInRect:(CGRect)rect {
 //search field placeholder color
 UIColor* color = [UIColor whiteColor];
 [color setFill];
 [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
}


10. 对于MonoTouch的(Xamarin.iOS),这里是亚当的回答,翻译成C#中:

public class MyTextBox : UITextField
{
 public override void DrawPlaceholder(RectangleF rect)
 {
  UIColor.FromWhiteAlpha(0.5f, 1f).SetFill();
  new NSString(this.Placeholder).DrawString(rect, 
    UIFont.FromName(MyAppearance.RegularFontName, 16f));
 }
}


11. 尽我所能为IOS7,少做的是:

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
 return [self textRectForBounds:bounds];
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
 return [self textRectForBounds:bounds];
}
- (CGRect)textRectForBounds:(CGRect)bounds {
 CGRect rect = CGRectInset(bounds, 0, 6); //TODO: can be improved by comparing font size versus bounds.size.height
 return rect;
}
- (void)drawPlaceholderInRect:(CGRect)rect {
 UIColor *color =RGBColor(65, 65, 65);
 if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
 [self.placeholder drawInRect:rect withAttributes:@{NSFontAttributeName:self.font, UITextAttributeTextColor:color}];
 } else {
 [color setFill];
 [self.placeholder drawInRect:rect withFont:self.font];
 }
}


12. 使用KVC

[yourtextfield setValue:[UIColor colorWithRed:120.0/255.0 green:116.0/255.0 blue:115.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];

你可以设置你自己的颜色来占位符
13. 处理垂直和水平,以及在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


14. 我建议另一种解决方案。由于占位符文本框的默认字体设置,只需设置初始字体颜色为你想要的占位符,字体颜色。然后设置你的UITextField的委托和

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
 //set color for text input
 textField.textColor = [UIColor blackColor];
 return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
 //set color for placeholder text
 textField.textColor = [UIColor redColor];
 return YES;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值