iOS Storyboard localizable strings do not work on UILabel subclasses - ios

I'm using the new iOS functionnality to translate the storyboards (http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/InternationalizeYourApp/InternationalizeYourApp/InternationalizeYourApp.html)
The problem with this solution, it does not work with my UILabel subclasses.
Here are the codes for my UILabel subclasses :
.h:
#import <UIKit/UIKit.h>
#interface LabelThinText : UILabel
- (void)awakeFromNib;
-(id)initWithFrame:(CGRect)frame;
#end
.m :
#implementation LabelThinText
- (void)awakeFromNib
{
[super awakeFromNib];
[self setFont:[UIFont fontWithName:#"Raleway-Light" size:[[self font] pointSize]]];
}
-(id)initWithFrame:(CGRect)frame
{
id result = [super initWithFrame:frame];
if (result) {
[self setFont:[UIFont fontWithName:#"Raleway-Light" size:[[self font] pointSize]]];
}
return result;
}
#end
I guess i'm missing something to get the automatic translations from my Storyboard.strings file.
Anyone has an idea ?
Thanks !

I ran into the same problem, and for the same reason, setting a custom font in a label. My strategy was a little more general, though. My custom font is Helvetica-like, so I used Helvetica Neue in IB as a placeholder font. The UILabel subclass translated that to my custom font, preserving font size and weight, so I could control all that through IB.
That made my workaround for the translation bug (I assume it's a bug) easier. I traverse all my views recursively in viewDidLoad, and map all UILabel fonts if they match the placeholder font, and the same for UIButton in my case.
Have you filed a bug?

Encountered the same problem, here's how I got around it:
Drop your custom class, and create a category on UILabel, in your case UILabel+ThinText:
- (void) setThinText:(BOOL)thinText
{
if (thinText) {
[self setFont:[UIFont fontWithName:#"Raleway-Light" size:[[self font] pointSize]]];
}
}
In your storyboard, select your label, choose the Identity Inspector and add the following User Defined Runtime Attribute:
Keypath: thinText – Type: Boolean – Value: checked

I got this issue too. I solve it setting the custom font in each ViewController. Let me show you an example:
CustomViewController.h
#interface CustonViewController
#property (strong, nonatomic) IBOutlet UILabel* someLabel;
#end
The in CustomViewController.m
- (void)viewDidLoad
{
[self setUoFonts];
}
- (void)setUpFonts
{
self.someLabel.font = [UIFont fontWithName:#"AmaticSC-Regular" size:self.someLabel.font.pointSize];
}
And that's it!. You're going to have your translation and your custom font.
Remember to remove the custom class from StoryBoard.

Related

About issue related to IBInspectable in localisation

In my app i set localisation text using IBInspectable string and also change language using manually using button action.
Issue
I can't able to refresh UI while change language manually. I need to refresh IBInspectable string while I change language.
I have achieved using below code :
1. InterFace and Implementation for UIView
#interface UIView (AdditionLocalization)
-(void)applyLocalization;
#end
#implementation UIView (AdditionLocalization)
-(void)applyLocalization{
if ([self isKindOfClass:[UILabel class]]) {
UILabel *lbl = (UILabel *)self;
[lbl setLocalizedText:lbl.accessibilityLabel];
}
}
2. InterFace and Implementation for Label
IB_DESIGNABLE
#interface UILabel(LabelAdditionLocalizations)
#property (nonatomic,copy)IBInspectable NSString *localizedText;
#end
#implementation UILabel(LabelAdditionLocalizations)
#dynamic localizedText;
-(void)setLocalizedText:(NSString *)localizedText{
self.text = [UtilityClass get:localizedText.length>0 ? localizedText : self.text alter:#""];
self.accessibilityLabel = localizedText.length>0 ? localizedText : nil;
}
3.Trigger Manually while button click
- (IBAction)clickChangeLanguage:(id)sender {
[UtilityClass setLanguage:current];
for (UIView *view in self.view.subviews) {
[view applyLocalization];
}
}

Will using appearance proxy on UILabel get my app rejected because it alters UIAlertView?

I'm hoping to customize all of the fonts in my app, and have been using the [UILabel appearance] API so far as it seems like the path of least resistance.
I noticed that this also affects the labels inside of the UIAlertView (as expected), but I'm also aware that customizing the alert view may cause an app to get rejected.
Does anyone have any experience with this? Should I just make my own subclass of UILabel instead and apply it manually to each label in my app?
UILabel class conforms to the UIAppearanceContainer protocol, a check of UILabel.h shows that none of its properties are marked with UI_APPEARANCE_SELECTOR, the prerequisite for the use of UIAppearance. So you need to create UILabel subclass as shown below.
#interface SmallLabel : UILabel
#end
#implementation SmallLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
#end
I think you can alsotry in your subclass something like:
UIFont *newFont = [UIFont fontWithName:#"YourFontName" size:self.font.pointSize];
self.font = newFont

Setting adjustsFontSizeToFitWidth globally for all UILabels

We have a multilingual app in which the size of text in labels can change depending on the language in use. e.g. German uses more space than English in a lot of spaces. In such cases, iOS automatically shrinks the text, substituting the middle letters with ".."
e.g. Cancel becomes Ca..el
It's easy to fix these cases individually by setting the adjustsFontSizeToFitWidth to YES in the label. But it's laborious to do so for every single label out there.
Is there a way one could set it to YES globally for all UILabel instances in the app? I tried using UIAppearance but it seems that it does not support the adjustsFontSizeToFitWidth property.
I think you can add the below code at ViewDidLoad for each ViewController of app:
NSArray *allSub = [self.view subviews];
for (id sub in allSub) {
if ([sub isKindOfClass:[UILabel class]]) {
UILabel *lb =sub;
lb.adjustsFontSizeToFitWidth = YES;
}
}
I think more cleaner way is to do a category for UILabel like this:
#import <UIKit/UIKit.h>
#interface UILabel (FR)
#property(nonatomic) UIBaselineAdjustment baselineAdjustment;
#property(nonatomic) CGFloat minimumScaleFactor;
#end
.
#import "UILabel+FR.h"
#implementation UILabel (FR)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
- (void)didMoveToSuperview {
self.adjustsFontSizeToFitWidth = YES;
self.minimumScaleFactor = 0.5;
[super didMoveToSuperview];
}
#pragma clang diagnostic pop
#end
And include it in your Prefix.h file

iOS 7 - linkTextAttributes on UITextView works badly

As you can see on UITextView class, linkTextAttributes seems to be a new property available from iOS7:
// Style for links
#property(nonatomic, copy) NSDictionary *linkTextAttributes NS_AVAILABLE_IOS(7_0);
and it should color links differently in an UITextView instance. So I tried to put a static (not editable) UITextView in a view controller (child of a tab bar controller), and set this property like below:
#property (nonatomic,strong) IBOutlet UITextView *copyrightText;
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor *linkColor = [UIColor colorWithRed:202.0f/255.0f green:202.0f/255.0f blue:202.0f/255.0f alpha:1];
NSDictionary *attributes = #{NSForegroundColorAttributeName:linkColor};
self.copyrightText.linkTextAttributes = attributes;
}
but at first load, links color seems to be not set. Then, if I switch to another VC and return to current VC, links color changes. What's the problem with this code?
You can try this line of code. I'm always using this with the animation. I think it can help you achieve that view in first load.
------> [self.view layoutIfNeeded];

Styling UILabel using UIAppearance proxy

I have a number of UILabel elements in my app and I'm looking for an easy way to style them using the UIAppearance Proxy object. My current approach is to create different subclasses of UILabel equipped with an UI_APPEARANCE_SELECTOR decorator in order to be accessible though [UILabelSubclass appearance] calls. You can find the sources I'm using as reference here and here.
The problem is that I don't want to set the font size but only the font family relying on the size defined in the Storyboard.
How can I set this in the subclass?
Here's a simple category that works with UIAppearance proxies:
#interface UILabel (FontName)
- (void)setFontName:(NSString *)name UI_APPEARANCE_SELECTOR;
#end
#implementation UILabel (FontName)
- (void)setFontName:(NSString *)name {
self.font = [UIFont fontWithName:name size:self.font.pointSize];
}
#end
I think you can try in your subclass something like:
UIFont *newFont = [UIFont fontWithName:#"YourFontName" size:self.font.pointSize];
self.font = newFont
Thanks to this post, and also a blog post from Peter Steinberger (http://petersteinberger.com/) I was able to get something together that works for me:
#interface UILabel (FontAppearance)
#property (nonatomic, copy) UIFont *appearanceFont UI_APPEARANCE_SELECTOR;
#end
#implementation UILabel (FontAppearance)
- (void)setAppearanceFont:(UIFont*)font
{
[self setFont:font];
}
-(UIFont*)appearanceFont
{
return [self font];
}
#end

Resources