如何子类化 UITextField 并重写 drawPlaceholderInRect 以更改占位符颜色

发布于 2024-11-09 22:32:06 字数 1269 浏览 5 评论 0原文

我有一个 3 UITextField ,其中设置了占位符文本。在 UITextField 之一上,我希望占位符文本为红色。

现在,在谷歌搜索之后,似乎最好的方法是子类化 UITextField 并覆盖 drawPlaceholderInRect

如何进行子类化和重写drawPlaceholderInRect?我还没有找到任何关于此的代码示例或教程,而且我是 Objective-C 和 iOS 开发的新手,所以发现解决这个问题很棘手。

答案:

创建了一个名为 CustomUITextFieldPlaceholder 的新 Objective-C 类,它是 UITextField 的子类。在 CustomUITextFieldPlaceholder.m 中放入以下代码

 @implementation CustomUITextFieldPlaceholder

- (void)drawPlaceholderInRect:(CGRect)rect {
    // Set colour and font size of placeholder text
    [[UIColor redColor] setFill];
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:12]];
}

@end

以在您的项目中实现上述内容

#import "CustomUITextFieldPlaceholder.h"

,并且

IBOutlet CustomUITextFieldPlaceHolder *txtName;

注意:这是有效的,我相信这是正确的做法,但是我还没有完全测试它。希望这个示例对其他人在我的情况下有所帮助。

编辑:

更改

[[UIColor redColor] setFill];

[[UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:0.7] setFill];

以便我可以将不透明度设置为 70% 以模仿默认占位符。

I have a 3 UITextField with placeholder text set. On one of the UITextField I want the placeholder text to be red.

Now after googling it seems the best way to do this is to subclass UITextField and override drawPlaceholderInRect.

How do I go about subclassing and overriding drawPlaceholderInRect? I've not found any code examples or tutorials on this and I'm new to objective-c and iOS development so finding it tricky to work it out.

Answer:

Created a new objective-c class called CustomUITextFieldPlaceholder which subclassed UITextField. In CustomUITextFieldPlaceholder.m put the following code

 @implementation CustomUITextFieldPlaceholder

- (void)drawPlaceholderInRect:(CGRect)rect {
    // Set colour and font size of placeholder text
    [[UIColor redColor] setFill];
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:12]];
}

@end

To implement the above in your project

#import "CustomUITextFieldPlaceholder.h"

and

IBOutlet CustomUITextFieldPlaceHolder *txtName;

Note: This works and I believe is correct practice, however I have not fully tested it. Hope this example helps others in my situation.

Edit:

Changed

[[UIColor redColor] setFill];

for

[[UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:0.7] setFill];

So that I could set the opacity to 70% to mimic default placeholder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不会写诗 2024-11-16 22:32:11
[yourTextfield setValue:[UIColor colorWithRed:62.0/255.0f green:62.0/255.0f blue:62./255.0f alpha:1.0f]  forKeyPath:@"_placeholderLabel.textColor"];

我想这会有帮助

[yourTextfield setValue:[UIColor colorWithRed:62.0/255.0f green:62.0/255.0f blue:62./255.0f alpha:1.0f]  forKeyPath:@"_placeholderLabel.textColor"];

I guess this would be helpful

°如果伤别离去 2024-11-16 22:32:10

为了回答您的具体问题,这就是子类化的工作原理:

// CustomTextField.h
@interface CustomTextField : UITextField {
}
@end

以下是如何重写该方法:

@implementation
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    return CGRectMake(x,y,width,height);
}
@end

但是我不认为这是您想要重写的方法。我认为这就是您正在寻找的:

@implementation
- (void)drawPlaceholderInRect:(CGRect)rect {
    // Your drawing code.
}
@end

To answer you specific question, this is how subclassing works:

// CustomTextField.h
@interface CustomTextField : UITextField {
}
@end

Here's how to override the method:

@implementation
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    return CGRectMake(x,y,width,height);
}
@end

However I don't think that's the method you want to override. I think this is what you're looking for:

@implementation
- (void)drawPlaceholderInRect:(CGRect)rect {
    // Your drawing code.
}
@end
夜清冷一曲。 2024-11-16 22:32:10

子类化不会改变颜色。我在这里提出了一项工作。

UITextField 占位符字体颜色白色 iOS 5?

Subclassing won't change the color. I have put forward a work around here.

UITextField placeholder font color white iOS 5?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文