【问题标题】:Border of UITextField in Objective cObjective c中UITextField的边框
【发布时间】:2015-03-18 16:28:36
【问题描述】:
当用户点击UITextfield 时,它的边框会像mac 的原生应用一样高亮。显示为蓝色 UIColor 就像动画一样。我是 iOS 新手。请帮忙 。提前致谢。
【问题讨论】:
标签:
ios
objective-c
iphone
ios8
ios8.1
【解决方案1】:
为此,您必须实现 uitextfield 委托
1.
- (void) textFieldDidBeginEditing:(UITextField *)textField {
if (textField == yourtextField) {
yourTextField.layer.borderColor = [[UIColor blueColor] CGColor];
yourTextField.layer.borderWidth = 1.0;
}
}
2.
- (void) textFieldDidEndEditing:(UITextField *)textField {
if (textField == yourtextField) {
yourTextField.layer.borderColor = [[UIColor clearColor] CGColor];
yourTextField.layer.borderWidth = 0.0;
}
}
如果您不了解委托,请研究 ios 中的委托,然后使用此代码
希望对你有帮助
【解决方案2】:
在您的 TextField 委托方法中:输入以下代码。
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
_txtF.layer.borderColor= [UIColor blueColor].CGColor;
_txtF.layer.borderWidth = 1.0f;
_txtF.alpha = 0.0;
//somewhere here should be the [someView addSubview:tmp];
//and after that should go the [someView addSubview:yourTextFieldView];
//somewhere later in your code, when you need the animation to happen
[UIView animateWithDuration:0.1 animations:^{
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_txtF.bounds];
_txtF.layer.masksToBounds = NO;
_txtF.layer.shadowColor = [UIColor blueColor].CGColor;
_txtF.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
_txtF.layer.shadowOpacity = 0.5f;
_txtF.layer.shadowPath = shadowPath.CGPath;
_txtF.alpha = 1.0;
_txtF.layer.cornerRadius = 4.0;
}];
}
你的输出是:
【解决方案3】:
使用UITextField的图层属性在它周围做一个边框。
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.layer.borderColor = [UIColor colorWithRed:151.0/255.0f green:193.0/255.0f blue:252.0/255.0f alpha:1.0f].CGColor;
textField.layer.borderWidth = 3.0f;
textField.layer.cornerRadius = 7.0f;
[self.view addSubview:textField];
更改 borderWidth 和 cornerRadius 值以获得更准确和所需的值,就像 mac 应用程序文本字段一样。