2013-03-22 34 views
8

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

我使用此代码,但不更改UITextView的边框颜色。

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

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

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

你可以阅读更多有关文档的UITextFieldDelegate方法你有没有包括QuartzCore框架到您的建立阶段? – 2013-03-22 14:14:39

+0

@ste prescott是的,我包含quartzCore框架到我的构建阶段。但是当我编辑文本字段时,它不应该改变边框颜色。 – raman 2013-03-22 14:18:12

+0

@raman:你有没有试过我的答案? – Bhavin 2013-03-22 14:41:44

回答

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; 
} 
+1

感谢您的回复。 – raman 2013-03-22 14:50:28

+6

在iOS 7中,您必须设置边框宽度或颜色不生效。 – Micah 2014-03-26 19:45:37

1
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 

声明如果用户可以编辑的文本字段。该方法更改为:

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

我对uitextfield使用uiborderstyleline。所以,如何将边框颜色设置为uitextfield的uiborderstyleline。 – raman 2013-03-22 14:37:05

+0

感谢您的回复 – raman 2013-03-22 14:49:02

1

对于给边境颜色UITextField

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

textField.layer.borderColor = [UIColor lightGrayColor].CGColor; // set color as you want. 
textField.layer.borderWidth = 1.0; // set borderWidth as you want. 
+0

我使用uiborderstyleline作为uitextfield。所以,如何将边框颜色设置为uitextfield的uiborderstyleline。 – raman 2013-03-22 14:42:22

+0

谢谢你的回复。 – raman 2013-03-22 14:50:44

0

当你编辑的文本字段,也有很多的方法,似乎真的很相似。您正在尝试使用-textFieldShouldBeginEditing:。根据文档,textFieldShouldBeginEditing,“询问委托是否应该在指定的文本字段中开始编辑。”用法是:“当用户执行通常会启动编辑会话的动作时,文本字段会首先调用此方法以查看是否应该实际进行编辑。在大多数情况下,您只需从该方法返回YES以允许编辑继续。”这不是你想要做的。您可以使用-textFieldDidBeginEditing:。这种方法,“告诉代表编辑开始为指定的文本字段。”它“通知代表指定的文本字段只是成为第一个响应者,你可以使用这个方法来更新你的代理的状态信息,例如,你可以使用这个方法来显示在编辑时应该可见的覆盖视图。

这意味着你的代码应该改变从:在http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

+0

我使用uiborderstyleline uitextfield。所以,如何将边框颜色设置为uitextfield的uiborderstyleline。 – raman 2013-03-22 14:40:21

+0

感谢您的回复。 – raman 2013-03-22 14:49:30

相关问题