I have inherited a project and i'm confused on what a certain key is.
My question is, what is the styleName key Path ? is it a property to the view ? How can i find out what key Paths are available ?
For example, after i select a UILabel from the storyboard i check the identity inspector and in the user defined runtime attributes i see the following:
I have tried opening the main-styles.plist file but not sure how its linked together.
when i click on the attribute inspector (while still keeping the UILabel in the storyboard highlighted) this is what it looks like:
There is an NSKeyValueCoding protocol, which many of the objects within UIKit conform to.
One of the methods within NSKeyValueCoding is valueForKey: (and many other relevant methods, check the documentation I linked).
By calling valueForKey: on an object, we can, at run time, access properties that were set on the interface builder.
So, for example, on this label, I might do something like this:
Objective-C:
NSString *style = [myLabel valueForKey:#"styleName"];
Swift:
let style = myLabel.valueForKey("styleName")
Now I can grab the value set through the Interface Builder and at run time, I can do something with the label based on what value was set here. For example, here, I might use the particular "style name" to design the label in a particular way.
If you search the project for valueForKey or "styleName", you will likely find where this property is being used and what's being done with it exactly.
To follow up about my question regarding the Attribute Inspector, as of Xcode 6, we can use the #IBInspectable property to create properties which will show up in the Attributes Inspector (as seen here). Consider this UIView extension:
extension UIView {
#IBInspectable var borderColor : UIColor? {
set (newValue) {
self.layer.borderColor = (newValue ?? UIColor.clearColor()).CGColor
}
get {
return UIColor(CGColor: self.layer.borderColor)
}
}
}
Now if we take a look at the Attributes inspector for any UIView (or subclass) in our storyboard, we'll see this:
We now have a "Border Color" property available via the Attributes Inspector which isn't ordinarily there. The reason I point this tool out is because whenever you set one of these properties via the Attributes Inspector, the value you set is actually stored as one of these "User Defined Runtime Attributes":
And whenever this view is loaded from the XIB in my app, one of the first things that will happen is that my borderColor property will be set to this red color I've selected in the Interface Builder.
Below is a list of the available attribute types and the corresponding property type.
Boolean – BOOL (true/false)
Number – NSNumber * or any numeric scalar, e.g. NSInteger
String – NSString
Point – CGPoint
Size – CGSize
Rect – CGRect
Range – NSRange
Color – UIColor
Based on the Apple doc
Use user defined runtime attributes to set an initial value for objects that do not have an interface builder inspector. For example, if you add the following entries in the identity inspector for a custom view:
The custom view will get this message when the nib is loaded:
[customView setValue:[NSNumber numberWithBoolean:NO] forKeyPath:#"isDataLoaded"];
[customView setValue:#"Hatha" forKeyPath:#"excersize.yoga"];
[customView setValue:nil forKeyPath:#"myData"];
Important: The property or key path for the user defined runtime
attribute must exist in the object otherwise an exception will occur.
Because those methods are called when the nib is loaded. So, those runtime attributes can be obtained inside the -(void)awakeFromNib.
For example,
- (void)awakeFromNib
{
// #property (nonatomic) BOOL isDataLoaded, which is assigned by the above `User Defined Runtime Attributes` picture.
BOOL isLoaded = self.isDataLoaded;
}
thanks nhgrif. Actually thanks to your answer which was great so plus one i found whats happening. They created a global category on UIView. its called UIView+mystyle. there they have a method with the following signature:
- (void) setStyleName:(NSString*) styleName
so xcode uses this method without the 'set' and matches it to the runtime key path attribute. in this method they are applying the attribute.
Related
Say you are setting
field
.defaultTextAttributes
.updateValue(20.0, forKey: NSAttributedStringKey.kern.rawValue)
(BTW you do that to space out text, like t h i s.)
in fact, is there a way to set that using User Defined Runtime Attributes,
right on the storyboard in Xcode?
No, there is not. User Defined Runtime Attributes works only where key-value coding would work, with a limited range of value types. A moment's thought will reveal that your code can't be expressed in that way.
You could, however, subclass to define a custom property and set it to 20 in User Defined Runtime Attributes, and respond with a setter observer to run the code you've shown. That way, different fields can have different kern values depending on a setting in the storyboard.
(If you're going to do that, you might as well make this property IBInspectable; IBInspectable effectively is User Defined Runtime Attributes, with a different interface.)
We are in the process of implementing IBInspectable into a large app in hopes of allowing some settings to be set in Interface Builder to reduce the amount of code in our views. I don't have much experience with IBInspectable/IBDesignable and am looking for some answers and/or clarification on what I'm doing wrong.
I have declared a property in a UITableViewCell subclass as follows:
#property (nonatomic,strong) IBInspectable UIColor* backgroundColor;
When declaring the property like this, I get an option to set that color in Interface Builder > Attributes Inspector, which is to be expected. However when I set the color, the value for _backgroundColor is nil at runtime.
[_labelLoginBackground setBackgroundColor:_backgroundColor];
Could someone clarify what might be going here? Thanks!
UITableViewCell is a subclass of UIView, which already contains a property named "backgroundColor". Do 1 of the following:
Rename your own "backgroundColor" property to "loginBackgroundColor" and start debugging from there.
OR
Do not create a redundant property. Set the background color using the selection widget that is already present in Interface Builder before you ever add IBInspectable.
I'm using new Xcode 6 feature called "LiveRendering".
As i need to use some inspectable properties a lot on different custom views, i want to declare them in protocols.
For example :
LiveRenderingTextAttributesProtocol (that declares inspectable properties for textColor, textSize
LiveRenderingBorderAttributesProtocol (that declares inspectable properties for borderStyle, borderColor, borderWidth)
etc ...
After that, each custom view implements the protocols it needs.
But i can't see my inspectable properties on InterfaceBuilder "Attributes inspector" column :/ The LiveRendering is working well when I define the values for these properties as "Used Defined Runtime Attributes", but i want to see them in my Attributes inspector column.
An idea to solve this problem please ?
A protocol defines a set of optional or required methods and attributes to be compliant with, but it's the class the responsible for implementing them. To be able to have reusable base IBInspectable properties you could have a superclass like MyInspectableView that has those properties declared and implemented, so you can subclass it and have those IBInspectables shared between all of them.
I am using storyboard and there are 5 view controllers in it. Each ViewController has only 1 UIButton. I want to set button's background color using User Defined Runtime Attributes. So I defined a keyPath "bgColor" and set corresponding hex color code "#ffaa11". But application crashes before loading the view. It gives exception:
[<UIRoundedRectButton 0x713fdb0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bgColor.
I dont want to create custom class. Please suggest how can I paas value through IB. Please check the snapshot.
NOTE: In real my requirement is to get a string value which has been entered in Interface Builder. Very similar to User Defined Runtime attributes. I have taken example of bg color.
Why are you using User Defined Runtime Attributes anyway? Switch to the Attributes Inspector and choose button's background from there.
First thing first.
Obviously it crashes. Because there is no bgColor property on the button. Hence when you are trying to set it as button.bgColor = #yourValue. It is crashing.
If you are using ios7 try setting the button's tintColor property. and for styles go through tintAdjustmentMode values.
And after these . If it still doesn't solve your issue. We will discuss something else :) :)
cheers. Have fun.
Apple's UIAccesibility Protocol reference states:
UIAccessibilityLabel
Discussion
The default value for this property is nil unless the receiver is a UIKit control, in which case the value is a label derived from the control’s title.
Well... both UIView and UIViewController are in the UIKit framework, but I can't find the default accessibility label for my view who's controlled by the UIViewController named LoginVC. I tried to log it, but get a blank string. Is that right? is the doc wrong? Or am I doing something wrong?
I know I can set the accessibility label manually; I'd like to avoid the clutter and use defaults if possible - which are already generally descriptive if their name is actually derived from their controller.
By default, a UIView and a UIViewController have nothing set for their title property, so the default accessibilityLabel property (which is derived from the title property) correctly and expectedly returns nil. Set their title properties (either in code or in Interface Builder) and you'll see it when you log accessibilityLabel at runtime.
I don't think Accessibility will populate itself automatically, if that's what you're asking.
If you asking for ways to set it, you should be able to do it programmatically:
[myButton setAccessibilityLabel:#"Hello"];
If you use Interface Builder, there is a field in the Identity Inspector tab that will allow you to set this. Select your view under Objects on the left. Then click the third tab from the left for Identity Inspector. Example below: