How to get the width of an NSString?

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

How does Apple update the Airport menu while it is open? (How to change NSMenu when it is already open)

Menu mouse tracking is done in a special run loop mode (NSEventTrackingRunLoopMode). In order to modify the menu, you need to dispatch a message so that it will be processed in the event tracking mode. The easiest way to do this is to use this method of NSRunLoop: [[NSRunLoop currentRunLoop] performSelector:@selector(updateTheMenu:) target:self argument:yourMenu order:0 modes:[NSArray … Read more

How can I both stroke and fill with NSAttributedString w/ UILabel

Yes, the key is to apply a Negative value to the NSStrokeWidthAttributeName If this value is positive you will only see the stroke and not the fill. Objective-C: self.label.attributedText=[[NSAttributedString alloc] initWithString:@”string to both stroke and fill” attributes:@{ NSStrokeWidthAttributeName: @-3.0, NSStrokeColorAttributeName:[UIColor yellowColor], NSForegroundColorAttributeName:[UIColor redColor] } ]; Thanks to @cacau below: See also Technical Q&A QA1531 Swift … Read more

Class method equivalent of -respondsToSelector:

Update after seeing your edit: A class object responds to respondsToSelector: just fine, as you’re probably aware. In a test application, I can do both of the following without any compiler warnings: NSLog(@”Responds to selector? %i”, [MyObject respondsToSelector:@selector(respondsToSelector:)]); NSLog(@”Responds to selector? %i”, [[MyObject class] respondsToSelector:@selector(respondsToSelector:)]); However, you’ve declared a protocol on your variable, so it … Read more

NSData-AES Class Encryption/Decryption in Cocoa

Why not use the built-in encryption algorithms? Here’s an NSData+AES i wrote which uses CCCrypt with a 256it key for AES256 encryption. You can use it like: NSData *data = [[NSData dataWithContentsOfFile:@”/etc/passwd”] encryptWithString:@”mykey”]; and decrypt it with: NSData *file = [data decryptWithString:@”mykey”]; DISCLAIMER: There no guarantee my NSData+AES is bug-free 🙂 It’s fairly new. I … Read more

What’s the best way to use Obj-C 2.0 Properties with mutable objects, such as NSMutableArray?

I ran into the same problem some time ago and found a document on the Apple Developer Connection recommending to provide your own implementation of the setter. Code sample form the linked document: @interface MyClass : NSObject { NSMutableArray *myArray; } @property (nonatomic, copy) NSMutableArray *myArray; @end @implementation MyClass @synthesize myArray; – (void)setMyArray:(NSMutableArray *)newArray { … Read more