8

UITextView当我们在 textfield 和 textview 中输入或编辑时,如何以编程方式设置 uitextfield 和边框颜色。

我使用了这段代码,但没有改变UITextView.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
4

4 回答 4

25

不要忘记: #Import <QuartzCore/QuartzCore.h>

工作代码:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor=[[UIColor redColor]CGColor];
    textField.layer.borderWidth= 1.0f;
    return YES;
}
于 2013-03-22T14:15:53.023 回答
1
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

声明是否允许用户编辑文本字段。将方法更改为:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
  textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
于 2013-03-22T14:16:30.463 回答
1

给定边框颜色UITextField

添加#import "QuartzCore/QuartzCore.h"框架工作。

textField.layer.borderColor = [UIColor lightGrayColor].CGColor; // set color as you want.
textField.layer.borderWidth = 1.0; // set borderWidth as you want.
于 2013-03-22T14:19:01.027 回答
0

当您编辑文本字段时,有很多方法看起来非常相似。您正在尝试使用-textFieldShouldBeginEditing:. 根据文档,textFieldShouldBeginEditing,“询问代表是否应该在指定的文本字段中开始编辑。” 用法是,“当用户执行通常会启动编辑会话的操作时,文本字段首先调用此方法以查看是否应实际进行编辑。在大多数情况下,您只需从该方法返回 YES 以允许编辑到继续。” 这不是你不想做的。

相反,您应该使用-textFieldDidBeginEditing:. 此方法“告诉代理指定文本字段的编辑开始”。它“通知代理指定的文本字段刚刚成为第一响应者。您可以使用此方法更新代理的状态信息。例如,您可以使用此方法显示在编辑时应该可见的覆盖视图。”

这意味着您的代码应更改为:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}

-(BOOL)textFieldDidBeginEditing:(UITextField *)textField {
    textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}

您可以在http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.htmlUITextFieldDelegate阅读有关文档中方法的更多信息

于 2013-03-22T14:26:27.263 回答