0

In my app I have a custom view containerView in this view there are more than 20 labels and I want to apply a border style to all of them.

Is there a way to avoid to add the border to each of them avoiding to have a long list?

Something similar to:

for each label in containerView {

   labels.layer.borderColor = UIColor.greenColor.CGColor

}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
SNos
  • 3,430
  • 5
  • 42
  • 92
  • I'd make custom label `BorderedLabel` with border and make those labels to be of this type. – kostek Apr 02 '16 at 10:47
  • thanks found an easier way using `for loop` – SNos Apr 02 '16 at 10:49
  • 2
    Where did you get Swift 3 ? ;-) – vadian Apr 02 '16 at 10:58
  • IBOutletCollection might be an idea if you are using interface builder – Oliver Atkinson Apr 02 '16 at 11:21
  • See http://stackoverflow.com/a/24066595/669586 – Sulthan Apr 02 '16 at 11:40
  • i do not agree that the for loop is the easier (and more elegant) way than creating a subclass. :) but that is just my personal opinion... if you want to use those labels in different viewcontrollers you have to create a for loop for each viewcontroller for example... – André Slotta Apr 02 '16 at 11:41
  • @AndréSlotta Actually, there is no need for it either, you can just set the `layer ` properties directly in Interface builder. – Sulthan Apr 02 '16 at 11:42
  • @Sulthan sure you can but you have to set it for EACH label. that is not really elegant and maintainable. lets say you want to change the border's width you have to do it for EACH label. – André Slotta Apr 02 '16 at 11:43
  • @AndréSlotta True, thanks god we have `@IBDesignable` now. – Sulthan Apr 02 '16 at 11:45

4 Answers4

2

create a subclass like this:

@IBDesignable
class BorderedLabel: UILabel {

    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.CGColor
        }
    }

    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }

}

then change your label's custom class in interface builder, set the borderColor / borderWidth properties you like and see the results live in interface builder!

André Slotta
  • 13,774
  • 2
  • 22
  • 34
1

You need to set borderWidth.

for subview in self.view.containerView.subviews as! [UIView] {

    if let label = subview as? UILabel {
       label.layer.borderColor = UIColor.greenColor().CGColor
       label.layer.borderWidth = 1
    }

}

Swift 4

label.layer.borderColor = UIColor.green.cgColor
Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • just for reference, the latest version of swift changes that color line to `UIColor.green.cgColor` – mjr Feb 19 '18 at 21:16
1

You can use this:

for view in self.view.containerView.subviews as! [UIView] {
    if let label = view as? UITextField {
           label.layer.borderColor = UIColor.blueColor().CGColor;
           label.layer.borderWidth = 1;
        }
    }
}
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • Thanks! Even better then my solution as it won't crash if there are no image views. However, I found that all the image views are wrapped in a secondary view called 'imageContainerView' – SNos Apr 02 '16 at 11:02
  • However, the code says : Forced cast of '[UIView]' to same type has no effect – SNos Apr 02 '16 at 11:18
0

My solution is:

for imageViews in self.containerView.subviews as! [UIImageView] {

            imageViews.layer.borderColor = UIColor.greenColor().CGColor
            imageViews.layer.borderWidth = 1

        }
SNos
  • 3,430
  • 5
  • 42
  • 92