I want a flat looking white bordered around my UIButton. I would like it in Storyboard or programmatically. My code isn't working.
Code:
UIButton.layer.cornerRadius = 2;
UIButton.layer.borderWidth = 1;
UIButton.layer.borderColor = UIColor.whiteColor()
You should create referencing outlet for your button from storyboard to your VC named for example myButton than set its properties
myButton.layer.cornerRadius = 2;
myButton.layer.borderWidth = 1;
myButton.layer.borderColor = UIColor.whiteColor().CGColor
You don't have to do this with code either. You can create a stretchable image and set it to the background image of the button in the attributes inspector.
as 0x7fffffff said. UIButton is the class it can be instatiated bu invoking its constructor like this
let instanceOfUIButton = UIButton()
then you can set the desired attributes:
instanceOfUIButton.layer.cornerRadius = 2;
In Xcode 8.2 (Swift 3) you can use "Identity Inspector Tab". Search for "Users Defined Runtime Attributes", after selecting your UIButton. There you can define these attributes:
Key Path: layer.cornerRadius; 2-Type: Number, 3-Value: 2
Key Path: layer.borderWidth; 2-Type: Number, 3-Value: 1
Key Path: layer.borderColor; 2-Type: Color, 3-Value: "select white or another one"
Another option instead of creating a reference to each button would be to create a subclass of the type UIButton. You could then set the properties in the subclass. Next you could change the class of all the buttons in the storyboard that need to have the same properties.
class MyButton: UIButton {
override func draw(_ rect: CGRect) {
super.draw(rect)
layer.borderWidth = 1.0
layer.borderColor = UIColor.White
layer.cornerRadius = 2
}
}
Add this line at the top
myButton.layer.masksToBounds = true
Related
ASAuthorizationAppleIDButton gives a nice looking button by default. And I would like to copy its font style and size for other UIButton. How can I retrieve the font style and size from a ASAuthorizationAppleIDButton after it is set?
I imagine there is a way similar to something like the below
fileprivate let appleButton = ASAuthorizationAppleIDButton()
fileprivate let fbButton = UIButton()
// set appleButton constraint etc etc…
// then,
fbButton.titleLabel.font = appleButton. sth sth…
I ended up making a custom button class with an imageView and lable
Edges of UITextfield get Clipped form all the corners when i set corner radious property.
does anybody knows why this happening?
thanks!!!
my Code:
override func viewDidLoad() {
super.viewDidLoad()
fullNameTextField.layer.cornerRadius = fullNameTextField.frame.size.height / 2
}
result:
i want to achieve this:
Try setting your border style to none and draw your border using in layer:
fullNameTextField.borderStyle = .none
fullNameTextField.layer.borderColor = UIColor.gray.cgColor
fullNameTextField.layer.borderWidth = 1.0
fullNameTextField.layer.cornerRadius = fullNameTextField.frame.size.height / 2
yes buddy its because your text field contains boder style. in interface builder select your text field and choose rounded boder style if you need border.. attaching an image have a look and you can fix it from storyboard :)
I'm trying to complete my first app in Swift and I met with the problem.
I have some buttons with the tittles and background image. Running the simulation on different devices makes them scale, so the tittles goes out of buttons frame.
There is no "Dynamic Type Automatically Adjust Font" checkbox in my Xcode attributes inspector so I made the custom UIButton class and made the inspectable var
#IBInspectable var adjustFontSize : Bool {
set { titleLabel?.adjustsFontForContentSizeCategory = newValue }
get { return titleLabel!.adjustsFontForContentSizeCategory }
but this does't helps and I got "Automatically adjusts font requires using a dynamic type text style" warning
So how can I make my button title scale to fit the Button frame when the button changes size and proportions on different devices?
This worked for me.
btn.titleLabel?.minimumScaleFactor = 0.1
btn.titleLabel?.numberOfLines = 1
btn.titleLabel?.adjustsFontSizeToFitWidth = true
Try these
button.titleLabel!.numberOfLines = 1
button.titleLabel!.adjustsFontSizeToFitWidth = true
button.titleLabel!.baselineAdjustment = .alignCenters
but for them to work, you will have to set button width constraint either in storyboard or programatically.
This will then make the title size change based on the width of the button.
I use defined runtime attributes in button.
layer.cornerRadius
layer.masksToBounds
layer.borderWidth
And I want to paint my border in green colour. But my code doesn’t work:
layer.borderUIColor
Borders have black colour. How to paint border in colour with runtime attributes?
Actually, you are using wrong attribute.The, correct attribute is layer.borderColor.
But again it will not work because it is type of CGColor and from IB we can only assign UIColor, we can't assign CGColor.
Eihter you can simply do it programaticlly.
Or
You can create extension with type CGColor.
Order also matters for runtime attributes.
I use following and it worked for me:
Xcode 13.4, MacOS 12.4, 07082022
This is the answer that creatively solved the problem without requiring a computed property. It works with layer.borderColor in the User Defined Runtime Attributes to add a border color to any, UIView.
Łukasz-kalbarczykenter
http://stackoverflow.com/a/46215487/1058199
It traps the setting of each runtime attribute and traps for the key name of borderColor. If we have a match, it returns the cgColor value for
borderColor from the supplied UIColor.
Here is my updated answer from the work of Łukasz-kalbarczykenter
All I changed was set the extension to CALayer from UIView and made sure to use self.border
import UIKit
extension CALayer {
open override func setValue(_ value: Any?, forKey key: String) {
guard key == "borderColor", let color = value as? UIColor else {
super.setValue(value, forKey: key)
return
}
self.borderColor = color.cgColor
}
}
Next, in the storyboard, just select your UIView, press command option 4 and just add layer.borderColor as your desired UIColor to the User Defined Runtime Attributes. Then run your app and it should work!
you should use : layer.borderColor and set layer.masksToBounds = false
This will work for sure.
you can manage this through storyborad as you are doing but you are passing the wrong key here. it should be
layer.borderColorFromUIColor
For those that don't know what I'm talking about, Xcode 6.0 added new features, IBDesignable and IBInspectable.
When you tag your custom views with IBInspectable properties, those properties show up in the Attributes Inspector in IB.
Likewise, when you tag a custom UIView subclass with IBDesignable, Xcode compiles your views and invokes the code to render your view objects right in the Xcode window so you can see what they look like.
The technique for adding IBDesignable and IBInspectable attributes to custom views is pretty much identical in Swift and Objective-C. IBInspectable properties appear in the Interface Builder Attributes Inspector regardless of which language you use to define them.
I've created a category of UIView in Objective-C and an extension of UIView in Swift that promote the borderWidth, cornerRadius, borderColor, and layerBackgroundColor properties of the view's underlying layer as properties of the view. If you change the property, the extension/category does type conversion as required and forwards the change to the layer.
The IBInspectable part works great. I see and can set the new properties in the IB attributes inspector.
I could have sworn that last week, the IBDesignable attribute on my view category/extension was working too, and I could see my custom UIView category rendering in IB with it's changed layer attributes. This week it isn't working.
Was I hallucinating?
Can categories/extensions of existing system classes draw their custom UI in Interface Builder when they are set up with IBDesignable?
Since posting this question I've learned that #IBDesignable does not work for class extensions. You can add the tag, but it has no effect.
I was able to make it work with code below, but the side effect is that some times IB agent in storyboard crashes because it has to refresh too many UI elements. Restarting Xcode fixes problem temporarily until next crash. Maybe that's the problem OP is facing
#IBDesignable
extension UIView
{
#IBInspectable
public var cornerRadius: CGFloat
{
set (radius) {
self.layer.cornerRadius = radius
self.layer.masksToBounds = radius > 0
}
get {
return self.layer.cornerRadius
}
}
#IBInspectable
public var borderWidth: CGFloat
{
set (borderWidth) {
self.layer.borderWidth = borderWidth
}
get {
return self.layer.borderWidth
}
}
#IBInspectable
public var borderColor:UIColor?
{
set (color) {
self.layer.borderColor = color?.cgColor
}
get {
if let color = self.layer.borderColor
{
return UIColor(cgColor: color)
} else {
return nil
}
}
}
}
That's why I am trying to add where clause to reduce subclasses which should extend this functionality:
Generic IBDesginables UIView extension
#IBDesignable work with UIView extension only in custom class.For example. UILabel is a default sub-class of UIView. It won't work there, but if you make a custom class called MyUILabel subclassing UILabel. assign the MyUILabel class to the Label your are working on. Then your corner radius in UIView extension will work of this MyUILabel.
( I guess the first week it work for you is because you are dealing with some custom class.)
I've made this work for my use case by having one #IBDesignable UIView that I set as the top view in my view controller. My particular use case is making ClassyKit styling visible in Interface Builder on the default UIKit views without have to subclass just for that and it's working great.
Here's an example of how you could set it up:
// in Interface Builder set the class of your top view controller view to this
#IBDesignable class DesignableView: UIView {
}
extension UIView {
open override func prepareForInterfaceBuilder() {
subviews.forEach {
$0.prepareForInterfaceBuilder()
}
}
}
extension UILabel {
// just an example of doing something
open override func prepareForInterfaceBuilder() {
layer.cornerRadius = 8
layer.masksToBounds = true
backgroundColor = .red
textColor = .green
}
}
This code block is working well for me.
import UIKit
public extension UIView {
#IBInspectable public var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
NOTE It might not work when being imported from a framework. I am trying to find out the reason now.