Reputation: 3
I am learning objective C and could use some pointers (pun intended). I have a background in Java. I just finished the book 'Programming with Objective C'. I was trying to use drawWithRect to draw some text onscreen but cannot get this piece of sample code to work.
#import <Cocoa/Cocoa.h>
#import <AppKit/NSStringDrawing.h>
//#import <UIKit/UIKit.h> //this is invalid
//#import <UIKit/UIColor.h> //this is invalid
#import "NSString+Draw.h"
NSDictionary *attributes = @{
NSForegroundColorAttributeName:[UIColor blueColor]
};
[@"Hello" drawWithRect:CGRectMake(20, 20, 50, 50)
options:0 attributes:attributes context:nil];
It doesn't seem to be in the Frameworks folders in my XCode project. I had looked on apple developer site but was swamped by unrelated information.
How do I get this UIKit? Is this an addon? btw why are some imports using quotes and others angle brackets? are they interchangeable?
I don't have a good big picture of the frameworks. What are the key frameworks?
Is there a web based API documentation like Javadocs?
Still at it.
Is there an equivalent of UIKit/UIColor etc for OSX? - I found it, AppKit/NSColor
Ok After a bit of exploration I got this but it still doesn't write onscreen.
#import <Cocoa/Cocoa.h>
#import <AppKit/NSStringDrawing.h>
#import <AppKit/NSColor.h>
#import "NSString+Draw.h"
@implementation NSString (Draw)
- (void) drawString:(NSString *) myString{
NSDictionary *attributes = @{
NSForegroundColorAttributeName:[NSColor blueColor]
};
[@"Hello" drawInRect:CGRectMake(20, 20, 50, 50) withAttributes:attributes];
}
Upvotes: 0
Views: 231
Reputation: 8609
To draw anything on the screen, you need something called a Graphics Context. The Graphics Context gives you an area to draw in and manipulate. Apple does a good job of explaining it in their documentation here. Further research through the Apple documentation turns up a specific section for drawing text (NSString
in particular). An excerpt below from the NSString Drawing documentation:
The Application Kit adds three methods to the NSString class to support drawing string objects directly in an NSView instance:
drawAtPoint:withAttributes:
,drawInRect:withAttributes:
, andsizeWithAttributes:
.
There are three things you may want to check in order to ensure everything is / will be drawn properly:
CGRect
(at least for testing)?If you're absolutely frustrated with drawing NSString using AppKit's methods, try the Core Text framework:
Core Text is an advanced, low-level technology for laying out text and handling fonts. The Core Text API, introduced in Mac OS X v10.5 and iOS 3.2, is accessible from all OS X and iOS environments.
Information on Frameworks and general SDK questions:
When you download Xcode, you're also downloading the Development SDK and related documentation too. The Development SDK is mainly comprised of Frameworks (from Apple). A Framework is a huge compiled code library with visible-header files. So, you can't see the implementations, but you can see the headers.
When importing a framework (doesn't matter if it's from Apple or not), use brackets (or a module if you have it enabled in the build settings). When importing files (not frameworks) then just use quotes.
There are a LOT of frameworks for both iOS and OS X. I can't list them all here, but I can tell you the key ones. Here's a link to the iOS developer documentation, including the frameworks. The big four on iOS are Foundation, UIKit, CoreGraphics, and (sometimes) QuartzCore. When you click on your project in the Xcode file navigator, you should see the project settings and a list of frameworks included. Click the plus button to see all Apple frameworks you can add. Most of them are related to system services (ex. GameCenter framework is GameKit, and Passbook framework is PassKit).
Yes! The Apple Developer Documentation is available both online and offline (installed with Xcode). Online documentation can usually be found through googling, but here's a link. It's also in the Xcode Help menu / documentation menu. You can also option-click on any piece of code you want to learn more about.
The equivalent to UIKit (an iOS only framework) for OS X is AppKit. Most of the classes are the same names, just with an NSA prefix instead of a UI prefix. For example, on OS X you'd need NSColor instead of UIColor.
Upvotes: 0
Reputation: 12782
To do things visually you need a Graphics Context to draw in.
Objective-C does not provide one. An operating system does through a window server. AppKit on OS X provides this through NSWindow. UIKit on iOS provides this with a single implicit window.
Upvotes: 0
Reputation: 150595
Apart fro needed to use UIKit and an iOS project, it's also beer to just import the top level header, rather than the individual ones.
So just
#import <UIKit/UIKit.h>
Or, if on Xcode 5
@import UIKit;
Upvotes: 0
Reputation: 2369
It looks like you've created a project targeting OSX. UIKit is a framework that is included with iOS only. Create an iOS project to use that framework.
UIKit is the big framework for iOS, and includes things like UIColor (or anything else with a UI prefix).
Docs are online, and can also be found in Xcode in the 'Help' tab
Upvotes: 3