Articles Tagged 'xcode'


iPhone: all system fonts

The Apple iPhone provides a limited number of fonts to developers. The list of available fonts is easily achieved by code, as we shall see. If you want to use your own fonts, for example by including it in the resources, it is a little more articulate and conivolge also the issue of licenses (rights) on fonts "embeddati" ... we'll talk later. Returning instead to present the official font nell'Apple iPhone these are (click on image to enlarge):

iphonefonts

The Apple iPhone SDK allows access to the special system fonts. These are identified by particular constants and are:

1
2
3
[ UIFont boldSystemFontOfSize : 12.0 ] ; UIFont myBoldFont * = [UIFont boldSystemFontOfSize: 12.0];
[ UIFont SystemFontOfSize : 12.0 ] ; UIFont mySystemFont * = [UIFont SystemFontOfSize: 12.0];
[ UIFont italicSystemFontOfSize : 12.0 ] ; UIFont myItalicFont * = [UIFont italicSystemFontOfSize: 12.0];

If you actually get a pointer to a font odei shown in the image above just use:

1
[ UIFont fontWithName : @ "Helvetica-Bold" size : 22.0 ] ; UIFont myCustomFont * = [UIFont fontWithName: @ "Helvetica-Bold" size: 22.0];

As noted the management of the font is special, in addition to the family (Helvetica, Courier, etc ...) must specify the type (bold, italic, etc ...). In practice, therefore, a font must be provided with these features. The Helvetica, for example, is present with:

1
2
3
4
Helvetica
Helvetica-Bold
Helvetica-Oblique
Helvetica-BoldOblique

If you want to display your fonts directly on the iPhone, here's some useful code line:

1
2
3
4
5
6
7
8
9
10
11
12
listOfFonts = [ [ NSArray alloc ] initWithArray : [ UIFont familyNames ] ] ; NSArray * listOfFonts = [[ NSArray alloc] initWithArray: [UIFont familynames]];
subFontTypes; NSArray * subFontTypes;

int i = 0 ; i< [ listOfFonts count ] ; i ++ ) { for (int i = 0; i <[listOfFonts count]; i + +) {
"Font Family: %@" , [ listOfFonts objectAtIndex : i ] ) ; NSLog (@ "Font Family:% @", [listOfFonts objectAtIndex: i]);
NSArray alloc ] initWithArray : [ UIFont fontNamesForFamilyName : [ listOfFonts objectAtIndex : i ] ] ] ; subFontTypes = [[ NSArray alloc] initWithArray: [UIFont fontNamesForFamilyName: [listOfFonts objectAtIndex: i]]];
int j = 0 ; j< [ subFontTypes count ] ; j ++ ) { for (int j = 0, j <[subFontTypes count] j + +) {
"+----->Type: %@" , [ subFontTypes objectAtIndex : j ] ) ; NSLog (@ "+ -----> Type:% @", [subFontTypes objectAtIndex: j]);
}
; [SubFontTypes release];
}
; [ListOfFonts release];

With the SDK 2.2.1 on my mortgage, I got:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Font Family: Courier
+ -----> Type: Courier
+ -----> Type: Courier-BoldOblique
+ -----> Type: Courier-Oblique
+ -----> Type: Courier-Bold
Font Family: AppleGothic
+ -----> Type: AppleGothic
Font Family: Arial
+ -----> Type: ArialMT
+ -----> Type: Arial-BoldMT
+ -----> Type: Arial-BoldItalicMT
+ -----> Type: Arial-ItalicMT
Font Family: STHeiti TC
+ -----> Type: Light-STHeitiTC
+ -----> Type: Medium-STHeitiTC
Font Family: Hiragino Kaku Gothic Pron
+ -----> Type: HiraKakuProN-W6
+ -----> Type: HiraKakuProN-W3
Font Family: Courier New
+ -----> Type: CourierNewPS-BoldMT
+ -----> Type: CourierNewPS-ItalicMT
+ -----> Type: CourierNewPS-BoldItalicMT
+ -----> Type: CourierNewPSMT
Font Family: Zapfino
+ -----> Type: Zapfino
Font Family: Arial Unicode MS
+ -----> Type: ArialUnicodeMS
Font Family: STHeiti SC
+ -----> Type: Medium-STHeitiSC
+ -----> Type: Light-STHeitiSC
Font Family: American Typewriter
+ -----> Type: AmericanTypewriter
+ -----> Type: AmericanTypewriter-Bold
Font Family: Helvetica
+ -----> Type: Helvetica-Oblique
+ -----> Type: Helvetica-BoldOblique
+ -----> Type: Helvetica
+ -----> Type: Helvetica-Bold
Font Family: Marker Felt
+ -----> Type: Thin-MarkerFelt
Font Family: Helvetica Neue
+ -----> Type: HelveticaNeue
+ -----> Type: HelveticaNeue-Bold
Font Family: DB LCD Temp
+ -----> Type: DBLCDTempBlack
Font Family: Verdana
+ -----> Type: Verdana-Bold
+ -----> Type: Verdana-BoldItalic
+ -----> Type: Verdana
+ -----> Type: Verdana-Italic
Font Family: Times New Roman
+ -----> Type: TimesNewRomanPSMT
+ -----> Type: TimesNewRomanPS-BoldMT
+ -----> Type: TimesNewRomanPS-BoldItalicMT
+ -----> Type: TimesNewRomanPS-ItalicMT
Font Family: Georgia
+ -----> Type: Georgia-Bold
+ -----> Type: Georgia
+ -----> Type: Georgia-BoldItalic
+ -----> Type: Georgia-Italic
Font Family: STHeiti J
+ -----> Type: Medium-STHeitiJ
+ -----> Type: Light-STHeitiJ
Font Family: Arial Rounded MT Bold
+ -----> Type: ArialRoundedMTBold
Font Family: Trebuchet MS
+ -----> Type: TrebuchetMS-Italic
+ -----> Type: TrebuchetMS
+ -----> Type: Trebuchet-BoldItalic
+ -----> Type: TrebuchetMS-Bold
Font Family: STHeiti K
+ -----> Type: Medium-STHeitiK
+ -----> Type: Light-STHeitiK

More ...

How to remove NSLog () from source XCode

<a target="_blank" href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSLog">NSLog()</a> a useful function during the early stages of a project for testing and debugging an application for Apple iPhone or, more generally, in XCode environment. Being just a function, just like the other, his presence will be felt even when releasing (release) our executable. potrebbero influire sulle performance della nostra applicazione, soprattutto se abbiamo inserito NSLog() all'interno di loop. It therefore becomes necessary to remove, in some way, all the lines of NSLog() from our code, because it is not necessary, either because the calls to NSLog() could affect the performance of our application, especially if we put NSLog() to 'inner loop.

Immediately rule out the solution to the "look" and "cancel" because one day there could be used again. We exclude also the solution of the "look" and "comments", uncomfortable for the same reason as before. Fortunately a clean, simple and are using the correct compiler conditionals. What we do in practice, and tell the compiler to exclude - if there is a specific condition - when compiling our source lines that contain NSLog() .

The compilations of directives and instructions of the conditional compiler, are a very powerful and widespread. Those coming from the development of the ANSI-C certainly know very well and will utilizzte in many situations. The peculiarity of these "instructions" resides in the fact, mentioned above, to be seen by the compiler and not executable. This feature makes them useful in many cases and can solve problems otherwise very annoying.

Let's see an example of code that, as predicted, you can "delete" by compiling pieces of code, in our case NSLog() :

1
2
3
4
5
6
# Define ACTIVE_NSLOG 1
/ / If the constant is defined ACTIVE_NSLOG fill
/ / Block of code between # ifdef and # endif
# Ifdef ACTIVE_NSLOG
" ... bla bla" ) ; NSLog (@ "... blah blah");
# Endif

). Conditional statements are part of the compiler of the same family as #define , also, in fact, are preceded by a "pound" ( # ). solo se ACTIVE_NSLOG è definito. In the example shown we have defined a constant ACTIVE_NSLOG ; subsequent lines of code tells the compiler to "include" line NSLog() only if ACTIVE_NSLOG is defined. If we took care, during the writing of our code, inserting calls to NSLog() within the block #ifdef ... #endif per far sparire, alla prossima compilazione, tutti i nostri NSLog() . #ifdef ... #endif , just delete the definition of the constant ACTIVE_NSLOG to fade away, the next compilation, all our NSLog() .

A best and final

Let us now see how to set the XCode environment to improve even more what we have done here! First we choose a constant name that we use in our projects to exclude from the compilation NSLog() . o quello che preferite. You can choose the name that you like, from DEBUG to MIO_DEBUG or whatever you prefer. Open your project, new or old. Place all NSLog() within the block (or block):

1
2
3
# Ifdef MIO_DEBUG
" ... bla bla" ) ; NSLog (@ "... blah blah");
# Endif

Select the main file of your project, right click and choose the Get Info.

getinfo

This opens a window with information about the project:

userdefine

Select the Build tab, verify that you are in Debug configuration (this is the gem), go to the User-Defined section and add, via the button at the bottom left, a new field called OTHER_CFLAGS . At this assegnamoli the value -DMIO_DEBUG=1 . The syntax is -D{mia define}=1 .

This procedure has two advantages:

  1. We must not put in the code #define MIO_DEBUG 1 , but we do it through the project information. So, when we go to fill out the release version (the one without the NSLog() ) we must remember to delete the line #define MIO_DEBUG 1
  2. The constant is defined in relation to the configuration, in our case Debug. Then, passing to the Release configuration (release) will be absent and the constant rows with NSLog() will not be compiled

Conclusions

The above procedure may be useful in a moltidutine other cases, with NSLog() , have nothing to do. Conditional statements can help the compiler in a wide range of contexts. They are often used by programmers to determine the type of operating system version, the target, the presence of mathematical processors, while maintaining the same "identical" source.

To understand this, as an example, we can utilizzre our constant MIO_DEBUG also to intervene in other areas of the code:

1
2
3
4
5
6
7
8
9
10
/ / Debug if they win the game
/ / With a score of 100 instead of 10000 :)
# Ifdef MIO_DEBUG
score == 100 ) if (score == 100)
# Else
score == 10000 ) if (score == 10000)
# Endif
{
; [HaiVinto self];
}

To finish, here are some examples and variants:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/ / In general there is a
# If expression
/ / If statement similar to the traditional, so with a full expression

/ / Check if a constant is defined
# Ifdef constant

/ / Check if it is NOT a constant defined
# Ifndef constant

/ / Else
# Else

/ / Closing of the block
# Endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ / For example ...
# Define DEBUG 1
# Define MIA_ALTRA_COSTANTE 5

...
# If DEBUG
/ / Fill in this
# Else
/ / Otherwise fill this other
# Endif

# If MIA_ALTRA_COSTANTE> 4
"..." ) ; NSLog (@ "...");
# Endif
1
2
3
4
# Ifndef INCLUDE_MIO_FILE
# Define INCLUDE_MIO_FILE
# Include "mio_file.h"
# Endif

More ...

iPhone FirstApp: Guess the number - Part 1

The first source that I went in his hands was written in Basic and consisted of a few lines of code, for enlightening me. It was a simple game that generated a random number from 1 to 10 and, through keyboard input, proved that the number entered was Maggione, less than or equal to the random number. Despite its rough simplicity remains, for me, one of the best examples - simple, fun and practical - to explain to those who do not know anything about programming what is meant actually for "computer program". So I decided to propose it for Apple iPhone, maybe will help someone ...

More ...

iPhone Objective-C: equivalence in the syntax

The syntax with square brackets in Objective-C is one of the reasons most frustration for those coming from other languages ​​(for objects). However, get used to it, you realize how readable the code becomes too with this mix of classic syntax and the more distinctly Smalltalk. For example we see how the same procedure can be written in both syntax. Take the initialization of a UIAlertView :

1
2
3
4
5
6
7
8
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title"
"Messaggio" message: @ "Message"
delegate: self
"Annulla" cancelButtonTitle: @ "Cancel"
"Ok" , nil ] ; otherButtonTitles: @ "Ok", nil];
; myAlert.tag = 1;
; [MyAlert show];
; [MyAlert release];

Setting the tag potavamo it even so:

1
2
3
4
5
6
7
8
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title"
"Messaggio" message: @ "Message"
delegate: self
"Annulla" cancelButtonTitle: @ "Cancel"
"Ok" , nil ] ; otherButtonTitles: @ "Ok", nil];
1 ] ; [MyAlert SETTINGS: 1];
; [MyAlert show];
; [MyAlert release];

Same thing in reading during the event:

1
2
3
4
5
void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
alertView.tag == 1 ) { // ... if (alertView.tag == 1) {/ / ...
/ / Or
alertView tag ] == 1 ) { // ... if ([alertView tag] == 1) {/ / ...
}

Note: Not all properties or methods have this dual syntax. In the case of ' UIAlertView we have this double feature. For other objects, or in other cases, however, may work only one of two ... but do not ask why (depending on how the developers have defined the interface).

More ...

Trace Actionscript, Objective-C NSLog ()

, usata per il debug delle applicazioni. Taking up the post from Actionscript to Objective-C (where you put the code and comparing the syntax Actionscript and Objective-C) in Actionscript we have the convenient function trace ( ) , used for debugging applications. This function outputs an output to the console Adobe Flash development environment. It is mainly used during development and testing of "movie" / application. In XCode / Objective-C we have: NSLog ( ) . di Actionscript: The syntax of this function is very similar to the trace ( ) in Actionscript:

1
"Sono una linea di debug" ) ; NSLog (@ "I am a debug line");

In Actionscript we have:

1
"Sono una linea di debug" ) ; trace ("I am a debug line");

Apart from the use of sign (@), as you can see, are identical. The differences (and similarities) start when you want to display values ​​of variables, for example in Actionscript we have:

1
2
3
"Coordinata x:" + x + " coordinata y:" + y ) ; trace ("x-coordinate:" + x + "y coordinate:" + y);
/ / Or
"Coordinate: " , x , y ) ; trace ("coordinates", x, y);

In Objective-C we have:

1
"Coordinata x:%i coordinata y:%i" , x, y ) ; NSLog (@ "x-coordinate: the y coordinate%% i", x, y);

che opera sull'Apple System Log. Note: NSLog() actually calls the more generic function NSLogv ( ) that works on Apple System Log. The functions are in fact identical, changing only the input parameters.

o sprintf ( ) . C developers will find very familiar with the formatting of strings, as with printf ( ) or sprintf ( ) . For details, see String Format Specifiers .

More ...

Apple iPhone SDK 3.0: first incompatibility

After you install the SDK 3.0 for Apple iPhone, evidently still not perfectly stable, I immediately noticed some problems, both in the compilation of the code is in the general XCode. The most important - which I promptly reported with a "home" to Apple, developers at the center - the creation of buttons through code. Here's an excerpt of the code is not compatible with SDK 3.0:

1
2
3
4
[ [ UIButton buttonWithType : UIButtonTypeRoundedRect ] initWithFrame : CGRectMake ( 0 , 0 , 100 , 40 ) ] ; UIButton * myButton = [[UIButton buttonWithType: UIButtonTypeRoundedRect] initWithFrame: CGRectMake (0, 0, 100, 40)];
@ "Bottone" forState : UIControlStateNormal ] ; [MyButton setTitle: @ "Button" forState: UIControlStateNormal];
/ / Other settings
myButton ] ; [Self.view addSubview: myButton];

By filling out this code with the SDK 2.2.1 you get a classic button with the label "Button". With the SDK 3.0, the button is created, but the label disappears. la situazione sembra migliorare, nel senso che la label viene resa apparentemente in modo corretto. Using as buttonWithType the type UIButtonTypeCustom the situation seems to improve, in the sense that the label is apparently made ​​correctly. It is still strange to the sudden failure of the type UIButtonTypeRoundedRect . I am still awaiting a response from Apple ... I just novelties; place!

More ...

Hi phone

iphone From today inaugurated a new section (category to be corrected) dedicated to developing applications on Apple iPhone! I state now that many of the items that will be publishing a "cut" in line with the spirit of this blog, that will be mostly for advanced users. However, as I have done this in other topics, try to be as clear as possible and, where necessary, put some "basic concept" useful to a wider audience.

More ...



Stop SOPA