Custom fonts not working in Swift programmatically but working with xib/Storyboard
I am using a custom font in my application. I added the fonts "ttf" file to the resource folder and made it available for all the target. I verified, it was in the copy bundle resource and in the plist file with proper naming conventions.
I used FontBook Mac app to take the PostScript name of the custom font and copy pasted to my code to avoid type errors.
I'm able to select the font in Storyboard/xib and its reflecting in the UI. But when I tried to do it programmatically like without using designers like below the font is not reflected in the UI.
The notable point here is I have 4 buttons and if I set font for one button via storyboard and set the same font for other buttons via programmatically, then its working.
exploreAppBtn.titleLabel?.font = UIFont(name: "SansOfcMed-Bold", size: 20)
Is it an Xcode issue? Anyone have faced it? If yes, whats the solution?
Added the Plist file and Build phases screenshots


Any help is appreciated! Thanks
ios swift xcode swift4 ios12
add a comment |
I am using a custom font in my application. I added the fonts "ttf" file to the resource folder and made it available for all the target. I verified, it was in the copy bundle resource and in the plist file with proper naming conventions.
I used FontBook Mac app to take the PostScript name of the custom font and copy pasted to my code to avoid type errors.
I'm able to select the font in Storyboard/xib and its reflecting in the UI. But when I tried to do it programmatically like without using designers like below the font is not reflected in the UI.
The notable point here is I have 4 buttons and if I set font for one button via storyboard and set the same font for other buttons via programmatically, then its working.
exploreAppBtn.titleLabel?.font = UIFont(name: "SansOfcMed-Bold", size: 20)
Is it an Xcode issue? Anyone have faced it? If yes, whats the solution?
Added the Plist file and Build phases screenshots


Any help is appreciated! Thanks
ios swift xcode swift4 ios12
what did you try to pass instead of CustomFontName?
– Robert Dresler
Nov 12 '18 at 12:52
CustomFontName is just a string constant in constant file.
– Logunath
Nov 13 '18 at 4:52
add a comment |
I am using a custom font in my application. I added the fonts "ttf" file to the resource folder and made it available for all the target. I verified, it was in the copy bundle resource and in the plist file with proper naming conventions.
I used FontBook Mac app to take the PostScript name of the custom font and copy pasted to my code to avoid type errors.
I'm able to select the font in Storyboard/xib and its reflecting in the UI. But when I tried to do it programmatically like without using designers like below the font is not reflected in the UI.
The notable point here is I have 4 buttons and if I set font for one button via storyboard and set the same font for other buttons via programmatically, then its working.
exploreAppBtn.titleLabel?.font = UIFont(name: "SansOfcMed-Bold", size: 20)
Is it an Xcode issue? Anyone have faced it? If yes, whats the solution?
Added the Plist file and Build phases screenshots


Any help is appreciated! Thanks
ios swift xcode swift4 ios12
I am using a custom font in my application. I added the fonts "ttf" file to the resource folder and made it available for all the target. I verified, it was in the copy bundle resource and in the plist file with proper naming conventions.
I used FontBook Mac app to take the PostScript name of the custom font and copy pasted to my code to avoid type errors.
I'm able to select the font in Storyboard/xib and its reflecting in the UI. But when I tried to do it programmatically like without using designers like below the font is not reflected in the UI.
The notable point here is I have 4 buttons and if I set font for one button via storyboard and set the same font for other buttons via programmatically, then its working.
exploreAppBtn.titleLabel?.font = UIFont(name: "SansOfcMed-Bold", size: 20)
Is it an Xcode issue? Anyone have faced it? If yes, whats the solution?
Added the Plist file and Build phases screenshots


Any help is appreciated! Thanks
ios swift xcode swift4 ios12
ios swift xcode swift4 ios12
edited Nov 13 '18 at 6:37
Logunath
asked Nov 12 '18 at 12:41
LogunathLogunath
2771618
2771618
what did you try to pass instead of CustomFontName?
– Robert Dresler
Nov 12 '18 at 12:52
CustomFontName is just a string constant in constant file.
– Logunath
Nov 13 '18 at 4:52
add a comment |
what did you try to pass instead of CustomFontName?
– Robert Dresler
Nov 12 '18 at 12:52
CustomFontName is just a string constant in constant file.
– Logunath
Nov 13 '18 at 4:52
what did you try to pass instead of CustomFontName?
– Robert Dresler
Nov 12 '18 at 12:52
what did you try to pass instead of CustomFontName?
– Robert Dresler
Nov 12 '18 at 12:52
CustomFontName is just a string constant in constant file.
– Logunath
Nov 13 '18 at 4:52
CustomFontName is just a string constant in constant file.
– Logunath
Nov 13 '18 at 4:52
add a comment |
2 Answers
2
active
oldest
votes
- Check if custom font is added in info.plist file with proper naming conventions.
Add extension
extension UIFont
class func fontName(size: CGFloat) -> UIFont
return UIFont.init(name: "CustomFontName", size: size)!
Usage:
exploreAppButton.titleLabel?.font = UIFont.fontName(size: 16)
Yes, font is listed in the plist file and I could able to do it via designers. Problem only when accessing through code.
– Logunath
Nov 13 '18 at 4:53
add a comment |
Could you please clarify how exactly you create CustomFontName
Maybe the font name is not valid
Valid fonts name you can find with the code.
let familyFontsNames = UIFont.familyNames
for familyName in familyFontsNames
let fontsName = UIFont.fontNames(forFamilyName: familyName)
for fontName in fontsName
print(fontName) // Prints list of all available fonts name. And you should use the name.
Try to check if your CustomFontName exists in the list.
CustomFontName is just a String constant in constants file. I tried giving the font PostScript name directly also, but no use. The font is listed in the family font names list.
– Logunath
Nov 13 '18 at 4:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262422%2fcustom-fonts-not-working-in-swift-programmatically-but-working-with-xib-storyboa%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
- Check if custom font is added in info.plist file with proper naming conventions.
Add extension
extension UIFont
class func fontName(size: CGFloat) -> UIFont
return UIFont.init(name: "CustomFontName", size: size)!
Usage:
exploreAppButton.titleLabel?.font = UIFont.fontName(size: 16)
Yes, font is listed in the plist file and I could able to do it via designers. Problem only when accessing through code.
– Logunath
Nov 13 '18 at 4:53
add a comment |
- Check if custom font is added in info.plist file with proper naming conventions.
Add extension
extension UIFont
class func fontName(size: CGFloat) -> UIFont
return UIFont.init(name: "CustomFontName", size: size)!
Usage:
exploreAppButton.titleLabel?.font = UIFont.fontName(size: 16)
Yes, font is listed in the plist file and I could able to do it via designers. Problem only when accessing through code.
– Logunath
Nov 13 '18 at 4:53
add a comment |
- Check if custom font is added in info.plist file with proper naming conventions.
Add extension
extension UIFont
class func fontName(size: CGFloat) -> UIFont
return UIFont.init(name: "CustomFontName", size: size)!
Usage:
exploreAppButton.titleLabel?.font = UIFont.fontName(size: 16)
- Check if custom font is added in info.plist file with proper naming conventions.
Add extension
extension UIFont
class func fontName(size: CGFloat) -> UIFont
return UIFont.init(name: "CustomFontName", size: size)!
Usage:
exploreAppButton.titleLabel?.font = UIFont.fontName(size: 16)
edited Nov 12 '18 at 17:54
rmaddy
243k27321383
243k27321383
answered Nov 12 '18 at 15:45
Sourabh KumbharSourabh Kumbhar
20914
20914
Yes, font is listed in the plist file and I could able to do it via designers. Problem only when accessing through code.
– Logunath
Nov 13 '18 at 4:53
add a comment |
Yes, font is listed in the plist file and I could able to do it via designers. Problem only when accessing through code.
– Logunath
Nov 13 '18 at 4:53
Yes, font is listed in the plist file and I could able to do it via designers. Problem only when accessing through code.
– Logunath
Nov 13 '18 at 4:53
Yes, font is listed in the plist file and I could able to do it via designers. Problem only when accessing through code.
– Logunath
Nov 13 '18 at 4:53
add a comment |
Could you please clarify how exactly you create CustomFontName
Maybe the font name is not valid
Valid fonts name you can find with the code.
let familyFontsNames = UIFont.familyNames
for familyName in familyFontsNames
let fontsName = UIFont.fontNames(forFamilyName: familyName)
for fontName in fontsName
print(fontName) // Prints list of all available fonts name. And you should use the name.
Try to check if your CustomFontName exists in the list.
CustomFontName is just a String constant in constants file. I tried giving the font PostScript name directly also, but no use. The font is listed in the family font names list.
– Logunath
Nov 13 '18 at 4:55
add a comment |
Could you please clarify how exactly you create CustomFontName
Maybe the font name is not valid
Valid fonts name you can find with the code.
let familyFontsNames = UIFont.familyNames
for familyName in familyFontsNames
let fontsName = UIFont.fontNames(forFamilyName: familyName)
for fontName in fontsName
print(fontName) // Prints list of all available fonts name. And you should use the name.
Try to check if your CustomFontName exists in the list.
CustomFontName is just a String constant in constants file. I tried giving the font PostScript name directly also, but no use. The font is listed in the family font names list.
– Logunath
Nov 13 '18 at 4:55
add a comment |
Could you please clarify how exactly you create CustomFontName
Maybe the font name is not valid
Valid fonts name you can find with the code.
let familyFontsNames = UIFont.familyNames
for familyName in familyFontsNames
let fontsName = UIFont.fontNames(forFamilyName: familyName)
for fontName in fontsName
print(fontName) // Prints list of all available fonts name. And you should use the name.
Try to check if your CustomFontName exists in the list.
Could you please clarify how exactly you create CustomFontName
Maybe the font name is not valid
Valid fonts name you can find with the code.
let familyFontsNames = UIFont.familyNames
for familyName in familyFontsNames
let fontsName = UIFont.fontNames(forFamilyName: familyName)
for fontName in fontsName
print(fontName) // Prints list of all available fonts name. And you should use the name.
Try to check if your CustomFontName exists in the list.
edited Nov 12 '18 at 13:22
Kuldeep
2,50441735
2,50441735
answered Nov 12 '18 at 12:54
GkoluniaGkolunia
828
828
CustomFontName is just a String constant in constants file. I tried giving the font PostScript name directly also, but no use. The font is listed in the family font names list.
– Logunath
Nov 13 '18 at 4:55
add a comment |
CustomFontName is just a String constant in constants file. I tried giving the font PostScript name directly also, but no use. The font is listed in the family font names list.
– Logunath
Nov 13 '18 at 4:55
CustomFontName is just a String constant in constants file. I tried giving the font PostScript name directly also, but no use. The font is listed in the family font names list.
– Logunath
Nov 13 '18 at 4:55
CustomFontName is just a String constant in constants file. I tried giving the font PostScript name directly also, but no use. The font is listed in the family font names list.
– Logunath
Nov 13 '18 at 4:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262422%2fcustom-fonts-not-working-in-swift-programmatically-but-working-with-xib-storyboa%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what did you try to pass instead of CustomFontName?
– Robert Dresler
Nov 12 '18 at 12:52
CustomFontName is just a string constant in constant file.
– Logunath
Nov 13 '18 at 4:52