Pages

Friday 24 April 2015

User defined run time attributes

User defined run time attributes are one of the hidden(likely) gems of the XCode which helps you to customise/configure your UI object in the interface builder itself, without having to write code.

The "User defined run time attributes" uses the Obejctive-C's already existed concept called Key Value Coding. With the Xcode 6, this is a game changer, see here IBInspectable and IBDesignable

Key value coding is a mechanism which helps us to access the properties of an object indirectly by name (or key, which is a string) rather than directly using the accessor method.

That means, say for example you have the property text for the UILabel, you can use the user defined runtime attributes for changing its value.



Wait, who would do that? The UILabel control already have the IB properties for giving the text.
But, think of some other properties that the IB doesn't have. You can do that in the u.d.r.a (ah.,, user defined run time attributes, I am tired of typing this).

For example, here I have a custom class called CustomLabel which is a subclass of UILabel which have a property called fontName. When I give string here, my CustomLabel will use this string and updates it font. I can assign the value to this fontName in the Xib itself like this.





The fun part is you can give not only key but also the key paths. (For those who doesn't know the difference between the key and the keypath, text is a key for the UILabel whereas layer.cornerRadius is a keypath for its corner radius)

For example, if you want to change the corner radios of a UIView, you can add a u.d.r.a  with the keypath layer.cornerRadius.



Don't try to give the layer.borderColor in this way, because borderColor needs a CGColor object but you can only give UIColor with the u.d.r.a. If you want to achieve this, you can add a category for the CALayer with a UIColor property and you can write your own logic to change its borderColor.

Note:
  If you give a u.d.r.a to a UI object, that doesn't have a property with that name, you will get a warning from the debugger like this. In the previous Xcode, it will raise a invalid argument exception and crash.




Here is a list of attribute types that you can use in the u.d.r.a.




For the sake of clarity, I am adding the screenshot of my CustomLabel class here.



Tuesday 14 April 2015

How to change the UITextField's height in the Storyboard/Interface Builder?

When you design your interface for iOS, you can’t change the height of the UITextField directly in xib, when the textfield’s type is UITextBorderStyleRoundedRect. If you need a little taller text field, especially for iPad, either you have to do that in code or you have to change the text field type to some other. But this little trick will help you to do that in xib itself. 


If you are using the constraints, you may specify the height constraint for the textfield.

1. Give some identifier to your textfield, so that you can find your textfield easily in the next step.



2. Right click the storyboard/xib file, select Open As -> Source Code. The storyboard/xib is basically an XML, so its attributes can be edited directly. 



3. Search your textfield, and change its height attributes. I have given 40 for my textfield here.
<rect key="frame" x="195" y="152" width="211" height="40"/>



4. Now if you open your xib as Interface Builder - Storyboard file, by following the step 2, you will find your textfield’s height bigger.


Here is the video if want to take a look.






So easy as it looks like. You can also edit any attributes of any controls like this. But, make sure you don’t break the file format.

Besides, I am wondering why apple’s HIG don’t allow to change the text field height.