EzDevInfo.com

cocoa-touch interview questions

Top cocoa-touch frequently asked interview questions

Vertically align text to top within a UILabel

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 the text to always be at the top of the UILabel?

image representing a UILabel with vertically-centered text


Source: (StackOverflow)

Is It Possible to NSLog C Structs (Like CGRect or CGPoint)?

I want to be able to debug C structures without having to explicitly type every property that they consist of.

i.e. I want to be able to do something like this:

CGPoint cgPoint = CGPointMake(0,0);
NSLog(@"%@",cgPoint);

Obviously the '%@' won't work, hence the question.


Source: (StackOverflow)

Advertisements

Multiple lines of text in UILabel

Is there a way to have multiple lines of text in UILabel like in the UITextView or should I use the second one instead?


Source: (StackOverflow)

What does the NS prefix mean?

Many classes in Cocoa/Cocoa Touch have the NS prefix. What does it mean? Thanks


Source: (StackOverflow)

Set the maximum character length of a UITextField

How can I set the maximum amount of characters in a UITextField on the iPhone SDK when I load up a UIView?


Source: (StackOverflow)

How to programmatically send SMS on the iPhone?

Does anybody know if it's possible, and how, to programmatically send a SMS from the iPhone, with the official SDK / Cocoa Touch?


Source: (StackOverflow)

Easy way to dismiss keyboard?

I have quite a few controls scattered throughout many table cells in my table, and I was wondering if there's an easier way to dismiss the keyboard without having to loop through all my controls and resigning them all as the first responder. I guess the question is.. How would I get the current first responder to the keyboard?


Source: (StackOverflow)

How to print out the method name and line number and conditionally disable NSLog?

I'm doing a presentation on debugging in Xcode and would like to get more information on using NSLog efficiently.

In particular, I have two questions:

  • is there a way to easily NSLog the current method's name / line number?
  • is there a way to "disable" all NSLogs easily before compiling for release code?

Source: (StackOverflow)

UITableViewCell, show delete button on swipe

How do I get the delete button to show when swiping on a UITableViewCell?

I have followed this post, but the event is never raised and the delete button never appears!


Source: (StackOverflow)

Getting current device language in iOS?

I'd like to show the current language that the device UI is using. What code would I use?

I want this as an NSString in fully spelled out format. (Not @"en_US")

EDIT: For those driving on by, there are a ton of useful comments here, as the answer has evolved with new iOS releases.


Source: (StackOverflow)

Can I embed a custom font in an iPhone application?

This question relates to iOS pre-3.2. As of 3.2 this functionality is easily achievable using samvermette's answer below, and I have changed the Accepted Answer (from commanda to samvermette) to reflect this. I can't give credit to both answers (besides uproots) but they are both good.

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 number 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.


Source: (StackOverflow)

UIView with rounded corners

My login view has a subview which has a UIActivityView and a UILabel saying "Signing In…". This subview has corners which aren't rounded. How can I make them round?

Is there any way to do it using a nib/xib?


Source: (StackOverflow)

How to scale a UIImageView proportionally?

I have a UIImageView and the objective is to scale it down proportionally by giving it either a height or width.

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

//Add image view
[self.view addSubview:imageView];   

//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;

//change width of frame
CGRect frame = imageView.frame;
frame.size.width = 100;
imageView.frame = frame;

The image did get resized but the position is not at the top left. What is the best approach to scaling image/imageView and how do I correct the position?


Source: (StackOverflow)

UITableView didSelectRowAtIndexPath: not being called on first tap

I'm having an issue with UITableView's didSelectRowAtIndexPath.

My table is setup so that when I select row it initializes a new view controller and pushes it.

The first time I tap any row in the table, the method does not get called. Once I select another row, it begins to work as normal.

I have verified this by setting a breakpoint on didSelectRowAtIndexPath. When adding an NSLog to the method I see that when I select the second row that finally pushes the new view controller, I see two log statements appear in the console at the same time.

Any suggestions?


Source: (StackOverflow)