Button title font - ios

btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20)
I know this is the way to change font size of button. But it does not work for me.
Anybody know why it's not working for me ?

Using setAttributedTitle on button instance fixes the problem
let title = "Some String"
let attributedTitle = NSAttributedString.init(string: title, attributes: [.font: UIFont(name:"Times New Roman", size: 20)!])
UIButton().setAttributedTitle(attributedTitle, for: .normal)

Related

UITextField Placeholder and Text Size should be separate - Swift

My requirement is such that when placeholder of textfield is visible then its color should be gray and font size 10, but when user starts typing into the textfield, its color should be black and font size 14. I have tried this:
textField.attributedPlaceholder = NSAttributedString(string: placeholderText,
attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!])
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!
But, my placeholder font size is getting overridden by textfield.font, so I am unable to get placeholder of size 10. Where am I going wrong? Tried this for couple of hours now. Any help will be appreciated.
Simply set the placeholder after setting the font since setting the font also applies to the placeholder (see https://developer.apple.com/documentation/uikit/uitextfield/1619604-font):
textField.font = ...
textField.textColor = ...
textField.attributedPlaceholder = ...
Try this code into your viewDidLoad() method:
textField.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!])
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!
Just try this code, take the UITextFieldEditingChanged action outlet and use as below.
#IBAction func nameTextFieldEditingChanged(_ sender: UITextField) {
if sender.text?.isEmpty ?? true {
//placeholder text size set here
textField.font = UIFont(name: "SourceSansPro-Regular", size: 10)!
} else {
// When user starting typing
textField.textColor = UIColor.black
textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!
}
}
If any doubt please comment ].
Just try this code may help you for further:
var myMutableStringTitle = NSMutableAttributedString()
let Name = "Enter Title" // PlaceHolderText
myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!]) // Font
myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:Name.characters.count)) // Color
txtTitle.attributedPlaceholder = myMutableStringTitle

tabBarController navigationItem.title color

I am setting the title as follows, but I want to change the color of the text as well, but there is no textattributes, I wonder how it can be done ?
self.tabBarController?.navigationItem.title = "Settings"
I tried the following as well, but it did not even show the label.
let navLabel = UILabel()
let navTitle = NSMutableAttributedString(string: "Settings", attributes:[
NSAttributedString.Key.foregroundColor: UIColor.blue,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17.0, weight: UIFont.Weight.light)])
navLabel.attributedText = navTitle
self.tabBarController?.navigationItem.titleView = navTitle
try this code, I hope it helps you.
Build Settings\Swift Language Version: 4.1
General\Deployment Target: 10.3
let attrsNormal = [NSAttributedStringKey.foregroundColor : UIColor.black,
NSAttributedStringKey.font : UIFont(name: "Arial", size: 14)!]
UITabBarItem.appearance().setTitleTextAttributes(attrsNormal,
for: UIControlState.normal)
The size of label is right ? Call fitThatSize

Swift change font and color of back button

I am developing an app in Swift 2.2. Now I want to change the back button font and color for a certain view. The view in question has a navigation controller as it's parent controller.
I've tried running both of the following lines in viewDidLoad of my ViewController
self.navigationController!.navigationItem.backBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
self.navigationItem.backBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
Neither throws any errors, but it doesn't make any difference to the back button. I've also tried running both of these
self.navigationController!.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
self.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
This however throws an error (error unwrapping nil). How should I change the font and color of the nav bar back button correctly? Feels like I'm not modifying the right items...
If you want to set same color to bar buttons implicitly then in your AppDelegate, in didfinishlaunchingwithoptions, write:
UINavigationBar.appearance().tintColor = UIColor.white //your desired color here
Update :
Put this in AppDelegate,
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal) // your textattributes here
Update 2 :
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.classForCoder()]).setTitleTextAttributes(["attribute" : "value"], forState: .Normal)
Hope this will help :)
Swift 3.0 answer (based on Lion's answer):
let newFont = UIFont(name: "Avenir Next", size: 16.0)!
let color = UIColor.white
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.classForCoder() as! UIAppearanceContainer.Type]).setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: newFont], for: .normal)
Works a treat, for those that have already managed to customise other parts of their nav bars but not the back button!
I think you should change it in the vc before your actual vc. Look at: UINavigationItem
Edit:
For example you can write:
let item = UIBarButtonItem(title: "Text goes here", style: .Plain, target: self, action: #selector(self.navigationController?.popViewControllerAnimated(_:)))
item.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 23)!], forState: .Normal)
navigationItem.backBarButtonItem = item
in your prepareForSegue Method.
Swift 4
in AppDelegate.swift
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
in swift 4.2
to change back button color
self.navigationController?.navigationBar.tintColor = .white
Use the following code:
navigationController?.navigationBar.barTintColor = UIColor.purpleColor()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
change colour according to your need
create custom button and make it as you want and add action to go back.
func addBackBarButtonOnNavigationBar(){
// add image here
let searchImage:UIImage = UIImage(named: "back button image")!
var backBtn:UIBarButtonItem = UIBarButtonItem(image: searchImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(classname.buttonActionMethodName(_:)))
backBtn.tintColor = UIColor.lightGrayColor()
if let font = UIFont(name: "AvenirNext", size: 15) {
backBtn.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
self.navigationItem.leftBarButtonItem = backBtn
}
func buttonActionMethodName(){
self.navigationController!.popViewControllerAnimated(true)
}
In Swift 5 you can do it by these:
self.navigationItem.backBarButtonItem?.tintColor = UIColor.red
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular)]
self.navigationItem.backBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
Please note it will be effective for the next pushed view controller not the current one on the display, that's why it's very confusing!
Also, check the storyboard and select the navigation item of the previous view controller then type something in the Back Button (Inspector).

Error when instantiating a UIFont in an text attributes dictionary

I'm trying to set the font of the UIBarButtonItem like so:
let barButton = UIBarButtonItem.appearance()
barButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "AvenirNext", size: 15], forState: UIControlState.Normal)
But it throws a compiler error saying:
Cannot invoke 'init' with an argument list type '($T7, forState: UIControlState)`
and I have no idea what that means. I have also tried
barButton.titleTextAttributesForState(UIControlState.Normal) =[NSFontAttributeName...]`
but it appears that it isn't assignable
How can I resolve this?
The initializer of UIFont returns an optional because it may fail due to misspelled font name etc.
You have to unwrap it and check:
if let font = UIFont(name: "AvenirNext", size: 15) {
barButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
UPDATED for Swift 3
if let font = UIFont(name: "AvenirNext", size: 15) {
barButton.setTitleTextAttributes([NSFontAttributeName:font], for: .normal)
}
Setting Custom font is little bit tricky, since they don't have font and title properties. Hope this following answer will help you.
let font = UIFont(name: "<your_custom_font_name>", size: <font_size>)
var leftBarButtonItem = UIBarButtonItem(title: "<font_hex_code>", style: UIBarButtonStyle.Plain, target: self, action: "buttonClicked:")
leftBarButtonItem.setTitleTextAttributes([NSFontAttributeName:font!], forState: UIControlState.Normal)
self.navigationItem.leftBarButtonItem = leftBarButtonItem
if let font : UIFont = UIFont(name: "Roboto-Regular", size: 15)
{
cancelBarButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
doneBarButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
With Swift 4
NSFontAttributeName is deprecated, you can use NSAttributedStringKey values to set attributes.
if let fontStyle = UIFont(name: "HelveticaNeue-Light", size: 19) {
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: fontStyle]
}

Appearance Proxy in Swift (iOS)

Has anyone tried using the appearance proxy in swift yet?
This syntax doesn't work, has anyone figured out how to set title text attributes on controls like segmentedControl or UITabBar? I think I am close
segmentedControl.titleTextAttributesForState(UIControlState.Normal) =
NSDictionary(objects: [UIFont(name: fontFamilyRegular, size: 16.0)],
forKeys: [NSFontAttributeName])
This should do it:
segmentedControl.setTitleTextAttributes([
NSFontAttributeName: UIFont(name: "Helvetica", size: 16.0)!,
NSForegroundColorAttributeName: UIColor.blueColor()
], forState: UIControlState.Normal)
Make sure you unwrap the the font (!)
let font = UIFont(name: "HelveticaNeue-Light", size:15.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName:font,NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Normal)
For XCode 6.1, try this:
UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIFont(name: "Helvetica", size: 16.0)!, forKey: NSFontAttributeName), forState: UIControlState.Normal)

Resources