Copy UITextView to PasteBoard - clipboard

I'm trying to copy my "DescriptionLabel" to the Pasteboard. DescriptionLabel is set as a UITextView (I know the name is a little confusing...). Anyway,
- (IBAction)copy:(id)sender {
UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
appPasteBoard.persistent = YES;
[appPasteBoard setString:#"This text is being copied"];
}
The string in the code is being copied, but I can't manage to get it copying my UITextView/DescriptionLabel. This:
[appPasteBoard setString:_DescriptionLabel];
is not working.
Do any of you have any clue of what I can do to make it work? Been struggling with this for days...

Well, the problem is that you are using setString: to store UITextView, which is an UIKit control and not NSString, in pasteboard. What you probably mean is to store its text value.
Objective-C does not support implicit conversions like Scala or Swift. Solution is simple, just access the text property explicitly:
[appPasteBoard setString:_DescriptionLabel.text];
I encourage you to look into UIPasteboard documentation for details concerning it API: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPasteboard_Class/index.html

Related

Change Xcode auto-formatting of NSDictionary

I'm trying to change Xcode's auto-formatting of the NSDictionary to conform to a style guide:
Xcode behavior:
NSDictionary *resourceResponseDictionary = #{
#"status": #"OK",
#"result": [NSDictionary dictionary]
};
Desired behavior:
NSDictionary *resourceResponseDictionary = #{
#"status": #"OK",
#"result": [NSDictionary dictionary]
};
Where would I begin to look to make the change? Is it even possible?
By auto-formatting I guess you mean 'Indentation'. I had a similar question/problem and came to realize that Xcode does not have a specific setting for this except the basic ones in preferences. For example, when adding multiple <Delegates, DataSource...>, starting a new line has no indentation formatting at all, and it must be done manually.
Short answer, you can't (without some hack), but, the default is consistent in this case, meaning: Xcode does indent, the way it knows how to. IMHO, it won't be a bad idea to adapt it.

Cocoa Paste to NSTextView in code

Thanks for the help.
I just need to paste the contents of the clipboard/pasteboard to an NSTextView by way of an action.
NSString *PboardType = #"PboardType";
- (IBAction)paste:sender
{
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSData *copiedData = [pb dataForType:PboardType];
[self RTFFromRange:copiedData];
}
The last line is obviously wrong. what do I need to do?
Thanks
Paul
Nothing. An NSTextView already supports pasting (and cutting and copying, and fonts, and search, and spelling-checking, and …). You don't need to do anything to implement it.
If you're implementing some different paste behavior, you should edit your question to specify what special behavior you're trying to achieve (as well as why the user will want your application to do something unusual).
If the standard paste behavior isn't working, you should edit your question to specify how it isn't working.

Configuring NSPredicateEditor(RowTemplate) for Spotlight metadata queriesI'

I'm trying to configure an NSPredicateEditor (in Interface Builder) to edit the predicate for an NSMetadataQuery.
As a first step, I'm trying to configure an NSPredicateEditorRowTemplate to accept key path(s) for the left-side expression, trying a single keyPath (kMDItemTextContent) to get started.
I can't figure out how to get all the pieces into IB. I've selected the row template, and set "Left Exprs" to "Key Paths" in the IB Attributes Inspector. But, using Apple's PhotoSearch example as a model, it appears that I should enter a user-readable attribute name (say, "Content") here; I can't figure out how to bind it to "kMDItemTextContent".
I've dissected the (correctly-configured) NIB in PhotoSearch(*), and inside it there is an NSKeyPathExpression specifying a metadata attribute attached to an NSPopUpButton/NSPopUpButtonCell.
I can't figure out where to navigate in IB to find the NSPopUpButton, and I'm not sure what I'd do to bind it to an NSExpression.
Any help appreciated.
(*) In case you're wondering, I got inside the NIB by converting it to a XIB, confirming that it still builds correctly, then examining it with BBEdit.
I've found that working with NSPredicateEditor and friends in Interface Builder is an exceedingly tedious task. For that reason, I do all of my row template configuration in code.
For your situation, it doesn't sound like you need a custom row template subclass, so you could probably just do:
#define NSPERT NSPredicateEditorRowTemplate
NSPERT * template = [[NSPERT alloc] initWithLeftExpressions:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:kMDItemTextContent]]
rightExpressionAttributeType:NSStringAttributeType
modifier:NSDirectPredicateModifier
operators:[NSArray arrayWithObject:
[NSNumber numberWithUnsignedInteger:NSContainsPredicateOperatorType]]
options:(NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption)];
Once you've got the template, simply add it to the predicateEditor:
NSMutableArray * templates = [[myPredicateEditor rowTemplates] mutableCopy];
[templates addObject:template];
[template release];
[myPredicateEditor setRowTemplates:templates];
[templates release];
As for translating the "kMDItemTextContent", if it doesn't happen automatically (and I think it might), you could use the NSPredicateEditor localization options to display a different name.

IBPlugin: Adding additional objects on drag from IB Library

I have a list view class that just like NSCollectionView requires an additional prototype item and a prototype view to be of any use.
When dropping an NSCollectionView from the library in Interface Builder those two helper items are automatically created. However I couldn't find a single official Apple document dealing with this use case (describing how its done).
Digging thru the Apple Dev Guides I could however find "ibDidAddToDesignableDocument:".
With the following code I managed to get my auxiliary items created on drop from library:
- (void)ibDidAddToDesignableDocument:(IBDocument *)document {
[super ibDidAddToDesignableDocument:document];
NSView *prototypeView = [[[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 300, 65.0)] autorelease];
DLListViewItem *prototypeViewItem = [[[DLListViewItem alloc] initWithNibName:nil bundle:nil] autorelease];
[document addObject:prototypeViewItem toParent:nil];
[document addObject:prototypeView toParent:nil];
[document connectOutlet:#"view" ofSourceObject:prototypeViewItem toDestinationObject:prototypeView];
[document connectOutlet:#"listView" ofSourceObject:prototypeViewItem toDestinationObject:self];
[document connectOutlet:#"prototypeItem" ofSourceObject:self toDestinationObject:prototypeViewItem];
}
However…
…IB adds those aux items for NSCollectionView only on the actual initial drag from the library, not on any other call of "ibDidAddToDesignableDocument:", such as when embedding, copying or duplicating the item. (while my method would, and on all)
This makes me wonder whether Apple actually uses "ibDidAddToDesignableDocument:" for this and if I'm on the right track with this at all.
How does one imitate this properly? I'm having a hard time trying to distinguish between different contexts for "ibDidAddToDesignableDocument:". Anybody successfully done this?
Unfortunately none of Google, Google Code, GitHub, or the documentation revealed anything helpful, so I'm in desperate need of help here. :(
Thanks in advance!
Edit: Oh great, this question just brought me the tumbleweed badge, yay! Not.
I'm more into useful answers actually, but thanks anyway ;)
I struggled with this on a plugin I did myself a while ago. In my case I was able to check a property of the object to see if it had been initialized already and skip adding the auxilliary objects in that case. I believe BWToolkit uses some internal checking that is similar. Couldn't you check your object's 'prototypeItem' property to see if you need to skip creating your aux objects?

How Can You Use An Image As A Background For A Text Field In Cocoa?

Is it possible to use an image as a background for a Text Field in Cocoa?
If so, how?
I don't know if this is the "correct" way to do it, but the first thing that comes to mind would be to make a custom subclass of NSTextField, which might look roughly like this:
- (void)awakeFromNib
{
[self setDrawsBackground:NO];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
[self lockFocus];
[[NSImage imageNamed:#"<#image filename#>"] drawInRect:rect
fromRect:rect
operation:NSCompositeSourceOver
fraction:1.0];
[self unlockFocus];
}
Again, that's a just rough outline of the essential parts.
Anyways, like I said, I'm not sure if this really the "correct" way to do it (or if there is even a "correct" way, for that matter), but this will give you a background image for your NSTextField.
Edit in response to Joshua's comment (I'm not going to have enough room in that tiny little comment box):
To add the image into your project, you'd drag it from wherever it is into the project window (the main list of files in the middle of the project window, although depending on how you've set up your Xcode editing environment, this might be different for you).
In order to subclass NSTextField, you would want to create a new Objective-C class file (File -> New File…), but edit the header so that the class inherits from NSTextField instead of NSObject. In other words, your header file might look like this:
#import <Cocoa/Cocoa.h>
#interface BGImageTextField : NSTextField
{
}
#end
As for the rest of the code, you would want to add that in the main body of the implementation file (BGImageTextField.m, for example), specifically in between the #implementation and #end keywords.
I'd also like to mention two things. First, I'd recommend picking up a copy of Cocoa Programming for Mac OS X, by Aaron Hillegass—it covers most of the Cocoa basics that I just went over, and is one of the best ways to learn Cocoa in general. Secondly, although my approach works, it's probably not the best approach—especially since I just recently found this post, which seems to hint at a better way of extending NSTextField.
Instead of subclassing NSTextField, just make the background color transparent [NSColor clearColor] and put the image behind it.

Resources