32

I want to set the border of a UITextView or a UILabel in a Storyboard. Can it be done?

Programmatically, it is setBorderColor and setBorderWidth.

But can the border be set in a Storyboard?

1

3 Answers 3

69

As was previously pointed out, these properties are part of a layer, not part of a view. But you can still set their values in IB. As hypercrypt pointed out, you can use User Defined Runtime Attributes. Since all views have a "layer" property, you can set "layer.borderWidth" for instance.

Here's a case, where I'm changing the cornerRadius. Works great.

enter image description here

5
  • 4
    OK. addendum. You cannot set the borderColor this way. But you can set the borderWidth (and cornerRadius). Don't know why this is, unfortunately. Perhaps when you set a color as an attribute, it's looking for a UIColor, but CALayer wants a CGColor?
    – Dan Morrow
    May 20, 2013 at 15:57
  • This worked. And layer.cornerRadius DID ALSO WORK ok. I'm using xcode 4.6.2 and tested it on iPhone 5 and simulator.
    – Doug Null
    May 23, 2013 at 15:14
  • @DanMorrow Yep, when you set a User Defined Runtime Attribute as type 'Color', it means a UIColor, not a CGColor. You can still get the behavior you want (being able to set border color from Interface Builder), but you'll need a custom UIView subclass with a UIColor property that you set from the storyboard and code that applies that color to the border, and then you'll need to nest your views in the storyboard inside instances of that subclass. Whether this is more hassle than it's worth is left as a question for the reader.
    – Mark Amery
    Aug 14, 2013 at 11:04
  • 2
    @MarkAmery Actually you don't need to subclass UIView, you can also accomplish this functionality with a category. Define a UIColor property in the category interface, implement the setter that sets self.layer.borderColor = inputColor.CGColor, and implement the getter to return [UIColor colorWithCGColor:self.layer.borderColor].
    – dmur
    Mar 13, 2014 at 19:27
  • 1
    and building on @dmur , you can now use IBDesignable, to actually draw what it looks like in Xcode 6. Check it out: nshipster.com/ibinspectable-ibdesignable
    – Dan Morrow
    Mar 17, 2015 at 3:15
7

use simple code in .m,it show border in view

view.layer.cornerRadius = 5.0f;
view.layer.masksToBounds = NO;
view.layer.borderWidth = .5f;
view.layer.shadowColor = [UIColor orangeColor].CGColor;
view.layer.shadowOpacity = 0.4;
view.layer.shadowRadius = 5.0f;
2
  • the question is how to do that in storyboard.
    – geekay
    Sep 27, 2014 at 10:36
  • I know this is an old topic but it came up in MY search, so the solution here is to set the User Defined Runtime Attribute "layer.borderWidth" to a numeric value (in points, so 0.5 would be 1/2 point) Apr 3, 2020 at 21:48
4

If you're targeting iOS 6+ you can use the User Defined Runtime Attributes in the Identity Inspector to set any properties. Performance is not an issue for either, so it doesn't matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.