Reset Appearance for button font conditionally - ios

My App supports two languages. Language can be changed within the app. For buttons I have set font through appearance property in app delegate. I need two different fonts based on language. I am setting it like,
if language == "en" {
UIButton.appearance().titleLabel?.font = UIFont.systemFontOfSize(18)
} else {
UIButton.appearance().titleLabel?.font = UIFont(name: "Cairo", size: 18)!
}
When language is changed then I am invoking above code but it looks like it sets to the one which gets called first time at the time of app launching.
I followed few threads & it suggests to handle it manually on each controller for relevant UI components. But looks quite lengthy work.
Any clues to sort out my issue where I can set appearance based on my language?

Related

How can i make customizable views on IOS ? (i used style.id as a parameter on android view class constructor)

First I want to talk about what I exactly did on Android, and I am now trying to do the same on IOS :
I developed a library on Android which contains an activity, in that activity we have several type of views (labels, buttons ... etc).
These views are used as an entry point/inputs for my library that provides some services to other apps.
To be able to match the general style of the user app, I created my own View Classes, the user(of my library) needs to provide a style id to the activity before launching it, so it can be used when constructing the View.
Doing so, we are able to maintain the style of the app without the need to re-implement the views for every app that uses my library or making the user(final user on physical device) feels as if he just moved out to another app.
I am really a newbie on iOS and I recently started to learn it so i am not sure how to do it, can you please provide me some guidelines please?
On iOS there are so many ways to create views (xibs, Storyboards , SwiftUI, and programatically). I am not really sure where to start.
He's a sample of my code on Android :
class MyView(val context: Context, val themeId: Int) : View(context) {
init {
val wrappedContext = ContextThemeWrapper(context, themeId)
val mInflater = wrappedContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
mInflater.inflate(R.layout.my_view_layout, this, true)
}
}
This is a very broad question, but the short answer is "sure, it can be done."
I suppose a quick example might be:
I have a custom UIView with a UILabel centered in it, with 20-pts padding on all 4 sides.
I have 4 "themes" defined, defining the view's background color, the label's text color and the label's font style.
I write a custom init func to accept an Int "id" of 1 through 4
The library user could then instantiate it along the lines of let myView = MyView(with themeId: 2)
As this is a very broad question (topic), once you start developing your custom view you could come back if you run into specific coding questions.

UITextView Font reversing back to System Default

I have set up a custom font for the UITableView overall in my app. I wrote this in my AppDelegate
UITextView.appearance().font = UIFont(name: "Avenir", size: 14.0)
So that the font of all of the UITextView's changes to that custom font.
So when I load my app, the font appears as system font in the home page, but everything else has the custom font, if I go to a different view in the app and I go back to the home page for a brief moment I see the custom font
but then after the background app refresh it goes back to this:
I'm not exactly sure what's going on in here. The other textViews have the custom font, and there's nothing messing around with the front in the background refresh either.
Note: The only font that changes back to system default is the black one, the other ones are labels and are working fine.
Any help would be appreciate it.
It turned out that I had to click on selectable in the UITextView in the Storyboard. It works good now!

Setting other font than system in WatchKit (Apple Watch)

I have one Apple Watch app that is using Menlo Bold font for label.
In first version of Xcode that supported watchkit I was able to change font of label in storyboard to Menlo Bold 36 and everything was working fine.
In curent version (Xcode 6.2) that is not possible any more, so I found this tutorial on how to set custom font in Apple Watch, http://www.tech-recipes.com/rx/53710/how-do-i-use-custom-fonts-in-my-apple-watch-app/
I this tutorial I found this code:
func printFonts() {
let fontFamilyNames = UIFont.familyNames()
for familyName in fontFamilyNames {
println("------------------------------")
println("Font Family Name = [\(familyName)]")
let names = UIFont.fontNamesForFamilyName(familyName as String)
println("Font Names = [\(names)]")
}
}
This code print available fonts.
It is used to see why is the name of the font that you have added.
At this stage I have not added any custom font, but I run this function just to see what font are available on Apple Watch by default.
After running this I found:
/* lot of fonts before */
------------------------------
Font Family Name = [Menlo]
Font Names = [[Menlo-BoldItalic, Menlo-Regular, Menlo-Bold, Menlo-Italic]]
------------------------------
/* lot of fonts after */
It look like that my Menlo-Bold is available on Apple Watch, but I just can not select it in StoryBoard.
So , I was think this is very good, I do not need to add font I will just use it.
This is my code:
// must maee attributed text
let attributedTextForHour = NSAttributedString(
string: "IS IT WORKING",
attributes: NSDictionary(object: UIFont(name: "Menlo-Bold", size: 36)!, forKey:NSFontAttributeName))
// with atributex text
mylabel.setAttributedText(attributedTextForHour)
When I run this code in simulator, font of the label is not changed.
And here are some other strange things:
in debugger, when I set break point on mylabel.setAttributedText(attributedTextForHour) and do quick look on attributedTextForHour it is showed in correct font.
So in debugger it is fine, but on simulator it is not.
So how to fix this ?
I do not have Apple Watch, so I can see what is happening on real device.
So, I do no know is there some problem in my code or this is bug form Apple.
If somebody can test this on the is computer and report results, that will be useful also.
Maybe whole problem is because I have not added Menlo Bold font from outside and I am using what printFont() function has shown.
So if anybody know where I can download Menlo Bold font, please provide link, so that I can test that also.
I manage to solve this problem, so I am posting for future generations.
There are few things that I have learned from this (after 3h of work)
Font that are by default printed by printFonts() they look-like that are available but they are not.
you can add your *.otf font by following http://www.tech-recipes.com/rx/53710/how-do-i-use-custom-fonts-in-my-apple-watch-app/ and then you can just select it in Storybord by custom font, so you do not need to make NSAttributedString
If you want to add font that is already printed in printFonts() that things become complicated.
You can not add it by same name, so you must add it with different name, I called it Menlo-Bold-My.otf, and you must change some ID of font.
So Menlo-Bold-My.otf is same as Menlo-Bold.otf from Apple.
For converting, manipulating fonts I used program http://fontforge.github.io/en-US/

How does waze change objects location with their localization?

The first image is when the language is set to Hebrew, the second is in English. Look at the word " Thanks: " in the image. How Waze managed to change the location of the word to a Right To Left language? With regular localization
Thanks:x
should have been
תודות:X
That's just one example. Waze does that a lot.
Waze supports iOS 4.3 and later so AutoLayout is out of the question. With regular localization I know you can change the content of the word but location? that's a first.
Can I set an if iphone language is English then change the layout to..? (if so that's a lot of ifs.."
Thanks
Frankly, I do not know if there is any standarized way of doing that.
But I see several options:
1. Using localized .xib files or even full storyboards.
2. Changing that programmatically. Get the current localization and add some IFs to the view controller and re-arrange the items. If this is in a custom UITableViewCell subclass then overwrite the layoutSubViews (or so) and change the layout accoring to the locale settings. If you layout it within cellForRowAtIndexPath then apply the localization there.

Change language for a loaded tabbarController in ios

I want to change the language of the loaded tabbar controller from the app when the user selects the language from the tableview
You should rather use the language the user applied to the whole OS. (link)
Apple provides a nice macro called NSLocalizedString, you might want to check that out.
Example:
// Localizable.strings (German)
"Edit" = "Bearbeiten";
// code
label.text = NSLocalizedString(#"Edit", nil);

Resources