I am trying to put a Attributed string inside a NSTextField, which itself is inside an NSAlert
Here is my code:
NSTextField *label1 = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 23, 50, 20)];
[label1 setEditable:FALSE];
[label1 setAllowsEditingTextAttributes:TRUE];
[label1 setBezeled:FALSE];
label1.backgroundColor = [NSColor clearColor];
NSString *login = #"Username";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:login];
NSString *boldFontName = [[NSFont boldSystemFontOfSize:12] fontName];
[attrString beginEditing];
NSRange ran = NSMakeRange(0, 8);
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:ran];
[attrString endEditing];
NSLog(#"%#",attrString);
[label1 setAttributedStringValue:attrString];
[alert setAccessoryView:label1];
[alert runModal];
However, as soon as [alert runModal] is called, my app crashes
"[__NSCFConstantString pointSize]: unrecognized selector sent to instance 0x7fff74035bb0"
I'm not sure why this is happening. It appears that it is related to the string, because as soon as i remove [alert setAccessoryView:label1] or give label1 a standard nsstring it works fine. Please help!
You have done right. But you did a small mistake. You have passed NSString as an attribute for NSFontAttributeName but it expects NSFont.
Try this.
NSFont *boldFontName = [NSFont boldSystemFontOfSize:12];
[attrString beginEditing];
NSRange ran = NSMakeRange(0, 8);
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:ran];
The above answer is absolutely correct. I had a same crash which used to crash in iOS 7.0.3 & 7.0.4 only, and works perfectly in all other versions. After so much investigation, I came to know that #"HelveticaNeue-Italic" is not available in iOS 7.0.3 & 7.0.4 versions, so that I used to get above crash in those versions.
I have fixed the issue with below code :
self.headerFont = [UIFont fontWithName:#"HelveticaNeue-Italic" size:16.0f];
if (self.headerFont == nil) {
self.headerFont = [UIFont fontWithName:#"HelveticaNeue" size:16.0f];
}
Related
I have a custom font with some symbols and I can set the text to a label in a table view, getting the symbol correctly, as follows:
cell.icono.textColor=[UIColor purpleColor];
[cell.icono setFont:[UIFont fontWithName:#"icomoon" size:17]];
const char *icon = "\ue904";
[cell.icono setText:[NSString stringWithUTF8String: icon]];
Now, I am storing in my database the symbols (as string) \ue904, \ue90b, etc, and I get them correctly, I see them in the console without problems, but when I do the following I do not see the symbol, only the string "\ue904"
cell.icono.textColor=[UIColor purpleColor];
[cell.icono setFont:[UIFont fontWithName:#"icomoon" size:17]];
const char *icon = [gridprinc.icono UTF8String];
NSLog(#"%s", icon);
[cell.icono setText:[NSString stringWithUTF8String: icon]];
In the same way, if I do the following, everything works fine
cell.icono.textColor=[UIColor purpleColor];
[cell.icono setFont:[UIFont fontWithName:#"icomoon" size:17]];
NSString *icon = #"\ue907";
[cell.icono setText:icon];
But when getting the string from CoreData, the label doesn't show the symbol, but the console correctly shows the string:
cell.icono.textColor=[UIColor purpleColor];
[cell.icono setFont:[UIFont fontWithName:#"icomoon" size:17]];
NSString *icon = gridprinc.icono;
[cell.icono setText:icon];
hi I need to change text to bold in message composer body pls help me , I've tried with attributed text but I couldn't add
NSMutableAttributedString *yourAttributedString = [[NSMutableAttributedString alloc] initWithString:message];
NSString *boldString = #"hey";
NSRange boldRange = [message rangeOfString:boldString];
[yourAttributedString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:boldRange];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
lbl.attributedText = yourAttributedString;
MFMessageComposeViewController *messageComposeVC = [[MFMessageComposeViewController alloc] init];
messageComposeVC.messageComposeDelegate = self;
messageComposeVC.recipients = selectedContacts;
messageComposeVC.body = [lbl.attributedText string];
[self presentViewController:messageComposeVC animated:YES completion:nil];
I think it is better if you initiate a attribute to hold all your text attributes ( text size, font , colors ,weight ,styles) and add that when you are initializing the text , in your case "yourAttributedString" . Not sure if there is a specific reason to as why you do this so but try the following .
Use LocalizedStandardRangeofString instead or RangeOfString as you are
displaying this text.Check the "important" section in this documentation https://developer.apple.com/documentation/foundation/nsstring/1416849-rangeofstring?language=objc
//make an attributes dictionary here.
NSDictionary *stringAttributes = #{
NSFontAttributeName:[UIFont boldSystemFontOfSize:20]
};
NSString *message = #" message hey";
NSMutableAttributedString *yourAttributedString =[[NSMutableAttributedString alloc]initWithString:message];
NSString *boldString = #"hey";
NSRange range = [message localizedStandardRangeOfString:boldString];
[yourAttributedString addAttributes:stringAttributes range:range];
self.lbl.attributedText = yourAttributedString;
I have an app which uses NSTextAlignmentJustified for an NSAttributedString. In iOS 6 everything is working great. But the same App running in iOS 7 (simulator or device makes no difference) is showing no Justify at all. Also the linespacing seems to have changed dramatically from iOS 6 to 7.
Anyone else encountered this problem? Is there any way to make a justified Textblock in iOS 7 (which works in iOS 6 too?)
Regards,
Markus
Ok, i kind of found a way to make the label Justifiy in iOS 7:
i simply set NSBaselineOffsetAttributeName to 0.
No idea why it works, but it works.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentJustified;
NSAttributedString *string = [[NSAttributedString alloc] initWithString:rawString
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
paragraphStyle, NSParagraphStyleAttributeName ,
[NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName,
nil]];
Setting firstLineHeadIndent on NSMutableParagraphStyle will also work too.
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified; // To justified text
paragraphStyles.firstLineHeadIndent = 0.05; // IMP: must have a value to make it work
NSString *stringTojustify = #"No one wakes up excited to see more advertising, no one goes to sleep thinking about the ads they’ll see tomorrow.";
NSDictionary *attributes = #{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:stringTojustify attributes:attributes];
self.lblQuote.attributedText = attributedString;
self.lblQuote.numberOfLines = 0;
[self.lblQuote sizeToFit];
Just for the record, you could also append a '\n' as first character for a plain UILabel.
self.text = [NSString stringWithFormat:#"\n%#",TextString];
{
CurFrame.origin.y -= FontSize;
self.frame = CurFrame;
}
self.textAlignment = NSTextAlignmentJustified;
I can’t figure out how to set the font/styling of my NSMenuItems in my NSMenu. I tried the setFont method on the NSMenu but it doesn’t seem to have any effect on the menu items. NSMenuItem doesn’t seem to have a setFont method. I would like for them all to have the same font/style so I would hope there’s just one property I can set somewhere.
They can have an attributed title, so you can set an attributed string as title with all it's attributed, font included:
NSMutableAttributedString* str =[[NSMutableAttributedString alloc]initWithString: #"Title"];
[str setAttributes: #{ NSFontAttributeName : [NSFont fontWithName: #"myFont" size: 12.0] } range: NSMakeRange(0, [str length])];
[label setAttributedString: str];
NSMenuItem has support for attributed strings as titles:
- (void)setAttributedTitle:(NSAttributedString *)string;
Example code:
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:#"Hi, how are you?" action:nil keyEquivalent:#""];
NSDictionary *attributes = #{
NSFontAttributeName: [NSFont fontWithName:#"Comic Sans MS" size:19.0],
NSForegroundColorAttributeName: [NSColor greenColor]
};
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:[menuItem title] attributes:attributes];
[menuItem setAttributedTitle:attributedTitle];
Documentation: https://developer.apple.com/library/mac/#documentation/cocoa/reference/applicationkit/classes/nsmenuitem_class/reference/reference.html
+ menuBarFontOfSize: from NSFont is your friend here.
If you don't plan to change the font family, you should use [NSFont menuBarFontOfSize:12] to get the default font and set a new size.
If you are only changing the color, you still need to set the default font size back by doing [NSFont menuBarFontOfSize:0].
So to only change the NSMenuItem color:
NSDictionary *attributes = #{
NSFontAttributeName: [NSFont menuBarFontOfSize:0],
NSForegroundColorAttributeName: [NSColor greenColor]
};
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:[menuItem title] attributes:attributes];
[menuItem setAttributedTitle:attributedTitle];
Actually [NSMenu setFont:] works for all menu items submenus (if last ones doesn't have their own font). Maybe you set attributed title before setting the menu font?
Realized it, after writing own procedure to iterate through menu items.
In case you need some custom processing (i.e. change font for not all items, or customize it for different items) here is a simple iterating code:
#implementation NSMenu (MenuAdditions)
- (void) changeMenuFont:(NSFont*)aFont
{
for (NSMenuItem* anItem in self.itemArray)
{
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:aFont forKey:NSFontAttributeName];
anItem.attributedTitle = [[[NSAttributedString alloc] initWithString:anItem.title attributes:attrsDictionary] autorelease];
if (anItem.submenu)
[anItem.submenu changeMenuFont:aFont];
}
}
#end
I am trying to get the width of an NSString (ex. NSString *myString = #"hello"). Is there a way to do this?
Thanks.
Here's a relatively simple approach. Just create an NSAttributedString with the appropriate font and ask for its size:
- (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width;
}
UIFont * font = [UIFont systemFontOfSize:15];
CGSize stringSize = [aString sizeWithFont:font];
CGFloat width = stringSize.width;
Using the UILabel's attributed string:
- (CGSize)getStringSizeWithText:(NSString *)string font:(UIFont *)font{
UILabel *label = [[UILabel alloc] init];
label.text = string;
label.font = font;
return label.attributedText.size;
}
Send the string a sizeWithAttributes: message, passing a dictionary containing the attributes with which you want to measure the string.
i dont know if you are suppose to use this in cocoa touch. if it is, then:
- (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width;
}
wont work.
in cocoa touch, you gotta add coretext framework and import the header
and write your code like this:
UIFont *font = [UIFont fontWithName:#"HelveticaNeue-BoldItalic" size:DEFAULT_FONT_SIZE];
// NSLog(#"%#", NSFontAttributeName);
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, (NSString *)kCTFontAttributeName, nil];
but, GEE!!!!!
NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithString:self.caption attributes:attributes];
[as size].width;
there's no size this method in NSMutableAttributedString!
finally, this would work
[self.caption sizeWithFont:font].width
as for ios 7 and up this is the correct way:
NSString * buttonTitle = #"demo title";
CGSize stringSize = [buttonTitle sizeWithAttributes:#{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
This works with iOS 14.5
Objective-C
Define attributes:
NSDictionary *attributes = #{
NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:25],
NSStrokeWidthAttributeName: #(0),
NSStrokeColorAttributeName: [UIColor blackColor]
};
Get width and height:
- (CGFloat)widthOfString:(NSString *)string {
CGSize stringSize = [string sizeWithAttributes:attributes];
return stringSize.width;
}
- (CGFloat)heightOfString:(NSString *)string {
CGSize stringSize = [string sizeWithAttributes:attributes];
return stringSize.height;
}
Sorry my question was not detailed enough and is not exactly what I'm trying to do. I am using a text storage, layout manager and a text container. The solution is to use the layout manager to determine the rectangle that bounds the rect. Here is the code.
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:#"hello"];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];
[textContainer release];
[textStorage addLayoutManager:layoutManager];
[layoutManager release];
//Figure out the bounding rectangle
NSRect stringRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(0, [layoutManager numberOfGlyphs]) inTextContainer:textContainer];
UIKit has a nice addition to NSString, making sizeWithAttributes: a bit lighter:
CGSize titleSize = [title sizeWithFont:titleFont
constrainedToSize:contentCellSize
lineBreakMode:UILineBreakModeWordWrap];
Here's Stephen's solution in Clozure Common Lisp, when using the Objective C bridge. I came across this post when searching for a solution, and I just rewrote Stephen's version which worked fine for me. Others using Clozure might find this helpful:
(defun string-width (str font)
(let* ((dict (#/dictionaryWithObjectsAndKeys: ns:ns-mutable-dictionary
font #$NSFontAttributeName
ccl:+null-ptr+))
(attr (#/initWithString:attributes: (#/alloc ns:ns-attributed-string)
(ccl::%make-nsstring str)
dict))
(size (#/size attr)))
(ns:ns-size-width size)))
This will work. You can try it.
NSDictionary *attrDict = #{NSFontAttributeName : [GenericUtility getOpenSansRegularFontSize:12]};
CGSize stringBoundingBox = [selectedReservationModel.DateLabel sizeWithAttributes: attrDict];
lblDeliveryDateWidth = stringBoundingBox.width;
Just in case you are wondering how to check a label size, you should use the UIFont, instead of the NSFont (not even sure if exists)