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 '18 at 12:52











  • CustomFontName is just a string constant in constant file.

    – Logunath
    Nov 13 '18 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 '18 at 12:52











  • CustomFontName is just a string constant in constant file.

    – Logunath
    Nov 13 '18 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 '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

















  • 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












2 Answers
2






active

oldest

votes


















2














  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 '18 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 '18 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









2














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















2














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













2












2








2







  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 '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

















  • 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













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 '18 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 '18 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 '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

















  • 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

















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.




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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ