NSFont convert font to bold or italic - objective-c

I'm using the following code to change a font to bold on iOS.
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithName:font.fontName size:font.pointSize];
UIFontDescriptor *styleDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:[fontDescriptor symbolicTraits] | UIFontDescriptorTraitBold];
UIFont *boldFont = [UIFont fontWithDescriptor:styleDescriptor size:0];
I'm trying to do the equivalent thing on Mac OS X. I tried this but it's picking a bolder font than the iOS code.
NSFont *boldFont = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSFontBoldTrait];
I want the Mac code to behave exactly like the iOS code and pick the same bold font.

Related

Force Lucida Grande Font in a Yosemite Cocoa app

I have a requirement for some labels in my app to use Lucida Grande. When I specify this font in Xcode it renders correctly in the xib, but when you launch the app in Yosemite it switches back to Helvetica Neue.
How can I get this to work correctly?
As per Ken's advice, setting the font manually in the awakeFromNib() method gives the correct result.
You can either set the font property of the NSTextField
[self.myLabel setFont:[NSFont fontWithName:#"Lucida Grande" size:50.f]];
or use an NSAttributedString
NSDictionary *attributes = #{ NSFontAttributeName : [NSFont fontWithName:#"Lucida Grande" size:50] };
NSAttributedString *text = [[NSAttributedString alloc] initWithString:#"Hello" attributes:attributes];
[self.myLabel setAttributedStringValue:text];
Interestingly, in many cases I have seen fonts with spaces in the font name input with dashes, i.e. #"Lucida-Grande", this actually breaks the output, dashes must be omitted.

UINavigationBar custom font not working on iOS 8

I've spent some hours trying to use a custom font for the navigation bar title on iOS 8. This works ok on iOS 7:
UIFont *font = [UIFont fontWithName:#"Gotham-Bold" size:12];
NSDictionary *textAttributes = #{ NSFontAttributeName: font };
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
But on iOS 8 the text simply disappears. Changing the color, or the system font size works perfectly, but trying to use a custom font won't work at all. Anyone has any idea on this?
Thanks!
EDIT
I've created a small project so you can try it yourself:
https://github.com/noquepoaqui/customHeaderFont
These lines of code are on MasterViewController on line 30.
You have a typo when you assign the attributes:
It should be:
self.navigationController.navigationBar.titleTextAttributes
instead of:
self.navigationController.navigationBar.TitleTextAttributes
If that doesn't fix the issue, you can test if the font was added correctly by placing this code in the AppDelegate's didFinishLoadingWithOptions. This will print all available fonts of your app. Just look at the output window and search for your font name (Gotham-Bold). If it's not listed you can delete the font from your project and add it again via drag & drop. Make sure to tick "add to target" next to your app in the dialog that appears.
//list all available fonts
for (NSString *family in [UIFont familyNames]) {
NSLog(#"---------- %# ----------", family.uppercaseString);
NSArray *names = [UIFont fontNamesForFamilyName:family];
for (NSString *font in names) NSLog(#"%#", font);
}
The .ttf font i was using was not working well. I've installed the .otf version and everything works well.

only change font size (and not font name)

What I want to do is change the font size.
I know below statement will change the font size, but it changes the font name because we are using systemFontOfSize
[UIFont systemFontOfSize: 13.0];
I know alternate option is as mentioned below.
[myLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
but I don't want to use fontWithName as I am setting that in IB.
I don't want to play with font name as my app is multi-language and hence I don't want to play with font name.
Any idea how can I just change fontsize and don't change the fontname.
Too bad that the font metrics properties of UIFont are readonly. It'd be nice if they could be adjusted dynamically without having to do this below:
UIFont * fontFromLabel = myLabel.font;
// now we have the font from the label, let's make a new font
// with the same font name but a different size
if(fontFromLabel)
{
// 13, or whatever size you want
UIFont * newFontForLabel = [UIFont fontWithName: fontFromLabel.fontName size: 13.0f];
if(newFontForLabel)
{
[myLabel setFont: newFontForLabel];
}
}

UITextView formatting with fontname and bold

In a UITextView is it possible to set the fontname and then set it to bold?
I know that font name and size can be set using
fontWithName:(NSString *)fontName size:(CGFloat)fontSize
But once this is done how can i make it bold?
The system font on the iPhone is a good choice for many purposes. You can easily select it in a regular, bold or italic style using built-in font class methods. For example
UIFont *mainTitleFont = [UIFont boldSystemFontOfSize:14.0];
UIFont *subTitleFont = [UIFont SystemFontOfSize:14.0];
UIFont *textFont = [UIFont italicSystemFontOfSize:12.0];
But what if you want a font using both bold and italic at the same time, or a different typeface altogether? In that case, you can use the “fontWithName” method as follows.
UIFont *altFont = [UIFont fontWithName:#"Courier-Bold" size:14.0];
Reference
Use the Bold variant of the font, e.g. #"Helvetica-Bold" as the fontName. If using the default system font is ok for you, you can also use boldSystemFontOfSize: method instead.
UIFont *f = [UIFont fontWithName:#"Verdana-Bold" size:12];
yourTextView.font = f;
If you want to give font with name then you can refer below link. It have all font which are provided by iOS with their font names.
iOS Fonts
Thanks.

Cocoa Touch - Changing tex attributes?

How can I change a textview font and font size..
I've tried this but it doesnt seem to change anything...
typingText.font = [UIFont fontWithName:#"Marker Felt Thin" size:64.0f];
Thanks!
The actual name of the font is "MarkerFelt-Thin".
typingText.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:64.0f];