编辑开始时更改 UITextField 背景

发布于 2024-08-16 13:47:06 字数 135 浏览 9 评论 0原文

我想在 UITextField 成为第一个响应者时更改其背景图像,以向用户显示它具有焦点,类似于 CSS 中的 :active 或 :focus 伪类。

我猜我可能需要以编程方式执行此操作;所以非常感谢任何帮助。

-贾尔斯

I'd like to change the background image of a UITextField when it becomes the firstResponder to show the user that it has focus, similar to the :active or :focus pseudo-classes in CSS.

I'm guessing that I may need to do this programmatically; so any help is greatly appreciated.

-Giles

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

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

发布评论

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

评论(4

倒数 2024-08-23 13:47:06

恕我直言,最简洁的方法是子类化 UITextField 并覆盖 becomeFirstResponderresignFirstResponder 来更改文本字段的背景图像。这样,您就可以在任何地方使用新的子类,而无需重新实现委托方法来更改背景。

- (BOOL)becomeFirstResponder {
    BOOL outcome = [super becomeFirstResponder];
    if (outcome) {
      self.background = // selected state image;
    }
    return outcome;
}

- (BOOL)resignFirstResponder {
    BOOL outcome = [super resignFirstResponder];
    if (outcome) {
      self.background = // normal state image;
    }
    return outcome;
}

The cleanest way IMHO is to subclass UITextField and override becomeFirstResponder and resignFirstResponder to change the background image of the text field. That way, you can use your new subclass anywhere without having to reimplement the delegate methods to change the background.

- (BOOL)becomeFirstResponder {
    BOOL outcome = [super becomeFirstResponder];
    if (outcome) {
      self.background = // selected state image;
    }
    return outcome;
}

- (BOOL)resignFirstResponder {
    BOOL outcome = [super resignFirstResponder];
    if (outcome) {
      self.background = // normal state image;
    }
    return outcome;
}
疧_╮線 2024-08-23 13:47:06

您也可以使用 UITextFieldDelegate 方法(恕我直言,比键值观察者更容易维护):

#pragma mark -
#pragma mark UITextFieldDelegate methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    _field.background = [UIImage imageNamed:@"focus.png"];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    _field.background = [UIImage imageNamed:@"nofocus.png"];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

这仅在 UITextField.borderStyle 属性是除 UITextBorderStyleRoundedRect 之外的任何类型时才有效(在这种情况下,不考虑背景属性) 。这意味着您可以将上面的代码与 UITextBorderStyleBezel、UITextBorderStyleLine 和 UITextBorderStyleNone 一起使用,如 borderStyle 文档中所述:

边框样式

文本字段使用的边框样式。

@property(非原子) UITextBorderStyle borderStyle

讨论

该属性的默认值为
UITextBorderStyleNone。如果定制
背景图片设置了,这个属性
被忽略。

这是 UITextField 的背景属性的文档:

背景

代表的图像
文本的背景外观
启用时的字段。

@property(非原子,保留)UIImage
*背景

讨论

设置后,引用的图像
该属性取代了标准
外观由
borderStyle 属性。背景
图像绘制在边框中
图像的矩形部分。图片
您用于文本字段的
背景应该能够拉伸
适合。

You might as well use the UITextFieldDelegate methods (IMHO, easier to maintain than key-value observers):

#pragma mark -
#pragma mark UITextFieldDelegate methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    _field.background = [UIImage imageNamed:@"focus.png"];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    _field.background = [UIImage imageNamed:@"nofocus.png"];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

This only works when the UITextField.borderStyle property is of any type but UITextBorderStyleRoundedRect (in that case, the background property is not taken into account). This means you might use the code above with UITextBorderStyleBezel, UITextBorderStyleLine and UITextBorderStyleNone, as explained in the borderStyle documentation:

borderStyle

The border style used by the text field.

@property(nonatomic) UITextBorderStyle borderStyle

Discussion

The default value for this property is
UITextBorderStyleNone. If a custom
background image is set, this property
is ignored.

This is the documentation for the background property of UITextField:

background

The image that represents the
background appearance of the text
field when it is enabled.

@property(nonatomic, retain) UIImage
*background

Discussion

When set, the image referred to by
this property replaces the standard
appearance controlled by the
borderStyle property. Background
images are drawn in the border
rectangle portion of the image. Images
you use for the text field’s
background should be able to stretch
to fit.

谁与争疯 2024-08-23 13:47:06

SWIFT 4

textField.addTarget(self, action: #selector(anyFunction), for: UIControlEvents.editingDidBegin)


@objc func anyFunction() {
    // Add your conde here
}

SWIFT 4

textField.addTarget(self, action: #selector(anyFunction), for: UIControlEvents.editingDidBegin)


@objc func anyFunction() {
    // Add your conde here
}
金兰素衣 2024-08-23 13:47:06

您可以尝试观察 isFirstResponder 的变化。并更改通知方法中的背景。像这样:

[textField addObserver:theObserver forKeyPath:@"isFirstResponder" options:0 context:nil];

然后在观察者中:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if(object == textField && [keyPath isEqual:@"isFirstResponder"]) {
        //fiddle with object here
    }
}

You can probably try observing for changes to isFirstResponder. and changing the background in the notification method. Something like:

[textField addObserver:theObserver forKeyPath:@"isFirstResponder" options:0 context:nil];

Then in the observer:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if(object == textField && [keyPath isEqual:@"isFirstResponder"]) {
        //fiddle with object here
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文