Showing posts with label objective-c. Show all posts
Showing posts with label objective-c. Show all posts

Jun 16, 2014

Format NSString to left pad zeroes in Objective C

Posted Jun 16, 2014
01, 02, 03, 04, 05..
int num = 1;
[NSString stringWithFormat:@"%02d", num];

001, 002, 003, 004, 005..
[NSString stringWithFormat:@"%03d", num];

0001, 0002, 0003, 0004, 0005..
[NSString stringWithFormat:@"%04d", num];

You get the pattern? Good!


Jan 10, 2013

[Solved] Property 'cornerRadius' cannot be found in forward class object 'CALayer *'

Posted Jan 10, 2013
When I tried the following:

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
myLabel.layer.cornerRadius = 2.0f;
[myLabel release];

I get an error: Property 'cornerRadius' cannot be found in forward class object 'CALayer *'

The solution is to import the QuartzCore framework:

#import <QuartzCore/QuartzCore.h>

Happy coding!

Jan 8, 2013

Get GMT in iOS using NSTimezone

Posted Jan 8, 2013
There are cases that you want to get the GMT string of an iPhone / iPad device. Here's how you can do it:

  NSTimeZone *localTime = [NSTimeZone systemTimeZone];
  CGFloat gmt = ([localTime secondsFromGMT]/60.f/60.f);
  
  NSMutableString *gmtStr = [[[NSMutableString alloc] init] autorelease];
  int hours;
  if (gmt < 0) {
    hours = ceil(gmt);
  } else {
    hours = floor(gmt);
  }
  
  int minutes = abs(round((gmt - hours) * 60));
  
  if (gmt >= 0) {
    [gmtStr appendFormat:@"+%d",hours];
  } else {
    [gmtStr appendFormat:@"%d",hours];
  }
  
  if (minutes > 0) {
    [gmtStr appendFormat:@":%02d", minutes];
  }
  
  NSLog(@"GMT is %@", gmtStr);

Jan 4, 2013

UIView is blurred after setting its width (autoresizingmask / anchoring related)

Posted Jan 4, 2013
Blurring of views usually occurs when the device is trying to fit 1 point to two pixels. This happens when you have an x or y origin with a decimal place. X or Y origins can have unintentional decimal places when you're setting the "center" property of the view instead of the actual frame. It is more apparent in non-retina devices because they have fewer pixels.

Unintentional decimal places in a view's origin can also occur when you set the width of a view that has an autoresizingmask where the view is automatically anchored to its parent and the system is adjusting the origin for you.

In my experience, I get a blurred view in landscape mode when my I set the width of my anchored view to an odd number, so I just make sure I always set the width to an even number.
E.g.

CGRect myRect = self.view.frame;
int myRectInt = (int) roundf(myRect.size.width);
if (myRectInt % 2 != 0) {
  myRect.size.width = myRectInt + 1;
}
self.view.frame = myRect;

Happy coding!

Converting CGFloat to int [Objective-C]

There are cases that you want to convert float numbers to integer. One example is when you're using a frame's attribute, say, frame.size.width and you want to know if it's odd or even number. Before you can do a modulo operation, the numbers must be in integer format.

The easiest way is to round the float value first and typecast it to int.
E.g.


int integerWidth = (int) roundf(myFrame.size.width);

Hope this helps!

Nov 6, 2012

[Solved] iOS didrotatefrominterfaceorientation is not called

Posted Nov 6, 2012
There are cases that did rotate method (didRotateFromInterfaceOrientation:) of a view controller is not triggered. A workaround is to manually call it from you main view controller, but if you don't have time, an easier fix is to use notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

Place the above in your controllers view will appear, and don't forget to unregister it in view did disappear.

- (void)didRotate:(NSNotification *)notification {
  //do your post-rotation logic here

}

May 24, 2012

Singleton Pattern in Objective-C (iOS) and release method (oneway) issue

Posted May 24, 2012
Apple gives a good sample on how to create a singleton class; I've modified it a bit for iOS programming:

static MyGizmoClass *sharedInstance = nil;

+ (MyGizmoClass*) sharedInstance
{
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }
    return sharedInstance;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedInstance] retain];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (NSUInteger)retainCount
{
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (oneway void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}


Modifications:
  1. Used "sharedInstance" method and variable name to make it easier to copy paste.
  2. - (void) release to - (oneway void) release
    The singleton sample produces a warning in the release method because NSObject uses the "oneway" return type modifier. It's basically an indication that the method is used for asynchronous messages. For more details on what oneway does, toodarkpark.net offers a good explanation.

Jul 20, 2011

How to make UILabel's text bold

Posted Jul 20, 2011
If you want to retain the system font and make it bold:

[myLabel setFont:[UIFont boldSystemFontOfSize:16]];