【问题标题】:How to set the border of UITextView to same as border color of UITextField如何将 UITextView 的边框设置为与 UITextField 的边框颜色相同
【发布时间】:2014-11-15 17:34:58
【问题描述】:

默认情况下,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颜色?

【问题讨论】:

标签: ios swift uitextview


【解决方案1】:

与 Swift 5.2 和暗模式支持一样 :)

我只想添加一个使用自适应颜色的语义 UI 示例。有时硬编码值不是您想要的,特别是如果您支持暗模式。 我认为使用.label 作为语义颜色可以让您在明暗模式下使用 UITextField 边框使用的默认灰色。 试试这个:

myTextView.layer.cornerRadius = 10
myTextView.layer.borderWidth = 1
myTextView.layer.borderColor = UIColor.label.cgColor

我也喜欢玩其他语义颜色。这在黑暗模式下看起来不错:

myTextField.layer.cornerRadius = 10
myTextField.layer.borderWidth = 1
myTextField.layer.borderColor = UIColor.systemGray4.cgColor

您的 UITextView 看起来与以下内容完全相同:

myTextView.layer.cornerRadius = 10
myTextView.layer.borderWidth = 1.0
myTextView.layer.borderColor = UIColor.systemGray4.cgColor

【讨论】:

  • 我认为这里的问题更多:“实际的文本字段值是多少?”
  • 你的意思是RGB值吗?已经有了 RGB 值的答案,但我想改用语义颜色。
【解决方案2】:

整个项目的通用解决方案。您可以外部 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 文件,您就可以在属性检查器中看到更多选项。

【讨论】:

    【解决方案3】:

    swift 4.x/ios11.

    我使用模拟器在 PSD 中进行了另一项测量。 我可以确认半径为 0.5,颜色为 0.8,因为 205/255 = 0.8(或十六进制中的“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{ 让 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。

    【讨论】:

      【解决方案4】:

      我在 iOS 10 上遇到过类似的问题,但无法找到答案,但这是我使用实用工具中的数字色度计和 iOS 模拟器发现的。

      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
      

      【讨论】:

      • 我什至不知道mac上有这样的应用程序:)
      【解决方案5】:

      在 Swift 上试试这个代码

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

      【讨论】:

      • 这使它变白了。
      【解决方案6】:

      确切的颜色是:

      目标-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
      

      【讨论】:

        【解决方案7】:

        此 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.8吗?
        【解决方案8】:

        试试这个代码。

        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。

        【讨论】:

        • 在 Mac 中有一个名为 DigitalColor Meter 的实用程序。使用该实用程序来获取文本字段的确切 RGB。您只需将光标拖到 UITextField 边框上即可。
        • 我使用 0.5 的边框宽度来获得 iPad mini 屏幕上另一个条目的确切外观。
        • @caitlin 同意,默认边框宽度不是1。
        猜你喜欢
        • 1970-01-01
        • 2010-12-24
        • 1970-01-01
        • 1970-01-01
        • 2015-11-29
        • 2015-12-11
        • 2013-09-16
        • 2013-06-03
        • 1970-01-01
        相关资源
        最近更新 更多