Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[NSNull pointSize]: unrecognized selector sent to instance in swift3 while using custom fonts

Tags:

ios

xcode8

swift3

I am trying to customize the font of the UISegmented control and i am using the following code as shown below:

let selectedfont = UIFont(name:"Roboto-Bold", size:22)
let unselectedfont = UIFont(name:"Roboto-Regular", size:22)

let normalTextAttributes: [String: Any] = [
  NSForegroundColorAttributeName: UIColor(red: 0.60, green: 0.60, blue: 0.60, alpha: 1.0),
  NSFontAttributeName: unselectedfont as Any
]
let selectedTextAttributes: [String: Any] = [
  NSForegroundColorAttributeName: UIColor.black,
  NSFontAttributeName: selectedfont as Any
]

self.setTitleTextAttributes(normalTextAttributes, for: UIControlState())
self.setTitleTextAttributes(selectedTextAttributes, for: .highlighted)
self.setTitleTextAttributes(selectedTextAttributes, for: .selected)

But the application crashes showing this: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull pointSize]: unrecognized selector

What is the issue here? Any help will be appriciated.

like image 422
Chelsea Shawra Avatar asked Jul 13 '17 12:07

Chelsea Shawra


1 Answers

Your call to UIFont(name:,size:) is likely returning nil. Did you add the custom font properly?

  1. Register the font in your info.plist adding the "Fonts provided by application" entry. Put the font file name, not the font name. Like font.ttf
  2. Go to your project on the project navigator, click on the project name and go to "Build phases" then look down in "Copy Bundle Resources" go to the bottom, there are a lot of files and search for the "+" sign. A popup window will open with your project files. Search for your font file and add it to the bundle.
  3. Finally check the font name. When you use it in your code, you have to use the font name, not the file name. To know the font name, double click on the font file in Finder and a window will open showing the font. Check that window title, that's the name of the font that you have to use.
like image 78
fpg1503 Avatar answered Jan 04 '23 01:01

fpg1503