Since I started iOS development there has always been one thing missing for me. That has been a vector graphic file format. There is no format currently natively supported by apple.
I did some digging and found SVGKit. This is a very nice library that loads in .svg files. It even supports SVG animations. Not to mention its free. So I had to share this. However it can be tricky to set up.
In the end I settled on using PaintCode. PaintCode is a vector based graphics application that generates CoreGraphics Objective-C. PaintCode will also load in SVG files.
The idea is you load an SVG file into PaintCode. Edit it if needed then copy the Objective-C code into your project.
There are different ways you can include it in your projects. I would like to use it in layer of the CALayer flavour.
What I have done is create a CALayer subclass. That all you have to do is past in the PaintCode code.
For convenience I create all my vector assets at 100×100 pixels. I then use a scale to get it to the right size.
This assumes you have PaintCode and have already got the code ready. Also assumes the image is 100×100.
Step 1:
Add the “QuartzCore.framework” to your project.
Step 2:
Create a new file using the “Objective-C Class” template for iOS. USE CALayer Subclass
Step 3:
Add the bellow line to to your Header file(.h).
Objective-C
1
2
//this method will kick off the drawing
-(void)drawImage;
Step 4:
Add the bellow code to the implementation file(.m). Note: see line 17 if your image is not 100×100 pixels
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-(id)init
{
self=[superinit];
if(self){
[selfsetNeedsDisplay];
}
returnself;
}
-(void)display
{
UIScreen*screen=[UIScreenmainScreen];
//Sets the hight and width of the image to 100x100 if you have a different image size change here
I have been trying out LevelHelper recently and it has been very useful when making games that have a fair bit of physics.
I was looking at making a really simple game that did not require physics. Thats where LevelHelper seemed to fall over. Since the editor generates code for you and that code has reference to Box2D.
Fear not for all is not lost, you can still use LevelHelper without implementing the physics. You still need use the Cocos2D-X with Box2D template in xCode. You just don’t attach the physics properties.
//attaches the physics properties and the sprites to the view.
loader->addObjectsToWorld(world,this);
The first parameter “world” is used to attach the physics properties. The second parameter “this” is what all the image assets in LevelHelper level are attached to. All you need to do is change the above line to:
C++
1
2
//only attaches the sprites to the view.
loader->addObjectsToWorld(NULL,this);
Now just ignore any code to do with physics and Box2D.
Recently I have been trying to get a custom map working with Longitude and Latitude that plotted in the x and y in iOS. It also needed to work with a zoom and offset. It was a bit of an argus journey but I got there in the end.
What you will need is a projections type(map type) and the formula for the conversion. Ideally a projection type that compensates for the curvature of the earth.
I settled for a Miller Cylindrical projection. The problem with the maps is finding an accurate map. You can use maps with offsets but getting started it helps if you have an accurate map. I ended up going through a lot of maps before I found an accurate one. It had a white border that I had to crop off but otherwise it was perfect.
DONT FORGET TO CROP THE WHITE BOARDER IF YOU SCALE IT UP
//replace this with the height and width of you image
doubleheight=6563;
doublewidth=8933;
lon=DegreesToRadians(lon);
lat=DegreesToRadians(lat);
x=lon;
y=1.25*log(tan(0.25*M_PI+0.4*lat));
x=((width/2)+(width/(2*M_PI))*x);
y=(height/2)-(height/(2*2.303412543))*y;
returnCGPointMake(x,y);
}
As you can see the the hight and width is hard coded. What if your map needs an offset and you also need to have a dynamic size to compensate for zoom levels.
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//add this at the top of your m file with the rest
#import <math.h>
// this simple c function can go under the imports and above the @implementation
Any object can implement the description method that is used to return a description of the contents of a receiver. The problem i have always found with this method. Is keeping the method up to date during the development process. Also getting other developers working on the project also keeping it up to date.
With that problem in mind I put together this method that will automatically generate the string and list all available properties in the the receiver. It will print out when the instance is used in NSLog or or printed using the GDB debugger commands in my previous post.
One of the things that I struggled with going from eclipse and Visual Studio to xCode was the the debugger. Most of the objects have memory references rather than values. For a long time while developing iOS apps all i would get, or though I had was one cryptic error message. Till I discovered po which stands for print object. This allowed me to print out the contents of an object which can be very useful.
Here is list of gdb commands: (Warning properties are not identified by the debugger so dont use anything with dot notation)
po – print object – can be used to print out the likes of NSStrings and NSDictionaries
use: po someObject
print – prints base types – used to print out to the screen ints floats and the like
use: print myFloat
set – set a variable – allows you to set variables
use: set someString=@”test”
call – calls a method – can be used to call methods or the properties getters and setters
use: – call [someObjectInstance withString:@”newString”];
n – Next – always you to travers your code during breck
use: n
There are 2 main places you can use the commands.
The first is the consol output during a break or crash just after the gdb:
The second is in a break point first set a break point then click the “Click to add action text” ass seen bellow.
Now you can add you command in the newly available text field that will always be triggered at that point in the code.
Carful with the break point triggers not to confuse your self if you put sets or calls in there.
Every time I start a new xCode project i do it a little differently. I include files in some and settings in others. I have decided to put them all here to make sure i don’t miss any. If you have any other project settings, scripts, snippets or useful utils please drop a comment.
1. Enabling and dealing with Zombies:
This allows you to find errors that come from delloced objects being called upon. That normally just gives you the useless error bad access. with this enabled it should take to a class or function when you see the bad access error. This can be dealt with in 2 ways either enabling zombies in the editor thus allowing you to debug it in xCode or using instruments. If the first method does not work for you try the second.
Method 1 xCode Debug:
get to the run settings by press: cmd+alt+r
from the left nave select “RUN yourApp.app”
select “Arguments” from the right tab system
expand if not already expanded the environment variables
add NSZombieEnabled the name parameter and YES to the value parameter
Method 2 instruments Debug:
Rather than take you through it Mark Johnson has created a good video that talks you through it.
PSLog is a NSLog replacment. I have seen it arround for a while now. Basicly it gives you a bit more info like where log is called from. The only problem with it, is the dependancy it creates on having to keep importing PSLog in every one of your projects.
I modified it so you can use NSLog in your code and at compile time in debug mode it replaces your NSLogs with PSLogs. During deploy it replaces NSLog with comments. This way it removes any depandacies on the PSLog function.
How ever this only applies to the PSLog function it self none of its expanded counter parts like PSLogDebug. Using PSLogDebug will maintain a dependancy.
Check it out:
https://github.com/abeazam/PSLog.git
If you #import “PSLog.h” in the .pch file in the support group in your project you wont have import it in every file you want to use it. which Helps the whole dependancy issue.
TODO, FIXME converted to warnings
I duno if you noticed but if you add a //TODO: or //FIXME: comments it appears in the function menu. The problem with that though is you need to be in the class to see the list. Ideally what I wanted was a list of todos that I can see them all in one place. You can do that by adding a bash script that adds them as warnings.
Code based localisation focuses on any strings you may have in your code. Finding these strings and making them dynamic. This is a contrast to how localisation is done in say Flash where the localisation methods are normally set up by the developer. Kind of like Flex.
If you are after NIB Localisation checkout this post here
Abstract: A localised project needs a local specific folder that follows a name convention made up of 2 parts. First is the language like “English”, “French” or “Japanese”. The second is the local project abbreviation “.lproj” putting them together you get folders by the name “English.lproj”, “French.lproj” or “Japanese.lproj”. All the local based copy and assets will end up in the respective local folder. You can create these folders manually but I like to get xCode to create them using the localisation functionality built into the IDE.
Basically any text that is set via code needs to be set using NSLocalizedString this takes 2 strings. The first is the the copy that needs localisation. The second string it takes is the comment that appears above the name value pair. see example:
C
1
2
3
4
//use this to set the default copy to "Please enter user name:" in English
self.userNameFeild.text=NSLocalizedString(@"Please Enter your user nsme;",@"user name subbmision copy");
//use this to set the default copy to "Please enter user name:" in french
self.userNameFeild.text=NSLocalizedString(@"Veuillezécrire le nomd'utilisateur:",@"Veuillez écrire le nom d'utilisateur");
Once you have written your code using NSLocalizedString with all the necessary default copy you will run a terminal script that will pull out the default strings strings to create a .strings file which you would then localise. “genstrings” is the command line tool used to interrogate your .m files to find what needs to be localised.
Once you have your localised .strings file you simply copy it to the different local folders updated the copy to the respective local and bobs your uncle, its done.
Implementation:
create a new project in xcode it should not matter what type but I will use an iPhone View-Based Application.
add the bellow code to the viewControler of the newly created project to quickly add some uilabels
uiLableSurName.text=NSLocalizedString(@"Last name:",@"sure Name touse");
[self.view addSubview:uiLableSurName];
[superviewDidLoad];
}
Open the terminal window and navigate too the root of you project folder. This is the folder that contains your Classes folder.
Run “genstrings ./Classes/*.m” in the terminal which will scan every .m file in your Classes folder for any references to NSLocalizedString and added them to a Localizable.strings file which should appear in the root of your project folder.
In the finder navigate to the root of the project folder and then drag and drop Localizable.strings from the Finder to the resource folder in XCode remembering to insure that the “Copy items into…” check box is unchecked. Also make sure that Text Encoding is set to UTF-16 to insure all languages are covered.
Finally press add.
Right-Click the newly added Localizable.strings then select “Get Info”.
Select the General tab from the “Get Info” panel
Click “Make file Localizable”
Click back on the general tab
Click “Add Localization”
Enter your new Locals “French”(from the English name section of the link) and or see here for full list: http://www.loc.gov/standards/iso639-2/php/English_list.php
Once you have added the Locals for Localizable.strings close the “Get Info” window
Run your app go to the settings panel>>Genral>>International change your local and it should display your localised text.
There are 2 ways to localizes iPhone application and regular OSX applications. The first is localizing the NIB file. This automatically hard codes the localized copy into a NIB file. This works as of iPhone SDK 3.0 should work on previouse versions and future versions but that was my version at the time.
You can also localise with code but that is not the focus of this point check back for Code based localisation maybe next week.
Abstract: A localised project needs a local specific folder that follows a name convention made up of 2 parts. First is the Language, like “English”, “French” or “Japanese”. The second is the local project abbreviation lproj. Putting them together you get “English.lproj”, “French.lproj” or “Japanese.lproj”. All the local based copy and assets will end up in the respective local folder. You can create these folders manually but I like to get xCode to create them using the localisation functionality built into the IDE.
NIB localisation works by taking a NIB that was built for one local with all the copy hard-coded into the NIB then scanning the NIB with the command line tool ibtool. Extracting the copy to a strings file. The strings file is then copied and localised for each local. Then another script hard codes the localised strings into the respective NIBs. This process only uses the generated strings files once to get the NIBs localised. Once this is done the Strings files are no longer necessary.
Implementation:
create a new project in xCode it should not matter what type but I will use an iPhone View-Based Application.
open up the view controller NIB in interface builder add some labels and buttons add some copy to them
save and close interface builder
in xCode right click the view controller NIB and click “Get Info”
make sure you are in the genral tab then click “Make file Localizable”
select the “General” tab again as may change after clicking “Make file Localizable”
now we are going to run the command line script above to scan your nib file and generate a strings file below I have broken down the command so you can replace what u need
I will use ibtool –generate-strings-file NIBCopy.strings English.lpoj/LocalizationNIBViewController.xib
ibtool is the command line tool called Interface builder tool that is installed by default when u install xCode
–generate-strings-file is the ibtool command that creates a strings file from a nib
NIBCopy.strings is the name I have chosen for my strings file u can rename it if you want but maintain the .strings at the end
English.lpoj/LocalizationNIBViewController.xib is the path from the root of my xCode project folder to the NIB file i want to get scanned you must rename this to match the path and name of your NIB
in your case you should only have to replace the NIB path from sub step 6
so you should end up with something similar to ibtool –generate-strings-file NIBCopy.strings English.lpoj/LocalizationNIBViewController.xib
it is likely that you will get the error “Unable to find either a loadable database or a Nodes.xml configuration file” i get it too don’t worry about it assuming your strings file was generated it will contain the name value pairs from the nib
check to make sure your NIBCopy.strings is created if so move on else go back to sub step one and try again maybe type out the whole request manually in-case copy pasting messed it up a bit
now copy the NIBCopy.strings to the French folder and any other locals folder you have created
update and localise the NIBCopy.strings in the respective folders
once again load up the terminal window
navigate to the root of your project folder in the terminal
now we are going to run the command line script above to take your NIBCopy.strings that contains your localised copy and hard code it into your localised NIBs
ibtool is the command line tool called Interface builder tool that is installed by default when you install xCode
–strings-file the ibtool command that refers to a strings file
French.lproj/NIBCopy.strings my french localised strings file
–write the command that writes a NIB file based on a Strings file and a NIB file
French.lproj/LocalizationNIBViewController.xib the NIB file that will be writen/updated with the localised copy
English.lproj/LocalizationNIBViewController.xibthe NIB file that will be the template for the localised french NIB file above
it is likely that you will get the error “Unable to find either a loadable database or a Nodes.xml configuration file” i get it too don’t worry about it in fact that is how i know it worked 😉
repeat sub step 1 to 7 for each localised NIB file local
to test this either open up the localised NIB in interface builder or run the app and change the language settings in the simulator or on your developer iPhone
the strings files are no longer needed as the copy is now hard-coded into the NIB
After playing around with UIButton class for a while I found Apple have been very sneaky with some of their UI classes.
What it appears they have done is use the UIButton class as a sort of wrapper for a number of private button classes. “UIRoundedRectButton” is one of those classes.
“UIRoundedRectButton” is a private class so it can not be extended(subclassed). The initilizers normaly return an instance of self and if that self is not the class type trying to hold it there will be trouble. The problem is if you initialise UIButton with buttonWithType the class it returns is a private class. If you try and extended the UIButton once your custom class is initialized it returns a “UIRoundedRectButton” object which is private so you can not contain it.
If you try the code below which uses the description you can see the object description is not UIButton.
In short you can not extend UIButton and initialize it with buttonWithType in Objective C. On the other hand you can extened UIButton for a button in interface builder. If you use interface builder you need to use initWithCoder as the initialiser. While interface builder does the private stuff.