I set the list of fontfamilies to a NSPopupButton. I don't know How to set the selected font preview to the NSTextView
have tried like this
NSString *fontstyle = [popup_button titleOfSelectedItem];
NSFont *fontst = [NSFontManager:nil willIncludeFont:fontstyle];
[text_view setFont:fontstyle];
Finally I got it...
NSString *fontstyle = [popup_button titleOfSelectedItem];
NSString *fontsize = [pop_size titleOfSelectedItem];
CGFloat fntsize = [fontsize floatValue];
NSFont *regular = [NSFont fontWithName:fontstyle size:fntsize];
[text_view setFont:regular];
Related
I was trying to create an animated text label and suddenly this odd problem hit me. I am trying to write out a simple label using CATextLayer on a view. As you can see, I have tried to calculate the frame of the text by using the sizeWithAttributes: of the NSAttributedString class. This gives out a frame that doesn't perfectly fit the CATextLayer even though I am displaying the same attributed string using the layer.
Any insights into why this odd font-rendering problem is happening would be appreciated.
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize view;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
view.layer = [CALayer layer];
[view setWantsLayer:YES];
CATextLayer *textLayer = [CATextLayer layer];
textLayer.fontSize = 12;
textLayer.position = CGPointMake(100, 50);
[view.layer addSublayer:textLayer];
NSFont *font = [NSFont fontWithName:#"Helvetica-Bold" size:12];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
NSString *helloString = #"This is a long string";
NSAttributedString *hello = [[NSAttributedString alloc] initWithString:helloString attributes:attrs];
NSSize stringBounds = [helloString sizeWithAttributes:attrs];
stringBounds.width = ceilf(stringBounds.width);
stringBounds.height = ceilf(stringBounds.height);
textLayer.bounds = CGRectMake(0, 0, stringBounds.width, stringBounds.height);
textLayer.string = hello;
}
#end
CATextLayer renders the glyphs using CoreText and not Cocoa (educated guess based on experiments).
Check out this sample code to see how to calculate the size using CoreText and a CTTypesetter. This gave us much more accurate results:
https://github.com/rhult/height-for-width
I am new to cocoa prgramming, and I am copying text using the code:
NSRange range = [textView selectedRange];
NSData* rtfData = [textView RTFFromRange: range];
NSAttributedString* aStr = [[NSAttributedString alloc]initWithRTF:rtfData documentAttributes:NULL];
NSString* str = [aStr string];
str contains the selected text, but how can I paste that text in a NSTextView whenever I click in a textview?
you have to set delegate of NSTextView to self, then you need to implement delegate method on which you want to perform this action.
And then you can use setString method of NSText(Super class of NSTextView) to set the text.
Edit - (Try with this first, if it set the text to textview)
NSRange range = [textView selectedRange];
NSData* rtfData = [textView RTFFromRange: range];
NSAttributedString* aStr = [[NSAttributedString alloc]initWithRTF:rtfData documentAttributes:NULL];
NSString* str = [aStr string];
[[textView setString:str];
for NSTextView you need :
[[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:str]];
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];
I have a window with a button for printing receipts. What I need to do is to create a simple NSTextView, add an NSString to it (at least something like "Hello World") and print it without displaying NSTextView on the window.
Here is what I currently have:
NSTextView *textView = [[NSTextView alloc] init];
NSString *text = #"testing";
[textView setEditable:true];
NSRange range = NSMakeRange( 0, [[textView string] length]);
[textView setSelectedRange:range];
[[[textView textStorage] mutableString] appendString:text];
NSPrintOperation *printOperation;
printOperation = [NSPrintOperation printOperationWithView:textView];
[printOperation runOperation];
when I run it, I see printing dialog, but preview is empty.
When I change printOperationWithView:textView]; from textView to one of the existing views on my window, it prints ok.
The main thing is.. I don't want to display the view after I click Print button. Ideally I would like to print the text, not the view.
Have you tried using -initWithFrame with a nonzero rect?
NSTextView *textview = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 468, 648);
(468 x 648 would make it fit an A4 sheet with 1-inch margins.)
Here is an implementation which works in an NSDocument subclass. Modify it to suit your needs.
- (NSPrintOperation *)printOperationWithSettings:(NSDictionary<NSString *,id> *)printSettings error:(NSError * _Nullable __autoreleasing *)outError
{
NSPrintInfo * printInfo = [self printInfo];
printInfo.topMargin = 15.0;
printInfo.leftMargin = 10.0;
printInfo.rightMargin = 10.0;
printInfo.bottomMargin = 15.0;
printInfo.verticallyCentered = NO;
NSRect bounds = printInfo.imageablePageBounds;
NSTextView * printView = [[NSTextView alloc] initWithFrame:bounds];
printView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[printView.textStorage appendAttributedString:self.attributedStringRepresentation];
return [NSPrintOperation printOperationWithView:printView printInfo:printInfo];
}
Have you tried just:
[textView print];
This is very good. Just one more enhancement. In order for the print out not to be centered, and to fill up the page correctly, pass printInfo in as a parameter.
NSPrintOperation *printOperation;
NSPrintInfo *pInfo = [[NSPrintInfo alloc] init];
[pInfo setBottomMargin:50];
[pInfo setTopMargin:50];
[pInfo setVerticallyCentered:false];
printOperation = [NSPrintOperation printOperationWithView:textView printInfo:pInfo];
[printOperation runOperation];
I am displaying buttons in NSMatrix.
My requirement is:
to change color of button title and
place an image at beginning of title, when certain condition is satisfied.
To do so, I used following code:
// setting attributed text
NSAttributedString *selectedCellAttribute;
NSFont *selectedCellFont = [NSFont fontWithName:#"Lucida Grande" size:11];
NSColor *selectedCellColor = [NSColor redColor];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSCenterTextAlignment];
// setting image
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
NSCell *cell = [imageAttachment attachmentCell];
[cell setImage:[NSImage imageNamed:#"caution_small.png"]];
NSDictionary *selectedCellDictionary = [NSDictionary dictionaryWithObjectsAndKeys:imageAttachment,NSAttachmentAttributeName,selectedCellFont,NSFontAttributeName,selectedCellColor,NSForegroundColorAttributeName,style,NSParagraphStyleAttributeName,nil];
// recognizing cell
NSButtonCell *associatedCell = [associatesMatrix cellAtRow:0 column:2];
selectedCellAttribute = [[NSAttributedString alloc] initWithString:[associatedCell title] attributes:selectedCellDictionary];
[associatedCell setAttributedTitle:selectedCellAttribute];
Although above code is showing change in color of title, it is showing no image placed in beginning of title :(
Can anyone suggest me where I may be wrong or some other method to implement my requirements?
EDIT:
At line:
NSCell *cell = [imageAttachment attachmentCell];
it is giving this warning when compiled:
type 'id <NSTextAttachmentCell>' does not conform to 'NSCopying" protocol.
Thanks,
Miraaj
You've set the attachment for the entire string. What you need to do is prefix the string with NSAttachmentCharacter, and set the attachment only for that section of the string.
You may want to put a space between the NSAttachmentCharacter and your actual text. Only the NSAttachmentCharacter should have the attachment attribute.