iOS 7: What is UIBarButtonItem's default font? - uikit

What is the default font of the title of a UIBarButtonItem with a style of UIBarButtonItemStyleDone?
The following just returns nil:
[doneBarButtonItem titleTextAttributesForState:UIControlStateNormal]

[UIFont boldSystemFontOfSize:17]
Note: I confirmed this by doing:
UIFont *font = [UIFont boldSystemFontOfSize:17];
[doneBarButtonItem setTitleTextAttributes:#{NSFontAttributeName: font}
forState:UIControlStateNormal];
And then, I took screenshots of the before & after and compared them. They were identical.

You can retrieve the default font for a UILabel (which includes UIBarButton's title label) by querying
UIFont.systemFont(ofSize: UIFont.labelFontSize)

Related

NSButton(Cell) setFont

It seems that NSButtonCell's setFont method is not available anymore from 10.9.
Is there any way (or category) to (re)implement it?
I don't know why Apple forces it's own styles on buttons.
I am trying for 2 days to style my own custom button (I also needed a category to simply change the button's text color - shame).
You can use -setAttributedTitle: of NSButton to set the style of button title.
Sample code:
NSDictionary *attributes = #{NSForegroundColorAttributeName: [NSColor redColor], NSFontAttributeName: [NSFont userFixedPitchFontOfSize:13.0f]};
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:#"Button" attributes:attributes];
[button setAttributedTitle:aString];

Incorrect font measures

On a NSTextField I'm setting a custom font with a size of 140. The text is set to #"28". But as you can clearly see on the image, the text field has plenty of space on top. This only happens with certain type of fonts, not all of them. My question is what information from the font could be affecting the textfield that ends up cropping the text ? (Ascender, Cap Height ?). And if so, can I modify the font file to fix it ?
The baseline will vary between fonts. In addition, there are other metrics that vary. You can work around this problem with NSAttributedString. You could try varying the NSBaselineOffsetAttribute and from within a paragraph setMinimumLineHeight and setMaximumLineHeight. The following is an example. Make sure to create two textField labels and connect their outlets.
self.Label1.stringValue = #"Test Text";
//
// baseline is different for each font!
//
//self.Label2.stringValue = #"Test Text";
NSFont *otherFont = [NSFont fontWithName:#"MarkerFelt-Thin" size:40.0f];
NSNumber *baseline = [[NSNumber alloc] initWithFloat: 5.0f];
NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paraStyle setParagraphSpacingBefore:20.0f];
[paraStyle setMinimumLineHeight:30.0f];
[paraStyle setMaximumLineHeight:50.0f];
NSDictionary *otherFDict = [NSDictionary dictionaryWithObjectsAndKeys: paraStyle, NSParagraphStyleAttributeName,
otherFont, NSFontAttributeName, baseline, NSBaselineOffsetAttributeName, nil];
NSMutableAttributedString *otherText = [[NSMutableAttributedString alloc] initWithString:#"Test Text" attributes:otherFDict];
self.Label2.attributedStringValue = otherText;

Code equivalent for size property in Interface Builder

I'm creating some NSTableColumns dynamically and they appear too tall in the table. In the Interface Builder there is a general setting to adjust the object size (mini, small, regular). Is there any code equivalent for this or should I simply select the font manually?
Update
I found that I can get the font with:
NSFont *font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
However, the row height doesn't match the items' height. Setting the font in code does nothing with the row height. I use NSTextFieldCells and NSPopUpButtonCells as data cells.
Oh, and I'm building for 10.6.
In addition to changing the font, you need to set the control size of the cell.
NSCell *theCell = ...;
[theCell setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]]];
[theCell setControlSize:NSMiniControlSize];
Apple is now providing an official guide for this.
Here's copied code snippet.
float fontSize = [NSFont systemFontSizeForControlSize:NSMiniControlSize];
NSCell *theCell = [theControl cell];
NSFont *theFont = [NSFont fontWithName:[[theCell font] fontName] size:fontSize];
[theCell setFont:theFont];
[theCell setControlSize:NSMiniControlSize];
[theControl sizeToFit];

Changing label text size in code

I have to adjust my label text size depending of the device (iPad, iPhone) and these orders just doesn't seem to work. I have my label declared in the interface, set on property as IBOutlet and synthesized. Then:
label.font = [UIFont fontWithName:#"Arial Black" size:50.0];
label.minimumFontSize = 50.0;
The size just doesn't change. :S
Any advices?
It may be useful to you to use:
[label setFont:[UIFont systemFontOfSize:35]];
or
[label setFont: [UIFont fontWithName:#"Arial" size:50.0]];
There's nothing wrong in your code except for the font you are using.
It should be :
label.font = [UIFont fontWithName:#"Arial" size:50];
Actually Arial Black is not supported by iphone.
You can check the list of the fonts supported by iPhone here.
You should take a look of the fonts you can use by Default in iOS
here is a link: iOS Links http://iosfonts.com/

Fuzzy text in NSStatusItem

I'm displaying a statusItem at launch like this:
theItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
NSString *theString = [textField stringValue];
(textField.stringValue = theString);
[theItem setTitle:theString];
[theItem setHighlightMode:YES];
The text looks very fuzzy. How can I clean up the look of the text?
Thanks.
Paul
Here's a screenshot with the digital menu bar clock on top, and NSStatusItem title on bottom:
Have you tried drawing the text into an image and using that image in the NSStatusItem?
Looks rather fine to me.
Okay, not "perfect" but it's more about uneven scaling than "fuzziness." Is this what you're seeing too?

Resources