Objective C writing text onscreen (and overview of libraries/frameworks) - objective-c

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

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

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:, and sizeWithAttributes:.
There are three things you may want to check in order to ensure everything is / will be drawn properly:
You must have an active NSView to draw on. Check that there is an active NSView to draw on.
You may need to add more parameters to your attributes dictionary. I don't see any required keys, but it might be a good idea to add a font parameter and a background color parameter.
Does the text actually fit in the specified bounds? Maybe specify a larger 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.

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;

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.

Related

How to check what frameworks and methods supported by winobjc

I tried to port a sample iOS app with a UITableView to Windows 10. In which it tries to display 100 rows with each row contains label with value as row number. Here is the code
UILabel *label = (UILabel*)[cell viewWithTag:1];
label.text = [NSString stringWithFormat:#"Name %ld", indexPath.row];
And when launched this app in windows10 i see only empty rows.
Does viewWithTag is supported?.
How do i check what api's and methods supported by winobjc? Is there any documentation available on this?
I work on the iOS bridge project at Microsoft. The problem you're seeing here has to do with cell reuse identifiers, not viewWithTag. Our Xib2Nib tool handles converting Storyboards and Xibs when you run vsimporter. Currently, Xib2Nib does not support cell reuse identifiers defined in Storyboards (although it does support cell reuse identifiers in Xibs). So when you call dequeueReusableCellWithIdentifier:forIndexPath:, the correct cell with your UILabel in it is not instantiated, which results in you getting blank cells.
There are a couple of potential solutions to this. You could build your layout programmatically in a UITableViewCell subclass and use registerClass:forCellReuseIdentifier:. You could also lay out your cell in its own xib file (separate from your Storyboard) and use registerNib:forCellReuseIdentifier:.
In either case, you should file an issue on the project on Github if you'd like to see support for cell reuse identifiers in Storyboards – Github is the best way to get in touch with our team and informs all of our prioritization decisions.
More generally, you can see what's supported and not supported by the bridge in the Visual Studio debug console; when missing or stubbed APIs are called, you'll get a message with details. We're also working on tools that will make browsing the API surface area easier.
Thanks for checking out the project!

NSOutlineViewDataSource protocol only shows up when importing Cocoa.h instead foundation.h

I was working on Lesson 49 of the Cocoa Programming on YouTube's apple programming channel and when I tried to make a delegate to the NSOutlineViewDataSource in my OutlineViewController.h file, Xcode did not know what I was talking about. I had NSObject as my superclass and it was importing Foundation.h like the video but it did not like the protocol. I had to change over to Cocoa.h to get it to work. Did Foundation change between Xcode 4.5 (I think that's what the video is using) and Xcode 6? I am running Xcode 6 on OSX 10.9.5
Thanks,
Patrick
Foundation.framework is much like its name says and is just the bare bones of the OSX and iOS frameworks. So stuff like strings, numbers, arrays, sets.
Note that Foundation isn't the same bunch of things on iOS and OSX but same principle, nuts and bolts.
To get all the widgets and shiny stuff that makes UI happen you need to import Cocoa on OSX or UIKit on iOS
Main moral of the story is don't trust tutorials too far as they are bound to be wrong at points.

encodeCGSize:forKey: and decodeCGSizeForKey: equivalent for OS X

I'm converting a little library to work with Tiled files (tmx extension) for my OS X's sprite kit game. I know there is no possibility to know the source code of the method in UIKit framework, any idea how to substitute these two Cocoa-touch only methods ?
Theses methods are part of the NSCoder class.
You can use encodeSize:forKey vs decodeSizeForKey: in Cocoa (reference).
Example:
[myCoder encodeSize:NSSizeFromCGSize(myCGSize) forKey:#"key"];
NSSize mySize = [myDecoder decodeSizeForKey:#"key"];

Render an icon inside NSCell (Cocoa OSX)

I have an NSOutlineView and a stack of objects, you can imagine it is a tree of files.
So I tried to extend the NSTextFieldCell class to parse the name of the current item and render an icon for it. But I am still stuck in the icon part. I simply can't get a standard-hardcoded-image to work!
I tried many tutorials, the only one I got to work is a class called PXSourceList, but it was designed for OSX 10.7+. Also the majority of these tutorials use AppDelegate with the NSOutlineViewDataSource protocol and I also want the code to be managed elsewhere, not in the APPDelegate class.
Can someone give-me some directions on the first steps? I think a bit of enlightenment on how the general logic surrounding the icon thing would be enough. I appreciate!
I use XCode 4.2 for Snow Leopard. The project I'm on is supposed to work in OSX 10.6+, so I can't use the new Lion approach of cells using NSViews.
You can get the file icon from its path as follows;
NSImage *iconImage1 = [[NSWorkspace sharedWorkspace] iconForFile:filepath];
You need an image cell to display the icon image.

Parse Starter Project for iPad

I know it's very vague and is asking a lot but does anyone know how to convert the standard iOS starter project from iPhone to iPad (both is best)? Or does anyone know where I can download one. I am a new iOS developer and am trying to start learning with Parse.
I am referring to this project https://www.parse.com/downloads/ios/parse-starter-project/latest
P.S. Just because this question isn't perfect doesn't mean you have to go and down vote and flag it for removal I don't have a lot of points already no need to lose even more :)
Not being able to see this sample project, it's hard to say for certain what it will take.
At bare minimum go into your project summary, and select "Universal" for the device support.
Above and beyond that, it just depends on what the app is and how it's structured. For NIBs, you will want a NIB for iPhone and one for iPad. I find it easy to abstract this away so that I can simplify my view loading:
MyController *myController = [[MyController alloc] initWithView:#"MyControllerView" bundle:nil];
Then in a category, I'd define initWithView similar to:
#implementation UIViewController (Universal)
-(id) initWithView:(NSString *)view bundle:(NSBundle *)nibBundle{
bool isIpad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
NSString *nibName = [NSString stringWithFormat:#"%#_%#", view, (isIpad ? #"iPad" : #"iPhone")];
return [self initWithNibName:nibName bundle:nibBundle];
}
#end
But, that's just one aspect of supporting both devices. In reality the subject is rather specific to the app you're working on. Things like OS support (e.g., am I only targeting iOS 6 or higher) play a factor in things.
I have solved it now, if anyone needs the files email me turboecreations#iCloud.com I would upload them but I dont want my MediaFire and other accounts to be removed if I run into copyright issues.