Im trying to apply on the following code to be bold with current custom font , how do i include it in the dictionary ?
let normalWhite = [NSFontAttributeName : UIFont(name: "somefont", size: 16.0)!,NSForegroundColorAttributeName : UIColor.whiteColor()]
I think you should include the custom bold font file in your Xcode and write its name like Helvetica-Bold
Related
I'm trying to change my font from medium to regular, but I keep getting nil when setting the font type for NSFontAttributeName. I can get the font as medium without a problem, but when I try to set it to regular programmatically, my program crashes because the font is nil.
let attrsUnselected = [NSFontAttributeName: UIFont(name: "Roboto-
Regular", size: 15)!, NSForegroundColorAttributeName : UIColor.gray]
let attrsSelected = [NSFontAttributeName: UIFont(name: "Roboto-Medium",
size: 15)!,NSForegroundColorAttributeName : UIColor.black]
Before my app crashes, I can see that the font is actually set to regular.
I don't know how many different ways I get validate that the font has been imported into the project.
Check the following:
The font is added to the target
Its listed under "Copy Bundle Resources" on your target
And does it show up in the list of available fonts when you print them?
I'm using swift. Is there any way to change default font/ font size etc of an NSMutableAttributedString? Clearly I could set those values specifically for a given range - but that will then override any specific settings in the string.
You can use like that
let yourAttributes = [NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline), NSForegroundColorAttributeName: UIColor.blackColor()]
let yourString = NSAttributedString(string: "Here my string !", attributes: yourAttributes)
Thanks
I am trying to change the font size of the title of a navigation bar. I know I can set its attributes using:
var attributes = [ NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "the font name", size: 18)! ]
...
self.navigationController?.navigationBar.titleTextAttributes = attributes
What I cannot seem to find is the correct 'System' font name.
I was after the default, a.k.a System, font name. I tried printing all the available fonts only to discover it does not belong to a family and does not seem to have an explicit name.
I think you need:
NSFontAttributeName : UIFont.systemFontOfSize(19.0)
Or the bold version:
NSFontAttributeName : UIFont.boldSystemFontOfSize(19.0)
See this guide for more info on user interface guidelines and fonts.
You can access the system font like this, and even set the weight of the font:
Swift 3, Swift 4
UIFont.systemFont(ofSize: 18, weight: UIFontWeightLight)
Swift 2
UIFont.systemFontOfSize(18, weight: UIFontWeightLight)
For the font weight you have the choice between those constants, there available from iOS 8.2:
UIFontWeightUltraLight,
UIFontWeightThin,
UIFontWeightLight,
UIFontWeightRegular,
UIFontWeightMedium,
UIFontWeightSemibold,
UIFontWeightBold,
UIFontWeightHeavy,
UIFontWeightBlack
SWIFT 4+:
shorter version
UIFont.systemFont(ofSize: 14.0, weight: .regular)
(In line with the answer from Philippe for the latest version)
Swift 4
UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.light)
Besides all the answers, it's a better idea to use system font with system styles instead of defining custom sizes and weights. To access them programmatically, for example for the headline, you can use this method:
let font = UIFont.preferredFont(forTextStyle: .headline)
I know it is a valid code at least for Swift 5.
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(6)]
Just use methods of UIFont (swift):
let sysFont: UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize())
Hope it helps!
Try the below code:
self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name:"Arial", size:14.0)!, NSForegroundColorAttributeName:UIColor.blackColor()]
I am getting the following error
Attributed string 'String ' contains NSFontAttributeName that isn't a
UIFont (SanFranciscoDisplay-Light). Removed
Which doesn't make any sense to me because it should be a valid font according to this. http://iosfonts.com. Here is my code.
atrString = NSAttributedString(string: atrString.string, attributes:
[NSForegroundColorAttributeName: UIColor.lightGrayColor(),
NSFontAttributeName:"SanFranciscoDisplay-Light"])
I am trying to access the San Francisco font from my watch kit code. Any tips or suggestions are appreciated.
NSFontAttributeName is a key that should contain a value of type UIFont.
Try UIFont(name: "SanFranciscoDisplay-Light", size: 25) with the size being whatever font size you want.
atrString = NSAttributedString(string: atrString.string, attributes: [NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName:UIFont(name: "SanFranciscoDisplay-Light", size: 25)])
I've read around for different solutions but nothing seems to work. This code creates a nil exception:
[NSFontAttributeName: UIFont(name: "Raleway-SemiBold", size: 16)!]
I have the fonts installed properly and they show up correctly in the app (target is set).
I tried to add the application provided fonts in the plist but nothing happened. I can't edit the items in the array: (they are item0 : string : Raleway-SemiBold.tff).
So basically I'm stuck... Sometimes Swift and Apple environments are great for a programmer, other times (most of the time), they are sooo faulty and need so many workarounds to reach the expected results.
Many thanks in advance for any help.
You're getting an exception because UIFont(name: "Raleway-SemiBold", size: 16) returns nil and you're force-unwrapping it with !.
Instead, use conditional unwrapping:
if let font = UIFont(name: "Raleway-SemiBold", size: 16) {
let attributes = [NSFontAttributeName: font]
// do something with attributes
} else {
// The font "Raleway-SemiBold" is not found
}
You can use UIFont's familyNames() and fontNamesForFamilyName(_:) methods to get the exact string required.
Swift 4
if let font = UIFont(name: "Raleway-SemiBold", size: 16) {
let attributes = [NSAttributedStringKey.font: font]
// do something with attributes
} else {
// The font "Raleway-SemiBold" is not found
}
You Just have to write the correct string name of your font.
Don't write the name that is font file name. (like bodoni-mt-bold.ttf its the file name i have downloaded from any site).
To find out the exact font name follow the image below.
Go to your label select it and go to custom font and then see the name of your custom font in its family. if your custom font name is there then copy that name and past it as a string where u wanna use it. (Note you can't copy font name text you have to write else where then past it)
For Swift 3, here's an update that worked for me:
First you'll set up the font and then create a textAttribute with the NSFontAttributeName:
let font = UIFont(name: "Raleway-SemiBold", size: 16)
textAttribute = [NSFontAttributeName: font as Any, NSForegroundColorAttributeName: UIColor.black]
You can then apply textAttribute to your label, textfield etc.