How do I create an NSFont that is both Bold and Italic? - cocoa

This is a beginner's question about font handling in Cocoa. I have a font family, e.g. Verdana, that has the following typefaces: Regular, Bold, Italic, and Bold Italic. I know those typefaces exist since they are available in the Fonts panel.
This works:
NSFont *regular = [NSFont fontWithName:#"Verdana" size:75];
NSFont *bold = [NSFont fontWithName:#"Verdana-Bold" size:75];
NSFont *italic = [NSFont fontWithName:#"Verdana-Italic" size:75];
This does not work:
NSFont *boldItalic = [NSFont fontWithName:#"Verdana-Bold Italic" size:75];
What is the simplest way to get the Bold Italic version of a given font family?

This works:
NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *boldItalic = [fontManager fontWithFamily:#"Verdana"
traits:NSBoldFontMask|NSItalicFontMask
weight:0
size:75];

See NSFontManager and -convertFont:toHaveTrait:
For more detailed information, I would suggest reading the Font Handling Guide and specifically the section titled Converting Fonts Manually.
Note, that the font you are using needs to have some version of it with the traits you are asking for, otherwise, you will get back a font, but without the requested trait.
If, eventually, you are seeking to add an italic trait to a font that doesn't have one, check out:
How do I get Lucida Grande italic into my application?

Verdana-BoldItalic.
(The actual name of the bold-italic variant of the font depends on the family, and some font doesn't have bold-italic, Use a NSFontDescriptor with -fontDescriptorWithSymbolicTraits: to get the exact bold italic font.)

Related

CKeditor - Font family

If I have 2 fonts with the same family name - for example Arial Regular and Arial Bold. How can I know what font is being used by default and how can I tell to use Arial Regular for the regular text and Arial Bold for the bold text?
CKEditor uses <strong> tags to denote content that was made bold using the editor.*
You can use this to achieve what you want in the editor's content.css by telling it to use a specific font for <strong> tags. Pseudocode:
body { font-family: "Arial" }
strong { font-family: "Arial Bold"; }
How can I know what font is being used by default
The easiest way is to look in the content.css file, or enter some text into CKEditor and do right-click -> "inspect element" and see which font is being used.
* = this may not necessarily apply to bold content copy & pasted from somewhere else, say MS Word. But all bets are off with that anyway.

Inserting regular space between characters in Cocoa text drawing

Which is the font style used in NSFont to create alignment/space between characters?
I tried doing with this code, but not able to align the characters.
NSFont *font=[NSFont fontWithName:#"TimesNewRomanPSMT" size:15.0 ];
[textview setFont: font];
Please specify the font style name which suits the above mentioned problem.
What about trying fontspace and pick the font you want.
And if you are looking to adjust the space between characters in a UILabel you can use:
#property(nonatomic) BOOL adjustsLetterSpacingToFitWidth;
Edit: Above code for ios.

Qt font families and styles on Mac OS X

I have a GUI application based on Qt (PyQt) running on Mac OS X (and other platforms). It allows the user to select fonts and has check boxes for bold and italic options.
On updating to the new Cocoa-based Qt, a user found the QFontComboBox no longer displays the different font styles (e.g. condensed, oblique, bold...) and just displays the font family name. Under Carbon it showed both. The user also cannot enter the name with the style in the combo box. Furthermore, I cannot construct the same font passing the name into the QFont constructor.
QFont doesn't seem to know anything about these missing styles. Confusingly it has another different notion of style. It looks like QFontDatabase has the information about the style variants. It can also construct fonts based on a family, style and point size.
So, are the following changes the correct approach?
Rather than have bold and italic buttons, have a combobox filled with the styles from QFontDatabase for the font selected in the QFontComboBox.
Store the style in saved documents (as text), rather than bold or italic.
So
Can I rely on the style being the same on systems with different languages? Can I include it in saved documents?
Is a style "Bold Italic" as robust a way of making a QFont with the desired properties, than setting the bold and italic properties of that QFont?
If I want to retain bold and italic checkboxes, how can I convert these attributes into the correct style? Can I just guess "Bold", "Italic" or "Bold Italic"?

IOS 5.1 UILabel with Heavy Check Mark character ignores TextColor

I've been developing an iOS app and recently "upgraded" to xCode 4.3.1 and the iOS 5.1 simulator and have a very strange issue with just one character. It's called "Heavy Check Mark" in the character viewer and it looks great in my app in 5.0.1 and below and is colored with a .textColor = [UIColor redColor]. In 5.1 it shows up black in the simulator and since my phone is jailbroken I haven't checked it in 5.1 on an actual device. If I put in any other character it shows up red, but this one specific character always shows black. If I put a space before it it shows up red but the spacing is off as I'm using a layer to border. Below is actual code, but I've tried a simpler label and have the same issue.
isChecked = [[[UILabel alloc] initWithFrame:CGRectMake(20.0,9.0,20,20)] autorelease];
isChecked.font = [UIFont boldSystemFontOfSize:24.0];
isChecked.backgroundColor = [UIColor clearColor];
isChecked.textColor = [UIColor redColor];
isChecked.layer.borderColor = [UIColor blackColor].CGColor;
isChecked.layer.borderWidth = 2.0;
isChecked.text = #"✔";
isChecked.tag = 2;
[cell.contentView addSubview:isChecked];
Anyone else experiencing problems with this or other special characters and UILabel.textColor? Any suggested workarounds? I've tried temporarily removing the layer and even creating a new minimal label and same results black if this character only and red as set if any other.
Update and fix that works for me, but still very strange. If anyone else ran into this obscure issue I found that using a named font instead of system font seems to fix it.
In iOS9 they have removed the possibility to colour the heavy checkmark also for non-standard fonts. This is because the U+2714 HEAVY CHECK MARK is included in Apple's set of emoji characters and it will be drawn as a full-colour bitmap instead of a single-colour unicode character.
The way to prevent this from happening, you could use a U+FE0E VARIATION SELECTOR-15 character. If you change the string to #"\u2714\uFE0E" you would be able to colour it.
isChecked.text = #"\u2714\uFE0E";
isChecked.textColor = [UIColor redColor];

How to use italic LucidaGrande font for a Cocoa App

Recently I'm working on Carbon to Cocoa transition for my App.
The font my App used is "LucidaGrande", I got it from this method:
myUIFont = [NSFont systemFontOfSize: [NSFont systemFontSize]];
Sometimes my app want to show italic style font in its UI, so I call this:
myUIFont = [[NSFontManager sharedFontManager] convertFont:myUIFont toHaveTrait:NSItalicFontMask];
But this conversion failed. I found that it's because that there is no italic style for "LucidaGrande" font. I can verify this by calling:
NSArray* availableNames = [[NSFontManager sharedFontManager] availableFontNamesWithTraits:NSItalicFontMask];
There's no "LucidaGrande" in availableNames. It is very curious because I can use italic "LucidaGrande" font in my Carbon App. Is there any solution to get a italic "LucidaGrande" font for Cocoa App? Or how can I get a similar italic one?
Thanks very much!
There is no italic version of Lucida Grande per se. Carbon text APIs fake it by slanting the regular version. Lucida Sans and Lucida Sans Unicode (regular) are identical in their Latin subsets, and have italic versions, but do not ship with the OS.

Resources