Friday, June 7, 2013

Sizes - a really good app to resize retina images

There are many apps to resize retina images in the Mac App Store. The best one I've found is called Sizes. It needs me only to drag my images and drop them on the icon at the dock. And that's it. No distractive windows, no inappropriate messages. I don't even need to name files properly, because Sizes does it for me automatically. For example, if I have a file example.png and it is 200x200, then Sizes will create 2 files: example@2x.png which is 200x200 and example.png which is 100x100. Really simple.
There are also some options to have more controll on this app. Here is how it looks like:


More information and download like on the product website: http://www.apptorium.com/products/sizes. Really recommended.

Thursday, April 11, 2013

UIWebView and shadow

Last time I was trying to add a shadow to UIWebView. And it wasn't so obvious, because the standard way didn't work:

    webView.layer.shadowRadius = 8;
    webView.layer.shadowOffset = CGSizeMake(00);
    webView.layer.shadowColor = [[UIColor grayColorCGColor];
    webView.layer.shadowOpacity = 1;

But there is a simple solution. You just have to add these 2 lines:

Wednesday, March 6, 2013

How to make UIButton text left (or right) aligned?

It's a really common question and the answer is:

[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

Of course, there is also:

[[button titleLabel] setTextAlignment:NSTextAlignmentLeft];

The difference is that the first method sets alignment of the button content, and the second one sets the alignment of the text in the label. But the label frame is as wide as the text is. So if the label has only 1 line, it will always be centered.

Tuesday, March 5, 2013

How to check if app is run under iPhone 5 (the one with 4" screen)?

Sometimes you need to check if app is run under iPhone 5 which has higher screen resolution than previous models. This code should be helpful:

- (BOOL)isIPhone5
{
    return [[UIScreen mainScreen] bounds].size.height == 568;
}

Monday, February 4, 2013

Converting string encoding from NSString to NSStringEncoding


Let's say you have to convert encoding from string representation (NSString) to NSStringEncoding. For instance, it happens when you want to get string from response using NSURLConnection. Here is simple solution:

- (NSStringEncoding)encodingStringToNSStringEncoding:(NSString *)encString
{
    CFStringRef cfEncoding = (__bridge CFStringRef)encString;
    return CFStringConvertEncodingToNSStringEncoding(
                CFStringConvertIANACharSetNameToEncoding(cfEncoding));
}

Friday, February 1, 2013

Safari and retina images

Every iOS app developer, and last time Mac OS X app developer, knows that it's not enough to prepare image in one resolution. Now we need 4 times greater (2 times for every dimension) images that can be displayed on Retina displays. But what about websites? These devices also display websites and most of them just doesn't look perfect. And that's right, they also should have 2 version of every image. How to set them? The solution is quite simple. We have to set alternate image using -webkit-image-set function. The following code shows an example that says everything:
.aClass {
    background: url(image.png);
    background: -webkit-image-set(url(image.png) 1x, url
(image_2x.png) 2x);
}
It's really important to define background the traditional way, because not every webbrowser recognize -webkit-image-set function.

To work with retina images more productively, it's good to use a tool to resize images automatically. I recommend Sizes app, because it alows you to use different suffix (like _2x) instead of @2x and makes image resizing really simple.

More details you can find in Safari Development Library.

Thursday, January 31, 2013

Playing system sounds

Many apps in Mac OS X use system sounds to notify user about some actions, e.g. operation is finished successfully or failure. Sound can be easily played using NSSound class:
[[NSSound soundNamed:@"Hero"] play];
The above code plays Hero sound. The problem is where to find the list of all these system sounds. They can be found in the following directory.
/System/Library/Sounds
Here the list of sounds found in that directory:
Basso
Blow
Bottle
Frog
Funk
Glass
Hero
Morse
Ping
Pop
Purr
Sosumi
Submarine
Tink 

Introduction to User Notifications in Mac OS X Mountain Lion


In Mac OS X 10.8 (Mountain Lion) Apple introduced User Notifications. They are displayed in the notification center. They can also be displayed as small popup message.


Displaying such a notification is quite easy. Cocoa Framework provides NSUserNotification class. The following code shows a usage example:

NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.title = @"Hello, World!";
notification.informativeText = [NSString stringWithFormat:@"details details details"];
notification.soundName = NSUserNotificationDefaultSoundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];

That should display a notification in the Notification Center. What about the popup message? It's not displayed by default. To display it, there must
be implemented NSUserNotificationCenterDelegate protocol. In the example I've created, I implemented it in my AppDelegate class. The method we need is called userNotificationCenter:shouldPresentNotification:. Here is an example of its implementation:

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
     shouldPresentNotification:(NSUserNotification *)notification
{
    return YES;
}

Of course, we need to set a class as a delegate of the NSUserNotificationCenter. A good method for that is applicationDidFinishLaunching: in AppDelegate class:


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}

And that's it! The message popup will be displayed together with the notification. I really recommend to check the documentation of classes that appered in this article. There are many other features like setting additional buttons or methods that are called when the notification is displayed or deliverd.

Hello, World!

Hi! I'm a software engineer, so I create software. A lot. Mostly for Macs and iPhones, so I'm using Cocoa and Cocoa Touch libraries every day. I also like to share my knowledge with other people. So I thought I should start a blog about Mac and iPhone software development. So I have.