Cocoa-Matic

iPhone, iPod touch, iPad tutorials, examples and sample code. Come and get it!

Thursday, August 12, 2010

UIButton with custom UIImage

Most developers want to steer clear of the default buttons provided in Cocoa because they're just not that attractive. The following code shows how to programmatically create an image button and define an action for when the button is pressed.
// Create button and set frame
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
// Set button image, in this case "my_image.png"
[myButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"my_image" ofType:@"png"]] forState:UIControlStateNormal];
// Call buttonWasPressed function when button is touched
[myButton addTarget:self action:@selector(buttonWasPressed) forControlEvents:UIControlEventTouchUpInside];
// Add the button to view
[self.view addSubview:myButton];

Labels: , ,

Thursday, August 5, 2010

UILabel dynamic sizing based on string

Here's how to create a UILabel that dynamically resizes to fit the NSString that is loaded into it:
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
self.titleLabel = tmpTitleLabel;
[tmpTitleLabel release];
self.titleLabel.textColor = [UIColor blackColor];
self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
self.titleLabel.numberOfLines = 0;
self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
self.titleLabel.text = self.myTitleString;
 
//Calculate the expected size based on the font and linebreak mode of label
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [self.myTitleString sizeWithFont:self.titleLabel.font constrainedToSize:maximumLabelSize lineBreakMode:self.titleLabel.lineBreakMode];
 
//Adjust the label the the new height
CGRect newFrame = self.titleLabel.frame;
newFrame.size.height = expectedLabelSize.height;
self.titleLabel.frame = newFrame;
And here's the explanation of what's going on with this code:
Read more »

Labels: , , ,

Wednesday, August 4, 2010

Better NSLog implementation

NSLog is an invaluable tool for developers who are looking to debug and/or troubleshoot code. It outputs to the debugger console during the application runtime. Before I show a better way to use the NSLog function, let's go over the basics with an example:

int myInt = 100;
NSLog(@"The value of myInt: %i", myInt);
This will output:
The value of myInt: 100

Depending on what type of value you're trying to print, you'll have to use different Format Specifiers (%i in the previous example). Here's a list of common Format Specifiers:
Read more »

Labels:

Tuesday, August 3, 2010

UITextField: Only allow numeric entry using NSScanner

Have a UITextField and want to prevent the user from entering anything but numbers (and decimal point)? This quick function utilizing the UITextFieldDelegate will do the trick.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

 NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
 
 // This allows backspace
 if ([resultingString length] == 0) {
  return true;
 }

 double holder;
 NSScanner *scan = [NSScanner scannerWithString: resultingString];
 
 return [scan scanDouble: &holder] && [scan isAtEnd];
}
Read more »

Labels: , , ,


« Older Entries  
Newer Entries »