Swift3 different font in the All of the UIView with Localization each
As I can see, there you have 3 tasks to do.
- Map font name with current language code
- Get regular font from the name
- Get bold font from the name (optionally, but spotted from your code)
1. Map font name with current language code
In order to do that you need to get that code and write a switch statement.
It can look like this (may vary depending on your needs)
func languageCode() -> String? {
return NSLocale.autoupdatingCurrent.languageCode
}
func localizedFontName() -> String {
let defaultFont = "Arial"
guard let code = languageCode() else {
return defaultFont
}
switch(code) {
case "en":
return "Helvetica"
case "fr":
return "Menlo"
default:
return defaultFont
}
}
Please refer to NSLocale documentation for more info.
2. Get regular font from the name
It can be achieved by using UIFontDescriptor
let DefaultFontSize: CGFloat = 12.0
func font(from name: String) -> UIFont {
let descriptor = UIFontDescriptor(name: name, size: DefaultFontSize)
return UIFont(descriptor: descriptor, size: DefaultFontSize)
}
3. Get bold font from the name
And the last task can be done via UIFontDescriptor
as well.
let BoldFontSize: CGFloat = 12.0
func boldFont(from name: String) -> UIFont {
let regularFont = font(from: name)
guard let descriptor = regularFont.fontDescriptor
.withSymbolicTraits(.traitBold) else {
return regularFont
}
return UIFont(descriptor: descriptor, size: BoldFontSize)
}
Ultimately it all can be used like this:
let nameFont = boldFont(from: localizedFontName())
let nameString = NSAttributedString(string: "username ",
attributes: [NSFontAttributeName: nameFont])
let surnameFont = font(from: localizedFontName())
let surnameString = NSAttributedString(string: "gender",
attributes: [NSFontAttributeName: surnameFont])
Complete Playground can be found here playground.zip
Hope that helps!
Set top alignment for labels with different font sizes, Swift 3
solution by programming:
you may use UILabel's attributed string property to handle this situation.
attribute options have one option named "NSBaselineOffsetAttributeName", it will render text offset the default baseline. and this offset can be measured through UIFont properties
offset = ascender - capHeight
code sample:
class CustomViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
let currencyLabel: UILabel = {
let label = UILabel()
let font = UIFont.systemFont(ofSize: 50)
// measure baseline offset
let offset = font.ascender - font.capHeight
label.attributedText = NSAttributedString(string: "$", attributes: [NSFontAttributeName: font, NSBaselineOffsetAttributeName: offset])
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.blue
return label
}()
let costLabel: UILabel = {
let label = UILabel()
let font = UIFont.systemFont(ofSize: 150)
let offset = font.ascender - font.capHeight
label.attributedText = NSAttributedString(string: "15", attributes: [NSFontAttributeName: font, NSBaselineOffsetAttributeName: offset])
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.green
return label
}()
func setupViews() {
view.backgroundColor = UIColor.red
view.addSubview(currencyLabel)
view.addSubview(costLabel)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[v0]-[v1]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": currencyLabel, "v1": costLabel]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": currencyLabel]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": costLabel]))
}
}
result:
How to set a custom font for entire iOS app without specifying size
- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:@"FagoOfficeSans-Regular" forView:self.view andSubViews:YES];
}
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
}
if (isSubViews)
{
for (UIView *sview in view.subviews)
{
[self setFontFamily:fontFamily forView:sview andSubViews:YES];
}
}
}
Swift 3 - Add Text to UIView Pop up
You can try
popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
let lb = UILabel(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
lb.text="anything"
popup.backgroundColor = UIColor.redColor
// show on screen
self.view.addSubview(popup)
popup.addSubview(lb)
lb.center = popup.center
Swift 3 how to animate font in a button
You can store the grab the original font size and scale it similar to how you do it with the frame and background color:
func animate() {
// Original frame and background color
let originalFontSize = self.playButton.titleLabel?.font.pointSize
// First UIView.addKeyframe()
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.5) {
self.playButton.titleLabel?.font = UIFont.systemFont(ofSize: originalFontSize!*1.2)
// Other animations ...
}
// Second UIView.addKeyframe() to return to normal
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
self.playButton.titleLabel?.font = UIFont.systemFont(ofSize: originalFontSize!)
// Other animations ...
}
Set the separator(UIView)'s color based on the date and text in Swift 3
i think you are comparing string objects that's why this is not working try this :
let today = Date()
let headerDate = "18 May 2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy" //Your date format
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
//Current time zone
let date = dateFormatter.date(from: headerDate) //according to date format your date string
print(date ?? "")
if today < date{
cell.separator.backgroundColor = UIColor(red: 1, green: 0.8314, blue: 0.1569, alpha: 1)
} else if cell.jobDetail.text == "Cancel" {
cell.separator.backgroundColor = UIColor.red
}
Related Topics
Swift Pattern Match on Array<Any>
Execute Code on Main Thread from Async F#
How to Cut a Hole in a Sprite Image or Texture to Show What Is Behind It Using Spritekit in Swift
Firebase Authentication: Linking Multiple Accounts in Swift
Msmessagelivelayout Freeze/Crash in Transcript When Info.Plist Contains Privacy Request
How Pass Data from Button in Tableviewcell to View Controller
How to Detect Encoding in Data Based on a String
Choppy Animation Swiftui Nested Views
How to Make Phone Calls in Swift
How to Programmatically Scroll iOS Wkwebview, Swift 4
Macos Swift UI View Where There Is a Search Field in The Title Bar
How to Have a Searchbar Which Shows Suggestions with Different UItableview
Validatemenuitem or Menuwillopen Not Called for Nsmenu
Spacing Between Sections in a Form
Wkwebview on Macos Cuts Off Top