Base Internationalization doesn't work in custom class - ios

I create a subclass of UILabel, in which I set font for its text(and this is the only thing I do in this subclass):
[self setFont:[UIFont fontWithName:#"mplus-1c-regular" size:14.0]];
I use Base Internationalization to do i18n for my project, this is the related translation in my storybaord.strings:
"kjN-FS-SlD.text" = "進步來自精準分析。";
If I set this subclass as the custom class of this label in interface builder, this translation doesn't work. It shows English text, which is my default language of base internationalization, just like my system language is English. However, if I don't set the custom class(leave it blank), it works as normal.
How can I make it work for my custom class?

Related

iOS - Dynamic Type and Interface Builder

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.

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...

Change System font for testing purposes

I'm currently working on an iOS project which has tons of labels/buttons/controls spread over dozens of scenes.
Most of those controls were created using Interface Builder.
So, it's now my job to make sure that every control (especially the labels) is formatted with the correct font family, which is not the case at the moment because many devs simply forget to change the font (our font must be set programmatically since it's not included in IB) after adding the control in IB.
Is there a way to change the system font temporarily so it's easier to see where font-settings have been forgotten?
I've searched for:
Changing the system font programmatically
Changing the font in Xcode somewhere
Changing the font in the iOS simulator (maybe as a debugging option)
But I was unsuccessful so far. I can't be the only one with this kind of problem - it's just naturally tedious to set every single control font programmatically.
The only thing I could imagine is like overriding the base UILabel's drawing method with a custom font (wingdings anyone?), but that seems a bit excessive?
You can try creating a category, which overrides the systemFontOfSize: method of UIFont or use method swizzling (you can find out more about method swizzling here: http://cocoadev.com/MethodSwizzling). Both are extremely ugly and shouldn't be used in production, but should be fine for testing purposes.
Here is an example category of UIFont:
#interface UIFont (SysFont)
#end
#implementation UIFont (SysFont)
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:#"YourFont" size:fontSize];
}
#end

Subclassing vs Category with Interface Builder

I have read multiple times that we should not subclass a component (a UIButton for example) :
Why shouldn't I subclass a UIButton?
Subclassing a UIButton
The problem is when I use Interface Builder.
For example, I have a button with a precise appearance in a lot of my views. I can set them each time with IB (it's painful), or I can use a custom class to factorize the custom behavior and appearance.
It seems a bit contradictory to me that the only way to simplify the process with IB is to do it the way that everybody recommends against.
Is there a better solution ? Can I use a category with IB ?
Thanks.
You might be able to use the UIView appearance proxy. I don't know what all you're doing to your buttons but this might help:
Put this is your AppDelegate file in the application:didFinishLaunchingWithOptions: method
if([UIButton conformsToProtocol:#protocol(UIAppearanceContainer)]){
[[UIButton appearance] setBackgroundImage:[UIImage imageNamed:#"YourImage"] forState:UIControlStateNormal];
//modify any other UIButton properties you want to set globally
}
The second link you provided was pretty clear, and this is pretty much what apple itself states, subclass, but never mess with the internal structure.
Best example is iOS 7, now things are completely different and, for example, an application I'm maintaining had a subclassed UIControl, and now it has trouble running on the new iOS, simply because, it was built with assumptions on how the internal structure works (iterating the internal subviews replacing some things). You might not get your app rejected, but it will be a pain in the a** to maintain.
As a rule of thumb, anything you can do to an UIButton from the outside, something like this:
[myButton setBackgroundImage:... forState:...];
[myButton setTextColor:... forState:...];
myButton.titleLabel.font = ...
You can move it to the inside of a custom subclass method:
+ (UIButton*)fancyPantsButton
{
UIButton *button = [UIButton butonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:... forState:...];
[myButton setTextColor:... forState:...];
myButton.titleLabel.font = ...
return button;
}
You can also do this on init or awakeFromNib without problems (and I usually prefer the later).
UIAppearence is also an option, as was suggested by user hw731. Whatever floats your boat, really.
As for the second question, nib files pretty much create instance a class and then fill-in the things it stores using setValue:forKey: when loading (that's why you get an error like "class is not key-value compliant for something" when you screw up a nib), so if something is categorised when the nib is being loaded, then yes, nibs respect categories, as its simply using initWithCoder.. and then filling in the gaps.
And, by the same token, the nib file won't be able to fill-in custom properties, since it doesn't know about them, unless you explicitly add them on the "User Defined Runtime Attributes" in IB (iOS 5 onwards).
Another technique for nibs, is using
#property (strong) IBOutletCollection(UIButton) NSArray *buttons;
And then iterating and customising buttons accordingly (be it via a subclass, category, local method, ...). This method is really helpful if you want just a handful of custom buttons, but not enough to warrant using a subclass.
I don't see any reason that you shouldn't subclass UIButton, especially for your purpose of making configuration with IB easier. Neither of the links you provided explain why you shouldn't subclass, so their assertions don't seem reliable. On the other hand, the presence of UIButtonTypeCustom in UIButton.h gives the impression that the framework authors planned for UIButton subclasses.

Selecting custom fonts through IB in Xcode

I read many tutorials explaining how to add custom fonts to iOS apps.But in every tutorials its done through code.I mean, if i need change the font of 10 labels,i have to write code for each labels.I would like to know is there any method to add custom fonts to my project and then select that font from the storyboard ,i mean from the attributes list of label object..
Please help me in solving this....
That cannot be achieved through the interface builder only. However you can create a custom class subclassing UILabel. You define the .m file as follows:
#implementation CustomLabel
-(void) awakeFromNib{
[super awakeFromNib];
self.font = [UIFont fontWithName:#"<your custom font's file name>" size: self.font.pointSize];
//set other settings of the custom label here (colour, etc.)
}
#end
Then in the .xib file whenever you use a UILabel, set the class to be CustomLabel.
This can be done in Storyboard or Interface Builder with the use of very simple categories and user defined runtime attributes.
Something like this:
Please check this response for more details on how to achieve it.
Try This One:
UIFont *font = [UIFont fontWithName:#"YourFontName(without Extension)" size:30.0f];
self.yourLabel.font=[font fontWithSize:28];
add your font name in your projectname.plist like this
Fonts provided by application ---->Array------>2(items)
item0 --------->string----->urfontname with extension(ttf/otf) also
item1 --------->string----->secondfont name with extension(ttf/otf) also
This could now be done through Interface Builder in Xcode 6.
Now when you add custom font to your project and after making sure it is properly setup in your target settings, you should see your font in the regular font selector. Simply select Font > Custom, Family > your custom font should be listed here.
I currently have a bug after selecting the font in beta5 but this is how to do it. I installed Xcode 6 only for this feature (far more convenient than defining it programmatically everywhere).

Resources