0

I want to set the name as bold in the string for the label with my custom font what I want:

"Hi Gaurav"

for this I have used the following code

let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: CGFloat(studentnameFontSize))]

let attributedStringStudenatName = NSMutableAttributedString(string:"Gaurav", attributes:attrs)

let normalText = "Hi "
let normalString = NSMutableAttributedString(string:normalText)

normalString.append(attributedStringStudenatName)

myLabel?.attributedText = normalString

but I want to use my custom font rather than systemFont: let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: CGFloat(studentnameFontSize))]

A J
  • 441
  • 4
  • 20
  • So your question is how to import a custom font in an iOS App? https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app – Larme Mar 24 '21 at 15:16
  • fonts are availabe, already imported, how to use them with attributed text? – A J Mar 24 '21 at 15:20
  • Then, get its postscript name, and do `UIFont(name: "yourCustomFontPostScriptName", size: CGFloat(studentnameFontSize))`? – Larme Mar 24 '21 at 15:21
  • and how to apply bold style? – A J Mar 24 '21 at 15:22
  • You can't add bold effect if you font doesn't have it. Use a bold font. – Larme Mar 24 '21 at 15:23
  • If you open `Font Book.app` in your macOS, see `Arial` and check each one of its "sub fonts" (Arial being the family name), you'll see that there is a font for each one : bold, italic, etc. You can right click on it and "Show in Finder" if you want to really see that there is multiple fonts. `Photoshop.app` and other apps of this kind can apply a "bold" effect (but that's their addon, it's not the official "bold"), italic, etc. – Larme Mar 24 '21 at 15:25
  • see first answer from this link https://stackoverflow.com/questions/28496093/making-text-bold-using-attributed-string-in-swift – Mayur Tanna Mar 24 '21 at 15:44
  • yes but I don't want to make it bold as systemBold, anyway I got vision – A J Mar 25 '21 at 05:20
  • in that link var boldFont:UIFont { return UIFont(name: "AvenirNext-Bold", size: fontSize), is thing , which you are looking for and you can find a way to get font name of your custom font through code. hope this helps :) – Mayur Tanna Mar 27 '21 at 04:52

0 Answers0