1

I have a subclass of UILabel with a property lineSpacing setup like so:

var lineSpacing = CGFloat(20.0)

When I have this here it pushes up the text, even though it isn't used for anything, I removed all the code except for that property:

class CustomLabel : UILabel {
    var lineSpacing = CGFloat(20.0)
}

and it is still pushed up, I changed the name of the property and it fixed it, I was just wondering where this is coming from as I can't access lineSpacing from a UILabel

regularLabel.lineSpacing

gives an error.

JAL
  • 41,701
  • 23
  • 172
  • 300
richy
  • 2,716
  • 1
  • 33
  • 42
  • Also observed here: http://stackoverflow.com/questions/32008465/linespacing-property-inside-uilabel-doesnt-work-as-expected – Martin R Jun 08 '16 at 18:06

2 Answers2

5

Yes, UILabel has an undocumented, non-public lineSpacing property. You can see it in this header file that was created from the runtime Objective-C metadata:

https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UILabel.h

Since it's not public, overriding it (even accidentally) may cause Apple to reject your app if you submit it to the App Store.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

Instead of implementing your own line spacing code (or if you really wanted to override that property), you could use the built in private property by creating a protocol exposing the property:

@objc protocol UILabelPrivate {
    var lineSpacing: Int {get set}
}

let label = UILabel()
let privateLabel = unsafeBitCast(label, UILabelPrivate.self)

privateLabel.lineSpacing = 2

label.valueForKey("lineSpacing") // 2

This effectively creates an "interface" exposing the normally private lineSpacing property.

JAL
  • 41,701
  • 23
  • 172
  • 300
  • 1
    It would be simpler to just use KVO: `label.setValue(2, forKey: "lineSpacing")` and less likely to trigger App Store rejection. – rob mayoff Jun 08 '16 at 20:41
  • Wow more than I needed, the property was called line spacing due to the spacing of drawn lines in the view and their spacing. I guess this might be confusing in a Label view. – richy Jun 08 '16 at 20:46