Top ios Questions

List of Tags

I have a UILabel with space for two lines of text. Sometimes, when the text is too short, this text is displayed in the vertical center of the label.

How do I vertically align my text to be always at the top of the UILabel?

alt text

Answered By: nevan king ( 850)

There's no way to set the vertical align on a UILabel, but you can get the same effect by changing the label's frame. I've made my labels orange so you can see clearly what's happening.

Here's the quick and easy way to do this:

    [myLabel sizeToFit];

sizeToFit to squeeze a label

If you have a label with longer text that will make more than one line, set numberOfLines to 0 (zero here means an unlimited number of lines).

    myLabel.numberOfLines = 0;
    [myLabel sizeToFit];

Longer label text with sizeToFit

Longer Version

I'll make my label in code so that you can see what's going on. You can set up most of this in Interface Builder too. My setup is a View Based App with a background image I made in Photoshop to show margins (20 points). The label is an attractive orange color so you can see what's going on with the dimensions.

- (void)viewDidLoad
    [super viewDidLoad];

    // 20 point top and left margin. Sized to leave 20 pt at right.
    CGRect labelFrame = CGRectMake(20, 20, 280, 150);
    UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
    [myLabel setBackgroundColor:[UIColor orangeColor]];

    NSString *labelText = @"I am the very model of a modern Major-General, I've information vegetable, animal, and mineral";
    [myLabel setText:labelText];

    // Tell the label to use an unlimited number of lines
    [myLabel setNumberOfLines:0];
    [myLabel sizeToFit];

    [self.view addSubview:myLabel];

Some limitations of using sizeToFit come into play with center- or right-aligned text. Here's what happens:

    // myLabel.textAlignment = UITextAlignmentRight;
    myLabel.textAlignment = UITextAlignmentCenter;

    [myLabel setNumberOfLines:0];
    [myLabel sizeToFit];

enter image description here

The label is still sized with a fixed top-left corner. You can save the original label's width in a variable and set it after sizeToFit, or give it a fixed width to counter these problems:

    myLabel.textAlignment = UITextAlignmentCenter;

    [myLabel setNumberOfLines:0];
    [myLabel sizeToFit];

    CGRect myFrame = myLabel.frame;
    // Resize the frame's width to 280 (320 - margins)
    // width could also be myOriginalLabelFrame.size.width
    myFrame = CGRectMake(myFrame.origin.x, myFrame.origin.y, 280, myFrame.size.height);
    myLabel.frame = myFrame;

label alignment

Note that sizeToFit will respect your initial label's minimum width. If you start with a label 100 wide and call sizeToFit on it, it will give you back a (possibly very tall) label with 100 (or a little less) width. You might want to set your label to the minimum width you want before resizing.

Correct label alignment by resizing the frame width

Some other things to note:

Whether lineBreakMode is respected depends on how it's set. UILineBreakModeTailTruncation (the default) is ignored after sizeToFit, as are the other two truncation modes (head and middle). UILineBreakModeClip is also ignored. UILineBreakModeCharacterWrap works as usual. The frame width is still narrowed to fit to the rightmost letter.

My Original Answer (for posterity/reference):

This uses the NSString method sizeWithFont:constrainedToSize:lineBreakMode: to calculate the frame height needed to fit a string, then sets the origin and width.

Resize the frame for the label using the text you want to insert. That way you can accommodate any number of lines.

CGSize maximumSize = CGSizeMake(300, 9999);
NSString *dateString = @"The date today is January 1st, 1999";
UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize dateStringSize = [dateString sizeWithFont:dateFont 

CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);

self.dateLabel.frame = dateFrame;

This page has some different code for the same solution:

Ian Terrell

Are there any repositories around for open sourced iPhone and iPad components?

For instance, I have found myself needing to create several new types of table cells to mimic some of Apple's existing functionality (for instance, all the different types of table cells present in the Settings application). I can't imagine I'm alone here.

Where do you go to find open sourced reusable components, or do you just write and hoard your own?

Update: I know there are open source full projects around (see this question), but rummaging through them and picking and choosing still leads to significant duplication of effort.

Meta: There's a site for that!

There's a new nicely browsable list of iOS controls at

Ongoing List

Here are some libraries that I've found or been told about (even answered here) since asking this question:

  • CocoaHelpers -- Extensions to common classes
  • MBProgressHUD -- Replacement for the undocumented UIProgressHUD
  • cocos2d for iPhone -- 2d game engine
  • TouchCustoms -- Memory management, ratings, progress bars, more (GitHub Offline)
  • s7graphview -- Graphing
  • core-plot -- More graphing
  • HTFramework -- Reusable views
  • EGOTableViewPullRefresh -- Pull to refresh like Twitter (Tweetie 2)
  • PullToRefresh -- Another pull to refresh implementation
  • MGSplitViewController -- UISplitViewController replacement for the iPad
  • AQGridView -- A UITableView-style replacement that supports grids
  • DDActionHeaderView -- Combine the core concept of UIToolbar and UINavigationBar
  • DDAlertPrompt -- UIAlertView subclass providing UITextFields for user/password inputs
  • ASIHTTPRequest -- An easy to use wrapper around the CFNetwork API -- No Longer Active
  • AFNetworking -- Popular delightful networking library for iOS.
  • RestKit -- A modern framework for implementing RESTful web services clients on iOS.
  • ShareKit -- A quick way to integrate Twitter,Facebook, etc. into your app.
  • iDev Recipes - Open source code for the blog
  • QuickDialog - Easy way to create dialogs, either in Obj-C or JSON
  • Three20 -- Custom UI classes used in the Facebook application
Answered By: MattDiPasquale ( 112)

Here are some good iOS open-source libraries/frameworks & projects:

UI Libraries & Frameworks


  • General
  • REST
    • RestKit: Makes interacting with RESTful web services simple, fast and fun
    • HTTPRiot: A simple HTTP REST Library
    • ObjectiveResource: Makes interacting with Ruby on Rails applications dead simple
  • Sockets
    • AsyncSocket: TCP/IP socket networking library that wraps CFSocket and CFStream
    • Zimt: HTML5 Websockets

Core Data


Game Libraries & Frameworks

Testing Libraries & Frameworks

I would like to check to see if I have an Internet connection on the iPhone using the Cocoa Touch libraries.

I came up with a way to do this using an NSURL. The way I did it seems a bit unreliable (because even Google could one day be down and relying on a 3rd party seems bad) and while I could check to see for a response from some other websites if Google didn't respond, it does seem wasteful and an unnecessary overhead on my application.

- (BOOL) connectedToInternet
    NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@""]];
    return ( URLString != NULL ) ? YES : NO;

Is what I have done bad? (Not to mention stringWithContentsOfURL is deprecated in 3.0) And if so what is a better way to accomplish this?

Answered By: iWasRobbed ( 528)

Use a simple library to do it: (ARC and GCD Compatible Reachability)

OR... Do it yourself:

1) Add SystemConfiguration framework to the project but don't worry about including it anywhere

2) Add Reachability.h and Reachability.m to the project (you can get those here)

3) Add @class Reachability; to the .h file of where you are implementing the code

4) Create a couple instances to check in the interface section of the .h file:

Reachability* internetReachable;
Reachability* hostReachable;

5) Add a method in the .h for when the network status updates:

-(void) checkNetworkStatus:(NSNotification *)notice;

6) Add #import "Reachability.h" to the .m file where you are implementing the check

7) In the .m file of where you are implementing the check, you can place this in one of the first methods called (init or viewWillAppear or viewDidLoad etc):

-(void) viewWillAppear:(BOOL)animated
    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    // check if a pathway to a random host exists
    hostReachable = [[Reachability reachabilityWithHostName: @""] retain];
    [hostReachable startNotifier];

    // now patiently wait for the notification

8) Set up the method for when the notification gets sent and set whatever checks or call whatever methods you may have set up (in my case, I just set a BOOL)

-(void) checkNetworkStatus:(NSNotification *)notice
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
        case NotReachable:
            NSLog(@"The internet is down.");
            self.internetActive = NO;

        case ReachableViaWiFi:
            NSLog(@"The internet is working via WIFI.");
            self.internetActive = YES;

        case ReachableViaWWAN:
            NSLog(@"The internet is working via WWAN.");
            self.internetActive = YES;


    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
        case NotReachable:
            NSLog(@"A gateway to the host server is down.");
            self.hostActive = NO;

        case ReachableViaWiFi:
            NSLog(@"A gateway to the host server is working via WIFI.");
            self.hostActive = YES;

        case ReachableViaWWAN:
            NSLog(@"A gateway to the host server is working via WWAN.");
            self.hostActive = YES;


9) In your dealloc or viewWillDisappear or similar method, remove yourself as an observer

-(void) viewWillDisappear:(BOOL)animated
    [[NSNotificationCenter defaultCenter] removeObserver:self];

Note: There might be an instance using viewWillDisappear where you receive a memory warning and the observer never gets unregistered so you should account for that as well.

I know about the HIG (which is quite handy!), but what programming practices do you use when writing Objective-C, and more specifically when using Cocoa (or CocoaTouch).

Answered By: Kendall Helmstetter Gelner ( 400)

There are a few things I have started to do that I do not think are standard:

1) With the advent of properties, I no longer use "_" to prefix "private" class variables. After all, if a variable can be accessed by other classes shouldn't there be a property for it? I always disliked the "_" prefix for making code uglier, and now I can leave it out.

2) Speaking of private things, I prefer to place private method definitions within the .m file in a class extension like so:

#import "MyClass.h"

@interface MyClass ()
- (void) someMethod;
- (void) someOtherMethod;

@implementation MyClass

Why clutter up the .h file with things outsiders should not care about? The empty () works for private categories in the .m file, and issues compile warnings if you do not implement the methods declared.

3) I have taken to putting dealloc at the top of the .m file, just below the @synthesize directives. Shouldn't what you dealloc be at the top of the list of things you want to think about in a class? That is especially true in an environment like the iPhone.

3.5) In table cells, make every element (including the cell itself) opaque for performance. That means setting the appropriate background color in everything.

3.6) When using an NSURLConnection, as a rule you may well want to implement the delegate method:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse *)cachedResponse
      return nil;

I find most web calls are very singular and it's more the exception than the rule you'll be wanting responses cached, especially for web service calls. Implementing the method as shown disables caching of responses.

Also of interest, are some good iPhone specific tips from Joseph Mattiello (received in an iPhone mailing list). There are more, but these were the most generally useful I thought (note that a few bits have now been slightly edited from the original to include details offered in responses):

4) Only use double precision if you have to, such as when working with CoreLocation. Make sure you end your constants in 'f' to make gcc store them as floats.

float val = someFloat * 2.2f;

This is mostly important when someFloat may actually be a double, you don't need the mixed-mode math, since you're losing precision in 'val' on storage. While floating-point numbers are supported in hardware on iPhones, it may still take more time to do double-precision arithmetic as opposed to single precision. References:

On the older phones supposedly calculations operate at the same speed but you can have more single precision components in registers than doubles, so for many calculations single precision will end up being faster.

5) Set your properties as nonatomic. They're atomic by default and upon synthesis, semaphore code will be created to prevent multi-threading problems. 99% of you probably don't need to worry about this and the code is much less bloated and more memory-efficient when set to nonatomic.

6) SQLite can be a very, very fast way to cache large data sets. A map application for instance can cache its tiles into SQLite files. The most expensive part is disk I/O. Avoid many small writes by sending BEGIN; and COMMIT; between large blocks. We use a 2 second timer for instance that resets on each new submit. When it expires, we send COMMIT; , which causes all your writes to go in one large chunk. SQLite stores transaction data to disk and doing this Begin/End wrapping avoids creation of many transaction files, grouping all of the transactions into one file.

Also, SQL will block your GUI if it's on your main thread. If you have a very long query, It's a good idea to store your queries as static objects, and run your SQL on a separate thread. Make sure to wrap anything that modifies the database for query strings in @synchronize() {} blocks. For short queries just leave things on the main thread for easier convenience.

More SQLite optimization tips are here, though the document appears out of date many of the points are probably still good;

Airsource Ltd

I would like to have an app include a custom font for rendering text, load it, and then use it with standard UIKit elements like UILabel. Is this possible?

I found these links:

but these would require me to render each glyph myself, which is a bit too much like hard work, especially for multi-line text.

I've also found posts that say straight out that it's not possible, but without justification, so I'm looking for a definitive answer.

EDIT - failed -[UIFont fontWithName:size:] experiment

I downloaded Harrowprint.tff (downloaded from here) and added it to my Resources directory and to the project. I then tried this code:

UIFont* font = [UIFont fontWithName:@"Harrowprint" size:20];

which resulted in an exception being thrown. Looking at the TTF file in Finder confirmed that the font name was Harrowprint.

EDIT - there have been a number of replies so far which tell me to read the documentation on X or Y. I've experimented extensively with all of these, and got nowhere. In one case, X turned out to be relevant only on OS X, not on iPhone. Consequently I am setting a bounty for this question, and I will award the bounty to the first person who provides an answer (using only documented APIs) who responds with sufficient information to get this working on the device. Working on the simulator too would be a bonus.

EDIT - it appears that the bounty auto-awards to the answer with the highest nunber of votes. Interesting. No one actually provided an answer that solved the question as asked - the solution that involves coding your own UILabel subclass doesn't support word-wrap, which is an essential feature for me - though I guess I could extend it to do so.

Answered By: commanda ( 167)

Edit: As of iOS 3.2, this functionality is built in. If you need to support pre-3.2, you can still use this solution.

I created a simple module that extends UILabel and handles loading .ttf files. I released it opensource under the Apache license and put it on github here: git://

The important files are FontLabel.h and FontLabel.m.

It uses some of the code from Genericrich's answer above.

Browse the source here:

I'm getting the following warning by the ARC compiler:

"performSelector may cause a leak because its selector is unknown".

Here's what I'm doing:

[_controller performSelector:NSSelectorFromString(@"someMethod")];

Why do I get this warning? I understand the compiler can't check if the selector exists or not, but why would that cause a leak? And how can I change my code so that I don't get this warning anymore?

Answered By: sergio ( 107)

My guess about this is this: since the selector is unknown to the compiler, ARC cannot enforce proper memory management.

In fact, there are times when memory management is tied to the name of the method by a specific convention. Specifically, I am thinking of convenience constructors versus make methods; the former return by convention an autoreleased object; the latter a retained object. The convention is based on the names of the selector, so if the compiler does not know the selector, then it cannot enforce the proper memory management rule.

If this is correct, I think that you can safely use your code, provided you make sure that everything is ok as to memory management (e.g., that your methods do not return objects that they allocate).

Michael Pryor

How can you beta test an iPhone app? I can get it on my own device, and anyone that gives me a device, I can run it on theirs, but is there a way to do a limited release via the app store for beta testing?

Answer: iOS 4.0 and later: Instructions on wirelessly distributing your application to beta testers.

Related: Also, see this question on getting your app onto phones without using the App Store.

Answered By: Arne Evertsson ( 304)

Creating ad-hoc distribution profiles

The instructions that Apple provides are not very concise or clear. This is how I created a general provisioning profile that will work with multiple apps, and added a beta tester.

My setup:

  • Xcode 3.2.1
  • iPhone SDK 3.1.3

Before you get started, make sure that..

  • You can run the app on your own iPhone through Xcode.

Step A: Add devices to the Provisioning Portal

  1. Send an email to each beta tester with the following message:

    To get my app on onto your iPhone I need some information about your phone. Guess what, there is an app for that!

    Click on the below link and install and then run the app.

    This app will create an email. Please send it to me.

  2. Collect all the UDIDs from your testers.

  3. Go to the Provisioning Portal.

  4. Go to the section Devices.

  5. Click on the button Add Devices and add the devices previously collected.

Step B: Create a new provisioning profile

  1. Start the Mac OS utility program Keychain Access.

  2. In its main menu, select Keychain Access / Certificate Assistant / Request a Certificate From a Certificate Authority...

  3. The dialog that pops up should aready have your email and name it it.

  4. Select the radio button Saved to disk and Continue.

  5. Save the file to disk.

  6. Go back to the Provisioning Portal.

  7. Go to the section Certificates.

  8. Go to the tab Distribution.

  9. Click the button Request Certificate.

  10. Upload the file you created with Keychain Access: CertificateSigningRequest.certSigningRequest.

  11. Click the button Aprove.

  12. Refresh your browser until the status reads Issued.

  13. Click the Download button and save the file distribution_identify.cer.

  14. Doubleclick the file to add it to the Keychain.

  15. Backup the certificate by selecting its private key and the File / Export Items....

  16. Go back to the Provisioning Portal again.

  17. Go to the section Provisioning.

  18. Go to the tab Distribution.

  19. Click the button New Profile.

  20. Select the radio button Ad hoc.

  21. Enter a profile name, I named mine Evertsson Common Ad Hoc.

  22. Select the app id. I have a common app id to use for multiple apps: Evertsson Common.

  23. Select the devices, in my case my own and my tester's.

  24. Submit.

  25. Refresh the browser until the status field reads Active.

  26. Click the button Download and save the file to disk.

  27. Doubleclick the file to add it to Xcode.

Step C: Build the app for distribution

  1. Open your project in Xcode.

  2. Open the Project Info pane: In Groups & Files select the topmost item and press Cmd+I.

  3. Go to the tab Configuration.

  4. Select the configuration Release.

  5. Click the button Duplicate and name it Distribution.

  6. Close the Project Info pane.

  7. Open the Target Info pane: In Groups & Files expand Targets, select your target and press Cmd+I.

  8. Go to the tab Build.

  9. Select the Configuration named Distribution.

  10. Find the section Code Signing.

  11. Set the value of Code Signing Identity / Any iPhone OS Device to iPhone Distribution.

  12. Close the Target Info pane.

  13. In the main window select the Active Configuration to Distribution.

  14. Create a new file from the file template Code Signing / Entitlements.

  15. Name it Entitlements.plist.

  16. In this file, uncheck the checkbox get-task-allow.

  17. Bring up the Target Info pane, and find the section Code Signing again.

  18. After Code Signing Entitlements enter the file name Entitlements.plist.

  19. Save, clean, and build the project.

  20. In Groups & Files find the folder MyApp / Products and expand it.

  21. Right click the app and select Reveal in Finder.

  22. Zip the .app file and the .mobileprovision file and send the archive to your tester.

    Here is my app. To install it onto your phone:

    1. Unzip the archive file.

    2. Open iTunes.

    3. Drag both files into iTunes and drop them on the Library group.

    4. Sync your phone to install the app.

Done! Phew. This worked for me. So far I've only added one tester.

When you tap a row in a UITableView, the row is highlighted and selected. Is it possible to disable this so tapping a row does nothing?

Answered By: Martin Gordon ( 515)

All you have to do is set the selection style on the UITableViewCell instance using either:

cell.selectionStyle = UITableViewCellSelectionStyleNone;


[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

More info here and here


It's been a while since I've had to adjust project build settings. After upgrading to a recent SDK I'm having trouble building my ad hoc distribution configuration.

Build generates this warning and error:

warning: iPhone apps should include an armv6 architecture (current ARCHS = "armv7")

iPhone/iPod Touch: application executable is missing a required architecture. At least one of the following architecture(s) must be present: armv6 (-19033)

However in my project I thought I had things set correctly:

  • Architectures is: Standard (armv6 armv7)
  • Base SDK: Latest iOS (currently set to iOS 4.2)
  • Valid Architectures: armv6 armv7

alt text

I have cleaned all targets.

Appreciate any tips. Thanks!

Answered By: Nick ( 383)

If using Xcode 4.2 or higher, try the following:

  1. Click your Project name (in the left column), followed by the Target:

    enter image description here

  2. Click the 'Build Settings' tab (in the right column):

    enter image description here

  3. Click the 'Release' or 'Distribution' row under 'Architectures', and choose 'Other...':

    enter image description here

  4. Double click the highlighted row named '$(ARCHS_STANDARD_32_BIT)' in the popover that appears, and replace it by typing 'armv6'. Then add a new row with the plus button in the bottom left of the popover, and type 'armv7', then click Done:

    enter image description here

Update: you should add armv7s to target the iPhone 5 as well. (And drop armv6 if building with Xcode 4.5 or higher, which no longer supports armv6.)

That's it. You should now be able to build/archive without generating errors.

If it still doesn't work, see this answer from justinxreese, which suggests adding entries for armv6 and armv7 under "Required Device Capabilities" in your info.plist file.

Robert Gould

I began an iPhone project the other day with a silly development code name, and now I want to change the name of the project since its nearly finished.

But I'm not sure how to do this with Xcode, trying the obvious of changing the application's name in the pinfo, causes the signing process to go wrong (I think...) and my app won't launch giving me a Launcher error.

I guess I could make a new project and copy paste everything over, but it seems so primitive, that I'm hoping for a more civilized solution.


Answered By: Giao ( 303)
  1. Go to Targets in Xcode
  2. "Get Info" on your project's target (your current silly development name)
  3. Search for "Product Name" under "Packaging". Change the value of that what you want the new program name is going to be.