Archive for the ‘iPhone’ Category

OpenGL ES iPhone Tutorials

Friday, September 4th, 2009

We've been learning the OpenGL ES platform over the last few days here in an attempt to prepare for our next iPhone app which will be a game. It's a pretty straightforward tool once you get used to it, but therein lies the problem: we can hardly find any OpenGL ES 1.1 tutorials that designed wholly with the iPhone in mind!

This is where a ton of Google and link following brings us to an amazing tutorial series by a gentleman named Simon Maurice. He has written extensively about using OpenGL ES for the iPhone and really breaks it down so that any developer can sit down and learn it.

Check it out here: iPhone OpenGL ES Tutorial Series.

Thanks Simon! Keep up the good work.

Free iPhone Programming Class & Lectures

Tuesday, September 1st, 2009

Stanford University has offered a wonderful course in iPhone Programming for free online through iTunes U.

It's a great series of lectures, sample applications, and documents that are very helpful for developers who are already familiar with the basics of OOP. It provides a very solid foundation for learning how to develop iPhone Applications.

To get the coursework and videos, go to: CS193P - iPhone Application Programming

We're not sure how long these resources will be available. Get them while you can!

Resize Multi-line Text to Fit UILabel on iPhone

Thursday, July 30th, 2009

The UILabel is a great tool for displaying lines of text in any view and is easy to set up using the provided Interface Builder. Unfortunately, there is a deceptive option in the Label Attributes that says "Font Size: Adjust To Fit." This is a great feature because it means that the iPhone will automatically resize your text down in order to fit all of it into the label. Unfortunately, this feature only works when your label's layout is set to single-line. If your text is multiline like ours was, it won't adjust the font size at all!

Well we did some brief searching around to locate a solution to this problem but ended up simply writing our own. We have the following situation:

  • We have dynamic text, the exact quantity of which varies.
  • We want to place that text in a fixed-width and fixed-height UILabel
  • We don't want the text to get cutoff if it doesn't fit perfectly

In order to meet this challenge, we wrote some simple code to automatically resize the text.

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
//Create a string with the text we want to display.
self.ourText = @"This is your variable-length string. Assign it any way you want!";
 
/* This is where we define the ideal font that the Label wants to use.
   Use the font you want to use and the largest font size you want to use. */
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:28];
 
int i;
/* Time to calculate the needed font size.
   This for loop starts at the largest font size, and decreases by two point sizes (i=i-2)
   Until it either hits a size that will fit or hits the minimum size we want to allow (i > 10) */
for(i = 28; i > 10; i=i-2)
{
	// Set the new font size.
	font = [font fontWithSize:i];
	// You can log the size you're trying: NSLog(@"Trying size: %u", i);
 
	/* This step is important: We make a constraint box 
	   using only the fixed WIDTH of the UILabel. The height will
	   be checked later. */ 
	CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
 
	// This step checks how tall the label would be with the desired font.
	CGSize labelSize = [self.ourText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
 
	/* Here is where you use the height requirement!
	   Set the value in the if statement to the height of your UILabel
	   If the label fits into your required height, it will break the loop
	   and use that font size. */
	if(labelSize.height <= 180.0f)
		break;
}
// You can see what size the function is using by outputting: NSLog(@"Best size is: %u", i);
 
// Set the UILabel's font to the newly adjusted font.
msg.font = font;
 
// Put the text into the UILabel outlet variable.
msg.text = self.ourText;

To use the above text, you need to make a "IBOutlet UILabel *msg;" which you assign as the UILabel's Reference Outlet from the interface builder.

And there you have it: easy automatic resizing of multi-line text to fit in your fixed-size UILabel for the iPhone using Objective-C!

Send Email from inside an iPhone App

Tuesday, July 14th, 2009

One of our first challenges with the Ninja Missions iPhone app was to make a simple and quick way for users to share missions with their friends. This is a valuable tool for viral marketing. Through past projects, we know how many people use the 'share this with a friend' links on websites. It's a surprisingly effective free marketing tool, so we wanted to add one to our iPhone.

Our first thought was: How cool would it be to allow people to text it to a friend?!

Unfortunately, Apple does not currently have an API that allows you to open a pre-filled SMS like they do for email. The only way to achieve this would be to use a SMS Gateway provider (like TM4B or Clickatell) and send the SMS through a web-call in the application.

We can open the SMS application through a link with the following code, but there is no way to fill in the message automatically. (See: Apple URL Scheme Reference)

1
2
<a href="sms:">Opens SMS Application</a>
<a href="sms:1-800-555-1234">Opens New SMS Message to 1-800-555-1234</a>

Or, open it from within an app with the following code:

1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:1-800-555-1234"]];

Clearly, just opening a blank text message wasn't going to work in this case, and the price of SMS Gateway Providers couldn't be justified until our app started generating a bit of revenue. So we decided to use the tried and true email link to open a premade email inside of the iPhone Mail application.

To Send Email from inside an iPhone App in Objective-C:

1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:test@test.com?subject=Subject%20Line&body=Body%20Text"]];

Simply put, the above code is the core function needed to open a new email message from within an iPhone App. However, this was not entirely what we needed. We needed to put a NSString generated from our data into the middle of a fixed chunk of text. To do that, we did this:

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
- (IBAction)sendEmail {
	/* We want to include this variable text within the premade body. 
	   You could replace this variable text with a personalized message from the user or remove it. */
 
	/* First, encode the variable text (in this case, the mission) 
	   NOTE: self.theMission is our NSString object which contain's the unescaped mission text
	   Replace this variable with your own variable (if needed). */
	NSString *encodedMission = [self.theMission stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
	/* Make a string that contains the first part of the static text.
	   We start off with the mailto:email?subject=subject&body=text format.
	   Stop right where you want to put in the variable's text.
	   ---------
	   To make this part easy, use a free web-based URL encoder like the one at: 
	   http://meyerweb.com/eric/tools/dencoder/
	   ---------
	   Our first half will give us:
	   To:Test@Test.com
	   Subject: Subject Text Here
	   Body: Fellow Ninja -
	   (line break)
	   */
	NSString *firstHalf = @"mailto:Test@Test.com?subject=Subject%20Text%20Here&body=Fellow%20Ninja%20-%0D%0A%0D%0A";
 
	/* And the second half of the email (the post-variable segment)
	   Encoded the same way we did the first half. This string will decode to:
	   (line break)
	   Yours,
	   (line break)
	   Ninja Master 
	   */
	NSString *lastHalf = @"%0D%0A%0D%0AYours%2C%0D%0A%0D%0ANinja%20Master";
 
	/* Now we concatenate the strings. */
	NSString *mailCode = [NSString stringWithFormat:@"%@%@%@", firstHalf, encodedMission, lastHalf];
 
	/* Now it's time to send it! The following code makes the above string into a 
	   URL and opens the URL causing the Mail program to launch with a premade email. */
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailCode]];
}

We then bind this code to a button action in the interface builder.

It is important to note that this button will not do anything inside the iPhone simulator from the iPhone SDK because the simulator does not have a Mail App. We have to test the app on a developer's iPhone to test the function.

And it works!

iPhone Email Screenshot iPhone Email Screenshot