ios UITextField 使用故事板设置边框颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26221993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITextField set border color using storyboard
提问by katit
I'd like to set border color using storyboard if possible. I've seen answer here: UITextField border color
如果可能,我想使用故事板设置边框颜色。我在这里看到了答案:UITextField border color
And I followed answer in storyboard:
我在故事板中遵循了答案:
All properties set, but TextField doesn't show border. Any suggestions?
已设置所有属性,但 TextField 不显示边框。有什么建议?
回答by Markus
Well as Bogdanpointed out, you could very well do that with simple subclassing and just a few bits of code. After that everything will be editable in Storyboards.
正如Bogdan指出的那样,您可以通过简单的子类化和少量代码来很好地做到这一点。之后,所有内容都可以在 Storyboards 中进行编辑。
- Subclass UITextField
- Create two properties, one for border width and one for border color
- Make those variables IBInspectable and entire class IBDesignable
- You'll be able to change color and width of border and see the change in real time.
- 子类 UITextField
- 创建两个属性,一个用于边框宽度,一个用于边框颜色
- 使这些变量 IBInspectable 和整个类 IBDesignable
- 您将能够更改边框的颜色和宽度并实时查看更改。
Code for illustration (Swift 3.1):
示例代码(Swift 3.1):
@IBDesignable
class FormTextField: UITextField {
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.cgColor
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
}
Edit: You'll see this in your Attributes Inspector
编辑:你会在你的属性检查器中看到这个
回答by Rameswar Prasad
As Bogdan pointed out it's true that you can't find the layer.borderColor property in storyboard as it's a run time thing.
正如 Bogdan 指出的那样,您确实无法在故事板中找到 layer.borderColor 属性,因为它是运行时的事情。
However still you can set the borderColorwithout using IB_DESIGNABLE, on any view(or UIView Subclass) with a little bit of coding.
但是,您仍然可以在不使用 IB_DESIGNABLE 的情况下通过一点编码在任何视图(或 UIView 子类)上设置borderColor。
Below are the steps how to achieve it,
以下是如何实现它的步骤,
- Create a category on CALayer class. Declare a property of type UIColorwith a suitable name, I'll name it as borderUIColor.
- Write the setter and getter for this property.
- In the 'Setter' method just set the "borderColor" property of layer to the new colors CGColor value.
- In the 'Getter' method return UIColor with layer's borderColor.
- 在 CALayer 类上创建一个类别。使用合适的名称声明UIColor类型的属性,我将其命名为borderUIColor。
- 为这个属性编写 setter 和 getter。
- 在“Setter”方法中,只需将图层的“borderColor”属性设置为新颜色的 CGColor 值。
- 在'Getter' 方法中返回带有图层边框颜色的 UIColor。
P.S: Remember, Categories can't have stored properties. 'borderUIColor' is used as a calculated property, just as a reference to achieve what we're focusing on.
PS:请记住,类别不能具有存储属性。'borderUIColor' 用作计算属性,作为实现我们关注的内容的参考。
Please have a look at the below code sample;
请看下面的代码示例;
Objective C:
目标 C:
Interface File:
接口文件:
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
@interface CALayer (BorderProperties)
// This assigns a CGColor to borderColor.
@property (nonatomic, assign) UIColor* borderUIColor;
@end
Implementation File:
实施文件:
#import "CALayer+BorderProperties.h"
@implementation CALayer (BorderProperties)
- (void)setBorderUIColor:(UIColor *)color {
self.borderColor = color.CGColor;
}
- (UIColor *)borderUIColor {
return [UIColor colorWithCGColor:self.borderColor];
}
@end
Swift 3.1:
斯威夫特 3.1:
extension CALayer {
var borderUIColor: UIColor {
set {
self.borderColor = newValue.cgColor
}
get {
return UIColor(cgColor: self.borderColor!)
}
}
}
And finally go to your storyboard/XIB, follow the remaining steps;
最后转到您的故事板/XIB,按照其余步骤操作;
- Click on the View object for which you want to set border Color.
- Click on "Identity Inspector"(3rd from Left) in "Utility"(Right side of the screen) panel.
- Under "User Defined Runtime Attributes", click on the "+" button to add a key path.
- Set the type of the key path to "Color".
- Enter the value for key path as "layer.borderUIColor". [Remember this should be the variable nameyou declared in category, not borderColorhere it's borderUIColor].
- Finally chose whatever color you want.
- 单击要为其设置边框颜色的视图对象。
- 单击“Utility”(屏幕右侧)面板中的“Identity Inspector”(左起第三个)。
- 在“用户定义的运行时属性”下,单击“+”按钮以添加关键路径。
- 将键路径的类型设置为“颜色”。
- 输入键路径的值作为“layer.borderUIColor”。[请记住,这应该是您在类别中声明的变量名称,而不是borderColor,这里是borderUIColor]。
- 最后选择你想要的任何颜色。
Edit: You've to set layer.borderWidthproperty value to at least 1 to see the border color.
编辑:您必须将layer.borderWidth属性值设置为至少 1 才能看到边框颜色。
Build and Run. Happy Coding. :)
构建和运行。快乐编码。:)
回答by Gismay
I'm not sure you can change the border colour of a UITextfield in storyboard. You can change it programmatically with something along the lines of;
我不确定您是否可以更改故事板中 UITextfield 的边框颜色。您可以通过以下方式以编程方式更改它;
UITextField *myTextField = (UITextField *)[self.view viewWithTag:1];
myTextField.borderStyle = UITextBorderStyleLine;
myTextField.layer.borderWidth = 2;
myTextField.layer.borderColor = [[UIColor redColor] CGColor];
Hope this helps.
希望这可以帮助。
回答by Gerardo Salazar Sánchez
Adition to markus, put the full code:
除了markus,把完整的代码:
import UIKit //IMPORTANT
@IBDesignable
class BorderTextField: UITextField {
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.cgColor
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
}
回答by Bogdan Raducan
It doesn't show any border because of the layer.borderColor
property. It's of type CGColor and Runtime attributes doesn't yet support that by default so, setting just one attribute wrong, disables the other ones as well.
由于layer.borderColor
属性,它不显示任何边框。它是 CGColor 类型,并且运行时属性在默认情况下尚不支持,因此,仅将一个属性设置错误,也会禁用其他属性。
To do it from the storyboard but also involving some code and subclassing, you can use this method:
要从故事板完成,但也涉及一些代码和子类化,您可以使用此方法:
Subclass UITextField and make an
IB_DESIGNABLE
UIColor property, that you'll then transform into CGColor and apply it toself.layer.borderColor
.
子类化 UITextField 并创建一个
IB_DESIGNABLE
UIColor 属性,然后将其转换为 CGColor 并将其应用于self.layer.borderColor
.
回答by FranMowinckel
This is a variant over @rameswar answer which I think it's correct. Since we're applying a UIColor
, I think it's preferable to write an extension for the UITextField
instead (UI
things together):
这是@rameswar 答案的一个变体,我认为它是正确的。由于我们正在应用 a UIColor
,我认为最好为UITextField
相反(UI
一起)编写扩展:
extension UITextField {
var borderColor : UIColor? {
get {
if let cgcolor = layer.borderColor {
return UIColor(CGColor: cgcolor)
} else {
return nil
}
}
set {
layer.borderColor = newValue?.CGColor
// width must be at least 1.0
if layer.borderWidth < 1.0 {
layer.borderWidth = 1.0
}
}
}
}
The runtime property would be then borderColor
(so you don't need to type layer.
and I think it's a bit cleaner than borderUIColor
).
运行时属性将是borderColor
(所以你不需要输入layer.
,我认为它比 更干净borderUIColor
)。
The borderColor
of CALayer
is optional so it's this property. It gets black when set to nil
的borderColor
的CALayer
所以它的这个属性是可选的。设置为黑色时nil
And finally, the layer.borderWidth
it's set to a minimum 1.0 because the color it's not set otherwise.
最后,layer.borderWidth
它被设置为最小 1.0,因为它没有设置其他颜色。