erichoracek / Motif

Lightweight and customizable stylesheets for iOS
MIT License
878 stars 64 forks source link

Swift: UIEdgeInsets property applier #48

Closed jsslai closed 9 years ago

jsslai commented 9 years ago

How do I add property applier for UIEdgeInsets in Swift? If I understood correctly, Objective-C version of property applier for UIEdgeInsets would be something like this:

[self mtf_registerThemeProperty:@"titleEdgeInsets" requiringValueOfObjCType:@encode(UIEdgeInsets) applierBlock:^(NSValue *value, UIButton *button) {
        button.titleEdgeInsets = value.UIEdgeInsetsValue;
}];

But Swift doesn't have @encode compiler directive.

erichoracek commented 9 years ago

@jsslai I'm hoping to really improve the Motif Swift API in 0.3, but in the interim you should be able to use something along the lines of:

let objCType = NSValue(UIEdgeInsets: UIEdgeInsetsZero).objCType
self.mtf_registerThemeProperty("someEdgeInsets", requiringValueOfObjCType: objCType) { (value, view) -> Void in
    // Apply your value
}

Let me know if this doesn't work, thanks!

jsslai commented 9 years ago

Thanks, it worked. I also had to change the theme.json property style for edge insets from "{0, 0, 0, 0}" to [0, 0, 0, 0]. I thought the JSON syntax was still the same...

erichoracek commented 9 years ago

@jsslai The JSON syntax is roughly the same—the string transformers for C structs were just deprecated in place of dictionary, array, and number transformers. So now, you can declare your edge insets as:

"insets": [1, 2, 3, 4]
"insets": {"left" = 10, "right" = 20}

or

"insets": 10

Which will give you an edge insets with all elements set to 10. Hopefully that's an improvement over the old string way of doing things!

jsslai commented 9 years ago

Did you also change the order of insets? Now [1, 2, 3, 4] gives:

(UIEdgeInsets) $R1 = (top = 1, left = 4, bottom = 3, right = 2)

erichoracek commented 9 years ago

@jsslai Yes—it now follows a similar pattern to that of CSS padding/margins. See https://developer.mozilla.org/en-US/docs/Web/CSS/padding#Formal_syntax