31

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?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • Please checkout these links : - http://stackoverflow.com/questions/8716446/uitextview-issue http://stackoverflow.com/questions/9392219/outline-to-uilabel-text – IronManGill May 20 '13 at 13:38

3 Answers3

68

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

Dan Morrow
  • 4,433
  • 2
  • 31
  • 45
  • 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 '13 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 '13 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 '13 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 '14 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: http://nshipster.com/ibinspectable-ibdesignable/ – Dan Morrow Mar 17 '15 at 03: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;
NANNAV
  • 4,875
  • 4
  • 32
  • 50
  • the question is how to do that in storyboard. – geekay Sep 27 '14 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) – Timothy Tripp Apr 03 '20 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.

hypercrypt
  • 15,389
  • 6
  • 48
  • 59