编辑开始时更改UITextField背景 [英] Change UITextField background when editing begins

查看:127
本文介绍了编辑开始时更改UITextField背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改UITextField的背景图像,当它变成firstResponder时向用户显示它有焦点,类似于CSS中的:active或:focus伪类。



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



-Giles

解决方案

你还不如使用UITextFieldDelegate方法(IMHO,更易维护比键值观察者):

 的#pragma马克 -  
#pragma mark UITextFieldDelegate方法

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

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

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

这仅作品当UITextField.borderStyle属性是任何类型,但UITextBorderStyleRoundedRect的(在这种情况下,不考虑背景属性)。这意味着你可以使用UITextBorderStyleBezel,UITextBorderStyleLine和UITextBorderStyleNone上面的代码,如borderStyle文档中所述:


borderStyle



'p>通过将文本字段中使用的边框样式。



@属性(非原子)UITextBorderStyle的borderStyle



讨论



此属性的默认值为
UITextBorderStyleNone。如果设置了自定义
背景图像,则忽略此属性


这是UITextField的背景属性:


背景



代表$ b的图像启用时
字段的$ b背景外观。



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



讨论



设置后,
引用的图像将此属性替换为受控制的标准
外观通过
borderStyle属性。背景
图像绘制在图像的边框
矩形部分。用于文本字段的
背景的图片
应该能够拉伸
以适合。



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

解决方案

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.

这篇关于编辑开始时更改UITextField背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆