jdl
jdl

Reputation: 6323

How to write NSString to the NSView?

I can write to iOS using something similar(UI instead of NS). But fails for OSX?

NSString *sText = @"Hello";
[sText drawInRect:CGRectMake(x, y, 150, 20) withFont:[NSFont boldSystemFontOfSize:fSize]]; 
// No visible @interface for 'NSString' declares the selector 'drawInRect:withFont:'

Upvotes: 0

Views: 194

Answers (2)

jdl
jdl

Reputation: 6323

Another approach: using "drawAtPoint":

http://forums.macrumors.com/threads/drawing-text-to-an-nsview.904433/

Upvotes: 1

l'L'l
l'L'l

Reputation: 47169

You need to use NSAttributedString properties on OS X:

NSString *sText = @"Hello";
[sText drawInRect:CGRectMake(x, y, 150, 20) withAttributes: @{ NSFontAttributeName : 
                                             [NSFont boldSystemFontOfSize:fSize] }];

https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSString_AppKitAdditions/index.html

Upvotes: 2

Related Questions