【问题标题】:Border of UITextField in Objective cObjective c中UITextField的边框
【发布时间】:2015-03-18 16:28:36
【问题描述】:

当用户点击UITextfield 时,它的边框会像mac 的原生应用一样高亮。显示为蓝色 UIColor 就像动画一样。我是 iOS 新手。请帮忙 。提前致谢。

【问题讨论】:

  • 你想达到什么目的?你有什么尝试...
  • 这个网站不是用来学习基础的。在互联网上搜索,学习如何做,并尝试自己当您在尝试中遇到问题时,不要在这里询问并展示您尝试过的内容。只需 5 秒的谷歌给我这个:coderwall.com/p/hsrecw/add-a-border-to-any-uiview
  • 检查我的答案。您可以使用动画更改文本框边框颜色。

标签: 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];
      

      更改 borderWidthcornerRadius 值以获得更准确和所需的值,就像 mac 应用程序文本字段一样。

      【讨论】:

        猜你喜欢
        • 2016-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-24
        • 1970-01-01
        • 2012-05-23
        • 1970-01-01
        • 2016-03-15
        相关资源
        最近更新 更多