首页 文章

如何将UITextView的边框设置为与UITextField的边框颜色相同

提问于
浏览
38

默认情况下,UITextField的边框颜色为浅灰色 . 我想将我的UITextView设置为与UITextField具有相同的边框颜色 .

我试过了:

myTextView.layer.borderColor = UIColor.lightGrayColor().CGColor
or
myTextView.layer.borderColor = UIColor.lightTextColor().CGColor
or 
myTextView.layer.borderColor = myTextField.layer.borderColor

他们仍然有不同的颜色 . 你能告诉我如何看到UITextView边框以匹配UITextField颜色吗?

谢谢 .

7 回答

  • 27

    试试这个代码 .

    UIColor *borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];
    
    myTextView.layer.borderColor = borderColor.CGColor;
    myTextView.layer.borderWidth = 1.0;
    myTextView.layer.cornerRadius = 5.0;
    

    更改borderWidth和cornerRadius值以获得与UITextField完全相同的ui .

  • 2

    此Swift代码也可用于设置与 UITextField 相同的边框颜色,宽度和半径:

    myTextView.layer.borderColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0).CGColor
    myTextView.layer.borderWidth = 1.0
    myTextView.layer.cornerRadius = 5
    
  • 0

    我有这个类似的问题适用于iOS 10,却不能找到答案,但是这是我发现使用数码色彩照度计在公用事业和iPhone模拟器 .

    Swift 3 / iOS 10

    let color = UIColor(red: 186/255, green: 186/255, blue: 186/255, alpha: 1.0).cgColor
    myTextView.layer.borderColor = color
    myTextView.layer.borderWidth = 0.5
    myTextView.layer.cornerRadius = 5
    
  • 14

    swift 4.x / ios11 .

    我使用模拟器在PSD中做了另一个测量 . 我可以确认半径是0.5,颜色是0.8,因为205/255 = 0.8(或者HEX中的"cdcdcd",如PSD所示,但是宽度必须是 0.5 . (我附加了一个PSD,您可以在其中比较编辑字段的半径(UITExtField)和应用于UITextView的半径 .

    所以它是正确的:

    let borderGray = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1)
        self.TxtV.layer.borderColor = borderGray.cgColor
        self.TxtV.layer.borderWidth = 0.5
        self.TxtV.layer.cornerRadius = 5
    

    注意:我试图从View上的TextField获取颜色,但我得到了:

    如果让borderGray = self.cellPhoneTxt.layer.borderColor {let BG = UIColor(cgColor:borderGray)

    print(BG)
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        BG.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        print(red, green, blue, alpha)
    
    }
    

    但我进入了控制台:

    kCGColorSpaceModelRGB 0 0 0 1

    0.0 0.0 0.0 1.0

    所以看来Apple正在使用全黑和一些Alpha .

    enter image description here

  • 59

    试试Swift的这段代码吧

    let borderColor = UIColor.whiteColor.Cgcolor()
    
    myTextView.layer.borderColor = borderColor.CGColor;
    myTextView.layer.borderWidth = 1.0;
    myTextView.layer.cornerRadius = 5.0;
    
  • 3

    确切的颜色是:

    Objective-C的:

    [UIColor colorWithRed:0.76 green:0.76 blue:0.76 alpha:1.0].CGColor;
    

    迅速:

    UIColor(red:0.76, green:0.76, blue:0.76, alpha:1.0).CGColor
    
  • 6

    对于整个项目的通用解决方案 . 您可以extern UIView类并将这些方法添加到它 .

    -(void)setBorderColor:(UIColor *)borderColor{
        self.layer.borderColor = (borderColor).CGColor;
    }
    -(UIColor*)borderColor{
        return [UIColor colorWithCGColor: self.layer.borderColor];
    }
    
    //Getter and setter for border width
    -(void)setBorderWidth:(NSInteger)borderWidth{
        self.layer.borderWidth = borderWidth;
    }
    -(NSInteger)borderWidth{
        return  (NSInteger)roundf((self.layer.borderWidth));
    }
    

    Its对UIView的扩展 . 只需在项目中添加这些UIView Designable.h / m文件,您就可以在属性检查器中看到更多选项 .

相关问题