NSMutableAttributedString set font size - xcode

Every resource I keep coming across for this is for iOS. I can't seem to get this working properly for Mac OSX. Any idea how to get this to set the font size as well? It currently sets the color and center alignment properly. Thanks
NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:#"Library"];
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0,[#"Library" length] ) ];
[libTitle setAlignment:NSCenterTextAlignment range:NSMakeRange(0, [#"Library" length])];
[libTitle addAttribute:NSFontSizeAttribute value:[NSFont systemFontOfSize:18.0] range:NSMakeRange(0, [#"Library" length])];
[self.libbtn setAttributedTitle:libTitle];

Try setting your font attributes from the get go, in the init method:
NSFont *systemFont = [NSFont systemFontOfSize:18.0f];
NSDictionary * fontAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:systemFont, NSFontAttributeName, nil];
NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:#"Library" attributes:fontAttributes];
NSRange rangeOfTitle = NSMakeRange(0,[libTitle length]);
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:rangeOfTitle];
[libTitle setAlignment:NSCenterTextAlignment range:rangeOfTitle];
[self.libbtn setAttributedTitle:libTitle];

Related

Looking for simple example to write text into a NSImage using Cocoa

NSImage *myNewIconImage=[[NSImage imageNamed:#"FanImage"] copy];
[myNewIconImage lockFocus];
[#"15" drawAtPoint:NSZeroPoint withAttributes:nil];
[myNewIconImage unlockFocus];
[myNewIconImage setTemplate:YES];
[[NSApplication sharedApplication]] setApplicationIconImage:myNewIconImage];
I am looking for a way to simply write a String onto this image.... and coming up very short. This does not worker me.
The following code will place a mutable attributed string on an NSImage:
NSImageView *imageView = [[NSImageView alloc] initWithFrame:NSMakeRect( 0, 0, _wndW, _wndH )];
[[window contentView] addSubview:imageView];
NSImage *image = [NSImage imageNamed:#"myImage.jpg"];
[image lockFocus];
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
[attr setObject:[NSFont fontWithName:#"Lucida Grande" size:36] forKey:NSFontAttributeName];
[attr setObject: [NSNumber numberWithFloat: 10.0] forKey: NSStrokeWidthAttributeName];
[attr setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
NSString *myStr = #"Welcome to Cocoa";
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithString: myStr attributes:attr];
[s drawAtPoint:NSMakePoint(130,130)];
[image unlockFocus];
[imageView setImage:image];

Appending NSAttributedStrings not working as expected

I'm trying to append attributed strings to make chord names like "G#m7" where the # and 7 are superscript. I'm doing it like this:
NSFont* font = [NSFont fontWithName:kGillSans size:24];
NSMutableDictionary* attributeNormal = [[NSMutableDictionary alloc] init];
[attributeNormal setObject:font forKey:NSFontAttributeName];
[attributeNormal setObject:color forKey:NSForegroundColorAttributeName];
NSFont* fontSmall = [NSFont fontWithName:kGillSans size:14];
NSMutableDictionary* attributeSuperScript = [[NSMutableDictionary alloc] init];
[attributeSuperScript setObject:fontSmall forKey:NSFontAttributeName];
[attributeSuperScript setObject:[NSNumber numberWithInteger:5] forKey:NSSuperscriptAttributeName];
[attributeSuperScript setObject:color forKey:NSForegroundColorAttributeName];
NSAttributedString* pitch = [[NSAttributedString alloc] initWithString:#"G" attributes:attributeNormal];
NSAttributedString* accidental = [[NSAttributedString alloc] initWithString:#"#" attributes:attributeSuperScript];
NSAttributedString* quality = [[NSAttributedString alloc] initWithString:#"m" attributes:attributeNormal];
NSAttributedString* seventh = [[NSAttributedString alloc] initWithString:#"7" attributes:attributeSuperScript];
NSMutableAttributedString* fullString = [[NSMutableAttributedString alloc] initWithAttributedString:pitch];
[fullString appendAttributedString:accidental];
[fullString appendAttributedString:quality];
[fullString appendAttributedString:seventh];
I then add this to a CATextLayer for display. Unfortunately, it's not working as expected. The superscripts are not superscript, and the [fullstring size] that is returned with it is way off, resulting in a hard to place layer. I turned the border width on to demonstrate the size issue.
I've experimented with different font sizes and values for the superscript attribute. Any ideas?
Try this:
NSFont* font = [NSFont fontWithName:#"GillSans" size:24];
NSDictionary * attributeNormal = #{NSFontAttributeName: font,
NSForegroundColorAttributeName: [NSColor blackColor]};
NSFont* fontSmall = [NSFont fontWithName:#"GillSans" size:14];
NSDictionary * attributeSuperScript = #{NSFontAttributeName: fontSmall,
NSSuperscriptAttributeName: #(2),
NSForegroundColorAttributeName: [NSColor blackColor]};
NSMutableAttributedString* fullString =
[[NSMutableAttributedString alloc]
initWithAttributedString:[[NSAttributedString alloc]
initWithString:#"G"
attributes:attributeNormal]];
[fullString appendAttributedString:
[[NSAttributedString alloc] initWithString:#"#"
attributes:attributeSuperScript]];
[fullString appendAttributedString:
[[NSAttributedString alloc] initWithString:#"m"
attributes:attributeNormal]];
[fullString appendAttributedString:
[[NSAttributedString alloc] initWithString:#"7"
attributes:attributeSuperScript]];

Can set either alignment or font, but not both, for NSTextField

I have a panel nib with an outlet for one of its textfields, which is set in the nib to have centered alignment. When I display the panel, I would like this textfield to be bolded.
Since NSTextField is a subclass of NSControl, it can use the setAttributedStringValue method and take an attributed string. So I incorporated a bold font like this:
NSFont *fontBolded = [NSFont fontWithName:#"Baskerville Bold" size:12.0f];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObject:fontBolded forKey:NSFontAttributeName];
NSString *sHelloUser = NSLocalizedString(#"Hello User", #"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];
[attrsHelloUser release];
The bolding shows up OK, but the field is now left-aligned.
I tried adding a setAlignment, but it had no effect:
[self.fooController.tfPanelCenteredField setAlignment:NSCenterTextAlignment];
So I tried adding a centered parapraph style to the attributed string’s attributes:
NSFont *fontBolded = [NSFont fontWithName:#"Baskerville Bold" size:12.0f];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:NSCenterTextAlignment];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:paragStyle, NSParagraphStyleAttributeName, fontBolded, NSFontNameAttribute, nil];
NSString *sHelloUser = NSLocalizedString(#"Hello User", #"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];
[attrsHelloUser release];
[paragStyle release];
Now the textfield is centered again, but the bolding is gone. It’s as though the attributed string can accept one and only one attribute setting. Am I missing something simple?
You have a typo in your code. NSFontNameAttribute should be NSFontAttributeName.
So your attributes dictionary is:
NSFont *fontBolded = [NSFont fontWithName:#"Baskerville Bold" size:12.0f];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:NSCenterTextAlignment];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:
fontBolded, NSFontAttributeName,
paragStyle, NSParagraphStyleAttributeName,
nil];

NSButton setAlignment does not work

I used
[button setAlignment:NSCenterTextAlignment];
to make the text display at the center of the button.
It worked.
But if I set button title attribute before the code, button 'setAlignment' will not work
- (void)setButtonTitle:(NSButton*)button fontName:(NSString*)fontName fontSize:(CGFloat)fontSize fontColor:(NSColor*)fontColor;
{
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:[button title]
attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:fontName size:fontSize]
forKey:NSFontAttributeName]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:fontColor
range:NSMakeRange(0, [[button title] length] )];
[button setAttributedTitle: attributedString];
[button setAlignment:NSCenterTextAlignment];//button title alignment always displayed as 'NSLeftTextAlignment' rather than 'NSCenterTextAlignment'.
}
title alignment always displayed as 'NSLeftTextAlignment' rather than 'NSCenterTextAlignment'.
Welcome any comment
Since you’re using an attributed string for the button title, the attributes in that string are responsible for setting the alignment.
To centre that attributed string, add an NSParagraphStyleAttributeName attribute with a centred alignment value:
NSMutableParagraphStyle *centredStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[centredStyle setAlignment:NSCenterTextAlignment];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
NSParagraphStyleAttributeName,
[NSFont fontWithName:fontName size:fontSize],
NSFontAttributeName,
fontColor,
NSForegroundColorAttributeName,
nil];
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:[button title]
attributes:attrs];
[button setAttributedTitle: attributedString];
In the code above, I’ve created a single attrs dictionary holding all the attributes for the attributed string. From your code, it looks like the font colour should be applied to the whole string anyway.

NSString sizeWithAttributes: content rect

How do I get a NSString's size as if it drew in an NSRect. The problem is when I try -[NSString sizeWithAttributes:], it returns an NSSize as if it had infinite width. I want to give a maximum width to the method. Is there any way of doing so? (BTW: Mac OS, not iPhone OS)
Thanks,
Alex
float heightForStringDrawing(NSString *myString, NSFont *myFont,
float myWidth)
{
NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:myString] autorelease];
NSTextContainer *textContainer = [[[NSTextContainer alloc] initWithContainerSize:NSMakeSize(myWidth, FLT_MAX)] autorelease];
;
NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init] autorelease];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
[textStorage addAttribute:NSFontAttributeName value:myFont
range:NSMakeRange(0, [textStorage length])];
[textContainer setLineFragmentPadding:0.0];
(void) [layoutManager glyphRangeForTextContainer:textContainer];
return [layoutManager
usedRectForTextContainer:textContainer].size.height;
}
It was in the docs after all. Thank you Joshua anyway!
I revised Alexandre Cassagne's answer for iOS with ARC enabled.
CGSize ACMStringSize(NSString *string, UIFont *font, CGSize size)
{
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:string];
[textStorage addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [textStorage length])];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:size];
textContainer.lineFragmentPadding = 0;
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
return [layoutManager usedRectForTextContainer:textContainer].size;
}
I believe your only option here is NSLayoutManager and asking for a union of the used rects for a given glyph range.
for OSX cocoa/Swift 4:
(using a custom NSview, to show also how to draw)
//
// CustomView.swift
// DrawMyString
//
// Created by ing.conti on 19/10/2018.
// Copyright © 2018 com.ingconti. All rights reserved.
//
import Cocoa
class CustomView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let s : NSString = "AA BB CC DD EE FF"
let W = 50
let size = NSSize(width: W, height: 100)
let boundingRectWithSize = s.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [:])
let r = NSRect(origin: CGPoint(x: 20, y: 30), size: boundingRectWithSize.size)
s.draw(in: r, withAttributes: [:])
r.frame()
}
}
(we also draw border, to see it)

Resources