Cocoa. Create NSTextView, add an NSString to it and print - cocoa

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];

Related

i want to paste selected text in a text view on button click named paste.how can i do this?

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]];

How can we put two line in UIBarButtonItem in Navigation Bar

"Now Playing" is in One line in UIBarButtonItem. I have to put it in two lines, like "Now" is ay top and "Playing" is at bottom.I have written the following line of code:-
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Now Playing"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = flipButton;
So i want to pu line break in between "Now Playing". So please help me out.
Yes you can. It is fairly simple to do. Create a multiline button and use that. The "trick" is to set the titleLabel numberOfLines property so that it likes multilines.
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.numberOfLines = 0;
[button setTitle:NSLocalizedString(#"Now\nPlaying", nil) forState:UIControlStateNormal];
[button sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
When you specify a button type of custom it expects you to configure everything... selected state, etc. which is not shown here to keep the answer focused to the problem at hand.
- (void)createCustomRightBarButton_
{
UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease];
addCustomLabel.text =#"Now\nPlaying";
addCustomLabel.textColor = [UIColor whiteColor];
addCustomLabel.font = [UIFont boldSystemFontOfSize:11];
addCustomLabel.numberOfLines = 2;
addCustomLabel.backgroundColor = [UIColor clearColor];
addCustomLabel.textAlignment = UITextAlignmentCenter;
CGSize size = addCustomLabel.bounds.size;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
[addCustomLabel.layer renderInContext: context];
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage * img = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:img
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)]
autorelease];
}
You can host a UIButton as customView inside your bar button (either set the bar button item customView or you can drag one directly on top of your UIBarButtonItem), hook it up as an outlet, then do;
-(void) viewDidLoad
{
//...
self.customButton.titleLabel.numberOfLines = 2;
self.customButton.suggestionsButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.customButton.suggestionsButton.titleLabel.textAlignment = UITextAlignmentCenter;
}
which in my case becomes
I create 2 PNG images by myself, and it looks good.
UIImage *img = [UIImage imageNamed:#"nowplaying.png"];
UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:#selector(presentNowPlayingMovie)] autorelease];

NSString *text to NSString *icon?

I am making an app that is a standalone menu item and the basis for the code is sample code I found on a website. The sample code uses a number as the menu icon, but I want to change it to an image.
I want it to be like other apps where it shows icon.png when not clicked and icon-active.png when clicked.
The current code is this:
- (void)drawRect:(NSRect)rect {
// Draw background if appropriate.
if (clicked) {
[[NSColor selectedMenuItemColor] set];
NSRectFill(rect);
}
// Draw some text, just to show how it's done.
NSString *text = #"3"; // whatever you want
NSColor *textColor = [NSColor controlTextColor];
if (clicked) {
textColor = [NSColor selectedMenuItemTextColor];
}
NSFont *msgFont = [NSFont menuBarFontOfSize:15.0];
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
[paraStyle setAlignment:NSCenterTextAlignment];
[paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableDictionary *msgAttrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
msgFont, NSFontAttributeName,
textColor, NSForegroundColorAttributeName,
paraStyle, NSParagraphStyleAttributeName,
nil];
[paraStyle release];
NSSize msgSize = [text sizeWithAttributes:msgAttrs];
NSRect msgRect = NSMakeRect(0, 0, msgSize.width, msgSize.height);
msgRect.origin.x = ([self frame].size.width - msgSize.width) / 2.0;
msgRect.origin.y = ([self frame].size.height - msgSize.height) / 2.0;
[text drawInRect:msgRect withAttributes:msgAttrs];
}
Also, I found a post describing a method on how to do this, but it did not work for me. The url to that is this: http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem/comment-page-1#comment-46501.
Thanks!
Use an NSImage and draw it where desired. For example:
NSString *name = clicked? #"icon-active" : #"icon";
NSImage *image = [NSImage imageNamed:name];
NSPoint p = [self bounds].origin;
[image drawAtPoint:p fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:1.0];
If this is for a status item and you just want an icon with no programmatic drawing, drop the view and set the status item's image and alternateImage. The former is what the status item uses normally; the status item switches to the alternate image (if it has one) when the user opens its menu.

NSTextField handling Enter key presses strangely

I'm sure I'm just using it incorrectly, but what I'm doing is I have a NSTextField with a attributed string with a few characters of text in a different font at the end of it. When the user clicks the text field, the text at the end should disappear, and when the user finishes editing their text and removes focus from the text field, the text gets added back to the end of the string they entered.
It's working fine when then user tabs out of the box, or clicks somewhere on the window to remove focus from the textfield. The only time it doesn't work is if they hit the "return" key in the text box. The text still gets added to the end of their string in this case, but it's in the same font as the rest of the string.
Here is the relevant portion of my code. I've verified that both methods are being called in the same sequence both when I tab out of a field and when I hit enter in the field.
- (void) selectText:(id)sender
{
[titleText setStringValue: [NSString stringWithFormat:#"%#", userEditableText]];
}
- (void) textDidEndEditing:(NSNotification *)notification
{
userEditableText = [textField stringValue];
NSString* fullText = [NSString stringWithFormat:#"%# (%#)", userEditableText, nonUserEditableText];
NSRange range1;
range1.location = 0;
range1.length = [userEditableText length] - ([nonUserEditableText length] + 2);
NSRange range2;
range2.location = range1.length;
range2.length = ([[nonUserEditableText length] length] + 2);
NSRange range3;
range3.location = 0;
range3.length = [fullText length];
NSFont *font = [NSFont fontWithName:#"Arial" size:14.0];
NSMutableDictionary* stringAttributes = [[NSMutableDictionary alloc] init];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:fullText attributes:stringAttributes];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingMiddle];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range1];
[attrString addAttribute:NSFontAttributeName value:font range:range2];
[textField setAttributedStringValue:attrString];
}
Not sure exactly what I did, but I changed around a bunch of code in several classes and now everything is working properly. Wish I knew what I did...

How to resize NSTextView according to its content?

I am trying to set an attributed string within NSTextView. I want to increase its height based on its content, initially it is set to some default value.
So I tried this method:
I set content in NSTextView. When we set some content in NSTextView its size automatically increases. So I increased height of its super view that is NSScrollView to its height, but NSScrollView is not getting completely resized, it is showing scroller on right.
float xCoordinate = 15.0;
[xContentViewScroller setFrame:NSMakeRect(xCoordinate, 0.0, 560.0, 10.0)];
[[xContentView textStorage] setAttributedString:xContents];
float xContentViewScrollerHeight = [xfContentView frame].size.height + 2;
[xContentViewScroller setFrame:NSMakeRect(xCoordinate, 0.0, 560.0, xContentViewScrollerHeight)];
Can anyone suggest me some way or method to resolve this issue. By doing google search I found that in UITextView there is contentSize method which can get size of its content, I tried to find similar method in NSTextView but could not get any success :(
Based on #Peter Hosey's answer, here is an extension to NSTextView in Swift 4.2:
extension NSTextView {
var contentSize: CGSize {
get {
guard let layoutManager = layoutManager, let textContainer = textContainer else {
print("textView no layoutManager or textContainer")
return .zero
}
layoutManager.ensureLayout(for: textContainer)
return layoutManager.usedRect(for: textContainer).size
}
}
}
NSTextView *textView = [[NSTextView alloc] init];
textView.font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
textView.string = #"Lorem ipsum";
[textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
textView.frame = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
+ (float)heightForString:(NSString *)myString font:(NSFont *)myFont andWidth:(float)myWidth andPadding:(float)padding {
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:myString];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(myWidth, FLT_MAX)];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
[textStorage addAttribute:NSFontAttributeName value:myFont
range:NSMakeRange(0, textStorage.length)];
textContainer.lineFragmentPadding = padding;
(void) [layoutManager glyphRangeForTextContainer:textContainer];
return [layoutManager usedRectForTextContainer:textContainer].size.height;
}
I did the function using this reference: Documentation
Example:
float width = textView.frame.size.width - 2 * textView.textContainerInset.width;
float proposedHeight = [Utils heightForString:textView.string font:textView.font andWidth:width
andPadding:textView.textContainer.lineFragmentPadding];

Resources