Custom fonts not working in Swift programmatically but working with xib/Storyboard










0














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



enter image description here



enter image description here



Any help is appreciated! Thanks










share|improve this question























  • what did you try to pass instead of CustomFontName?
    – Robert Dresler
    Nov 12 at 12:52










  • CustomFontName is just a string constant in constant file.
    – Logunath
    Nov 13 at 4:52















0














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



enter image description here



enter image description here



Any help is appreciated! Thanks










share|improve this question























  • what did you try to pass instead of CustomFontName?
    – Robert Dresler
    Nov 12 at 12:52










  • CustomFontName is just a string constant in constant file.
    – Logunath
    Nov 13 at 4:52













0












0








0







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



enter image description here



enter image description here



Any help is appreciated! Thanks










share|improve this question















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



enter image description here



enter image description here



Any help is appreciated! Thanks







ios swift xcode swift4 ios12






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 6:37

























asked Nov 12 at 12:41









Logunath

2721618




2721618











  • what did you try to pass instead of CustomFontName?
    – Robert Dresler
    Nov 12 at 12:52










  • CustomFontName is just a string constant in constant file.
    – Logunath
    Nov 13 at 4:52
















  • what did you try to pass instead of CustomFontName?
    – Robert Dresler
    Nov 12 at 12:52










  • CustomFontName is just a string constant in constant file.
    – Logunath
    Nov 13 at 4:52















what did you try to pass instead of CustomFontName?
– Robert Dresler
Nov 12 at 12:52




what did you try to pass instead of CustomFontName?
– Robert Dresler
Nov 12 at 12:52












CustomFontName is just a string constant in constant file.
– Logunath
Nov 13 at 4:52




CustomFontName is just a string constant in constant file.
– Logunath
Nov 13 at 4:52












2 Answers
2






active

oldest

votes


















3














  1. Check if custom font is added in info.plist file with proper naming conventions.


  2. 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)





share|improve this answer






















  • 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 at 4:53


















0














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.






share|improve this answer






















  • 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 at 4:55










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
);



);













draft saved

draft discarded


















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









3














  1. Check if custom font is added in info.plist file with proper naming conventions.


  2. 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)





share|improve this answer






















  • 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 at 4:53















3














  1. Check if custom font is added in info.plist file with proper naming conventions.


  2. 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)





share|improve this answer






















  • 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 at 4:53













3












3








3






  1. Check if custom font is added in info.plist file with proper naming conventions.


  2. 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)





share|improve this answer














  1. Check if custom font is added in info.plist file with proper naming conventions.


  2. 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)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 17:54









rmaddy

238k27309375




238k27309375










answered Nov 12 at 15:45









Sourabh Kumbhar

30114




30114











  • 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 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 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 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 at 4:53













0














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.






share|improve this answer






















  • 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 at 4:55















0














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.






share|improve this answer






















  • 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 at 4:55













0












0








0






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 13:22









Kuldeep

2,47641534




2,47641534










answered Nov 12 at 12:54









Gkolunia

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 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 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 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 at 4:55

















draft saved

draft discarded
















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線