iOS - Dynamic Type and Interface Builder - ios

Trying to implement support for Dynamic Type and have an issue. I set the style I want to use on a label or something in Interface Builder. I register for the UIContentSizeCategoryDidChangeNotification, and in the handler, I set the label's font to ... what? How do I know what style to use? Shouldn't there be an accessor that lets me find that out? If not, I have to put it in 2 places, which means they'll get out of sync and I'll be annoyed. Any thoughts?

I don’t think this will satisfy you, but set the font to [UIFont preferredFontForTextStyle:UIFontTextStyleTitle2 or whatever style you set in Interface Builder.
Ignore the setting in Interface Builder. It’s not even worth setting. Interface Builder is a (mostly) static representation of the initial state of your views, but this is Dynamic Type.
You could subclass UILabel to make it dynamic, and/or join us on the dark side of setting up views in code.

Since iOS 10, there's no need to follow this rationale because the adjustsFontForContentSizeCategory property allows an automatic scaling of the font sizes according to the content type size selected in the settings.
All the text styles are well defined in the Apple reference site and their size variations as well.

Related

Storyboard user defined runtime attributes with enum

Does anyone know if it's possible to set user defined runtime attributes in Xcode's storyboard with an enum's name (not value).
I tried String and Number types, neither of them works.
Setting it programatically is an option, I know.
In other words, for example I want to able to set the textAlignment attribute of UILabel to NSTextAlignmentCenter without knowing NSTextAlignmentCenter's real numeric value (1).
It is possible only if Xcode provides you with interface for that. For example, there're buttons to switch between NSTextAlignment for UILabel.
And, no, there's no way to set any enum values - as long as Xcode doesn't provide functionality like that for editing property lists.
It will become possible, when property lists will support enums

Custom font with size classes in iOS

I'm trying to add 2 different font sizes for iphone and ipad layouts using size classes. It works cool with a default System font but doesn't work with custom font(I'm using PragmataPro in my project). If I add the second size for wR hR then font looks correctly in interface builder(I even checked xml) but in simulator and on device it becomes System instead of PragmataPro. But if I remove wR hR(or whatever layout I'm using for another size) then font shows correctly. Any idea how to solve this issue?
Thanks!
Subclass UILabel and override "layoutSubviews" method like:
- (void)layoutSubviews
{
[super layoutSubviews];
// Implement font logic depending on screen size
self.font = [UIFont fontWithName:#"CustomFont" size:self.font.pointSize];
}
Follow the link (it is a step-by-step from Apple):
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/ChangingtheFontforaSizeClass.html
The 'custom' is there to define one single setting per type class.
If you don't define a 'custom' library with all the variations you want than I don't really think there to be a solution, mainly because by 'custom' you mean ONE SINGLE COMPONENT OF A GIVEN LIST OF CHOICES...

why is UIFont.LineHeight read-only?

Any idea how to set line height in MonoTouch?
UIFont.LineHeight is read-only and has got no setter.
MonoTouch: 6.0.8
It's an Objective-C readonly property, see Apple documentation.
What you need to do is create a new UIFont instance, with a size that will match the line height you want, and assign (or use) that font instance in your code.
Some time ago I needed to change line spacing, but keep font size. Maybe this solution is a dirty hack, but it is simpler than writing your own UI components or use third-party libraries.

Is there a way to see Core Graphics drawings in Interface Builder?

Making UI programmatically is very powerful.
But is there a way I would have missed to see for instance CG Graphics drawing in IB without to have to compile the code ?
I guess no, but I'd prefer to ask to refer that after :)
Nop, how would IB know what to draw without compiling the code?
The new answer to this question is: Yes.
With Xcode 6, you can make Interface Builder render your custom views. In addition, you can also make Interface Builder-inspectable properties on custom views that you can set from Interface Builder just like properties of built-in views.
All you need to do is add the line IB_DESIGNABLE just above the class declaration in the header. This will make Interface Builder render your custom view by invoking its drawRect: method.
If you also want the inspectable properties, just add IBInspectable to the property declarations such as: #property IBInspectable float myValue, and you will be able to set that property directly from Interface Builder.
With the combination of those two, I am currently designing a custom view and by changing the properties (color, stroke width etc) I can see the effects in realtime, which is probably just you were asking for.
It came a bit late, but finally, it's here.

Using constants in Nib files

I have an App with few Nibs, the Nibs are built according to a color scheme. Now I have a requirement to change the color scheme. So I need to go to each Nib, and each component in it and change its color.
I was wondering whether I can tell the Nib to read the color from a "#define" so it would be easy to make these kind of changes in the future.
Or any other way to change the Nib content not by going to each Nib and changing it but by doing it is a central place.
There is currently no way to use constants or defined symbols inside the context of a Nib file. You should consider filing an enhancement request with Apple at http://bugreporter.apple.com for this functionality.
Wouldn't be suitable to set IBOutlet for each element that required color customization and then set color in code?
Even after creating the nib with some other colour, you can still change its the background colour's value in lets say the viewDidLoad function or in any other function to the #define-ed value.

Resources