RSS Feed

Resize table cell according to text length

Posted on

Some times we need to customize our table cell’s height according to text length. For long text we need to make a bigger cell and vice versa. Here is the method to do this:

1. define you table cell’s size and font:

#define CONST_Cell_height 50.0f
#define CONST_Cell_width 270.0f
#define CONST_textLabelFontSize     16
#define CONST_detailLabelFontSize   14

2. Here is the method to calculate table cell width:

– (int) heightOfCellWithTitle :(NSString*)titleText
CGSize titleSize = {0, 0};
CGSize subtitleSize = {0, 0};

if (titleText && ![titleText isEqualToString:@””])
titleSize = [titleText sizeWithFont:[UIFont boldSystemFontOfSize:16]
constrainedToSize:CGSizeMake(CONST_Cell_width, 4000)

if (subtitleText && ![subtitleText isEqualToString:@””])
subtitleSize = [subtitleText sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(CONST_Cell_width, 4000)

descriptionLabelHeight = subtitleSize.height;

return titleSize.height + subtitleSize.height;

3. Use this in your table height for row:

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *title = @”Yor cell title”;
NSString *subtitle =@”Your cell subtitle”;

int height =  “some constant” + [self heightOfCellWithTitle:title andSubtitle:subtitle]; // some constant is used to adjust cell height. may be zero.

return (height < CONST_Cell_height ? CONST_Cell_height : height);


it will resize the table cell according to text size.

!! Enjoy !!

How to create a password-protected (encrypted) disk image in Mac OS X 10.3 or later

You can move files to or from an encrypted disk image as easily as you can from a non-encrypted disk image. Follow these steps to create an encrypted disk image:

  1. Open Disk Utility (/Applications/Utilities/).
  2. Mac OS X 10.4, 10.5 or later: Click the New Image button, or choose New > Blank Disk Image from the Disk Utility File menu.
    Mac OS X 10.3 through 10.3.9: Choose New from the Disk Utility Image menu.
  3. Enter a name in the Save As field. This name is used for the disk image (.dmg) file.
  4. Change the save destination if you wish to.
  5. Select a size for the image file from the Volume Size pop-up menu (Size in Mac OS X 10.3) .
  6. Choose a volume format if you don’t want to use the default Mac OS X Extended (Journaled).
  7. Choose an image format. You can use “sparse disk image” for a disk image that only uses as much space as it needs, rather than a set amount of space. If you’re not sure, use “read/write disk image” choice.
  8. Choose 128-bit AES (and/or 256-bit AES in Mac OS X 10.5 or later) from the Encryption pop-up menu to encrypt the image’s contents with a password. If you don’t choose an encryption, your new image won’t be encrypted.
  9. Click the Create button.
  10. Enter and verify a good password in the dialog window that appears. This password will be saved in your keychain by default, or you can deselect “Remember password (add to keychain)” if you don’t want that. You can store the password in the keychain both for convenience and for reducing risk of password loss.
  11. Click OK.

Important: If you forget the password, data stored in the encrypted disk image cannot be retrieved. If you have saved the password in the keychain file, the password will be available to you there.


!! Cheers !!

XCode4 hangs at “Attaching to ….”

Posted on

Sometimes when you rename you app XCode4 hang while running app on simulator. And it stuck at “Attaching to ‘app name’ “. Here is the step how to fix it:

1. Clicking on the project name in the left pane (at the very top). This will bring up a new menu to the right, something like the project/ target editors in XCode4

2. Click on Target and Click on Build Settings up at the top.

3. Under Packaging make sure your product name is the same for every build, and equal to whatever it says it’s attaching to. Eg if XCode is Hanging at “Attaching to Lasso Tool” but your product name is “Alter Image” then it won’t build.

That was exactly my problem because I renamed my app half way through development.

!! Cheers !!

Masking of Image on another image

Posted on

Here is a small core graphics method for masking of images:


– (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGContextRef mainViewContentContext;
CGColorSpaceRef colorSpace;

colorSpace = CGColorSpaceCreateDeviceRGB();

// create a bitmap graphics context the size of the image
mainViewContentContext = CGBitmapContextCreate (NULL, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

// free the rgb colorspace

CGImageRef maskingImage = [maskImage CGImage];
CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, maskImage.size.width, maskImage.size.height), maskingImage);
CGContextDrawImage(mainViewContentContext, CGRectMake(0, 0, image.size.width, image.size.height), self.CGImage);

// Create CGImageRef of the main view bitmap content, and then
// release that bitmap context
CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);

// convert the finished resized image to a UIImage
UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
// image is retained by the property setting above, so we can
// release the original

maskingImage = nil;
// return the image
return theImage;


For more detail about masking please checkout Quartz 2D developer guide:


!! Enjoy !!

Limiting Zoom level in UIPinchGestureRecognizer

Posted on

Sometimes when we are zooming image by using Pinch Gesture, we can’t handle the size of zooming. Here is a small code to do this.

The code below assumes there is an instance variable CGFloat lastScale and that a view has been set for the UIPinchGestureRecognizer.

– (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {

CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@”transform.scale”] floatValue];

// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;

CGFloat newScale = 1 –  (lastScale – [gestureRecognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;

lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call

!! Enjoy !!

Multiple Skype Accounts on Mac OS X

Posted on

I needed to run multiple Skype instances on Mac OS X and here is the way to do it.

1. Create a new user from System Configuration > Users.

2. Login with this new user, might not be necessary, but I did it to see if everything is ok.

3. Go back to your normal user and open a Terminal.

4. Type su newuser. It will ask for the user’s password. Replace newuser with the user you created

5. Type /Applications/ and the new Skype instance should appear in the dock

You have to leave the Terminal open, if you close the terminal the other Skype will close. Also note, that preferences and files are saved in the newuser‘s directory.

nohup /Applications/ might also work to run Skype in the background so it’s not closed when the terminal is closed. Hope it saves you some time.




Posted on

Just under one year ago, Apple shocked the computing world with a 9.7-inch touchscreen tablet that few truly expected.

Some called the original Apple iPad a large-format iPhone. Others berated the name and made jokes that were not remotely funny.

The early reviews were marginal at best – we handed the device a solid four stars. Technical folks decried the lack of Adobe Flash and the missing cameras.

Now, 60,000 apps later (according to Apple, who counts every conceivable option) and just a few weeks after the first real Android 3.0 tablet contender hit the streets (Motorola Xoom), the iPad 2 has sauntered onto the playing field.

At 241mm tall, 186mm wide, and 8.6mm thick, the iPad 2 is just a hair smaller than the original iPad and it’s thinner than the iPhone 4. It has a curved edge that makes it look a bit more ‘space age’ and, surprisingly, easier to grasp because you can curve your fingers more easily around the bezel.
With rounded edges, iPad 2 has a more contoured look, shunning the straight edges of the original iPad.  According to Kevin Keller of IHS iSuppli, the slim appearance of iPad 2 not only makes the first iPad seem bulky, but also makes other tablet devices seem gargantuan compared to iPad 2. 

Despite having a slimmer battery, iPad 2 is roughly 15 percent more efficient in terms of power usage compared with the original.  Foregoing the two thick cells of the first iPad, Apple used three thinner, slimmer, cells, reducing the battery’s thickness.

For the screen, Apple did away with the metal sheet structure in favor of a touchscreen with an improved glass layer.  The glass on iPad 2 is thinner, more flexible and durable, than the screen of the original iPad according to IHS tests.

Compounding all of this is the cost advantage Apple has over its competitors.  UBM indicates that Apple reportedly only spent $270 to manufacture the 32GB iPad 2, cheaper compared to the estimated cost for the Motorola Xoom.

iPad 2 is smaller, faster and more feature-rich than the first generation iPad.  Moreover, with low costs, both to manufacture and to offer consumers, others can’t match, Apple and iPad should continue to dominate the tablet market.

Have you purchased iPad 2? 🙂