Thursday, February 25, 2010

How to Disable UIWebView Selection

You could do this by running a short javaScript line at webViewDidFinishLoad, but I find this more elegant: define UIWebView properties in CSS.
NSString *myHtml = [NSString stringWithFormat:
   "<style type=\"text/css\">"
   // Disable Open/Copy/Cancel alert with long link selection
   "* {"
   "-webkit-touch-callout: none;"
   // Disable selection/Copy of UIWebView
   "-webkit-user-select: none;"
   "</head><body>%@</body></html>", message.body];
[myWebView loadHTMLString:myHtml baseURL:nil];
As a bonus I left there also how to disable link selection popup.

Monday, February 22, 2010

How to Clear UITabBar badgeValue

It's delightfully easy to add a badgeValue into a UITabBarItem, but I couldn't find how to remove it. Turned out it's as easy as adding one:

if (count)
    self.parentViewController.tabBarItem.badgeValue = @"!";
    self.parentViewController.tabBarItem.badgeValue = nil;
Thanx Apple for these delightful surprises!

Tuesday, February 16, 2010

Why viewWillAppear Wasn't Called?

Have several views defined as XIB, work just fine as standalone views and as main views under UITabBar. However they stopped working when added them as subviews under main view in UITabBar (don't ask...)

The problem was that viewWillAppear wasn't called automatically, because I added the views manually under another view. Apple documentation suggests calling the missing function manually - and yet again everything works just fine!

self.myController =
[[MyViewController alloc]
initWithNibName:@"MyViewController" bundle:nil];
[self.mainView addSubview:myController.view];
[self.myController viewWillAppear:YES];

Wednesday, February 10, 2010

How to Make UIWebView Open target="_blank" Links

There are several ways to do this, here's mine.
NSString *js = @"\
var a = document.getElementsByTagName('a');\
for (var i=0; i<a.length; i++) {\
[self.myWebView stringByEvaluatingJavaScriptFromString:js];
Possible advantage: reduces size of HTML document.

How to Do Totally Custom Titlebar

iPhone SDK does not support a "titlebar" with custom background image, fully visible title text and titlebar buttons.

You can create semi-transparent UINavigationBar or UIToolbar (style Black Translucent) to see custom background (UIView just below), but your image is still dimmed. You can tune Alpha value between 1 and 0, but at the same time your title text and buttons visibility change, too.

Another solution is to create a custom setup, which just looks like a titlebar:

Inherit your own MyToolbar class from UIToolbar. Place on it 3 UIBarButtonItems: one for left side button, one Flexible Space UIButton in middle and one for right side button. The middle flexible sized button should push your other button to correct locations. If not, change it to fixed size and reside as you wish. Your number of button can also differ, naturally. I used 5.

Here's the full source code for your class:

@interface MyToolbar : UIToolbar {

#import "MyToolbar.h"
@implementation MyToolbar

- (void)drawRect:(CGRect)rect
// Warning: Hardcoded for portrait size
UIImage *image = [[UIImage imageNamed:@"sample.png"] retain];
[image drawInRect:rect];
[image release];

Place a UILabel on top of your toolbar, resize to fit the available space between other buttons. Change font e.g. Helvetica Bold size 18, color red, , shadow black and background transparent (opacity 0%).

How to Put a Smiley Into Your UILabel

Sometimes iPhone SDK just makes you smile, things you expect to be difficult turn out to be really simple. Take for example a smiley face :)
NSRange range = {NSNotFound, 0};
NSString *s = @"This is a smiley :) face";

range.location = 0;
range.length = [s length];
s = [s stringByReplacingOccurrencesOfString:@":)"

iPhone contains by default a Japanese font, which supports unicode emoji characters. As a result you suddenly have built-in supports for about 460 graphical icons, all of which you can put anywhere as part of any text string!

Here's full list of emoji characters in iPhone. Have fun ;)

Thursday, February 4, 2010

How to Use RGB Values to Show a Non-white Color

This one was really annoying! In every other system I've used, and there's a few, RGB color values (Red - Green - Blue) were from 0 to 255.

iPhone RGB values are from 0 to 1. Using something like (0, 0, 255) gives no warnings what-so-ever. Just silently accepts whatever values you give it and happily tries its best to show you - WHITE !!!

As a sample, here's beige color:

myView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.86 alpha:1];

Please note that alpha value 0 (zero) means transparent. If your alpha is 1 (one), you will see color of object below your component, regardless whether you used correct RGB values or not.

That wasn't happy debugging at all.

Tuesday, February 2, 2010

How to Use Struct in NSDictionary

Thought I wanted to use a NSDictionary for some reason, but after all the trouble didn't. However it was quite a challenge to make it work, so I'll post here a code sample. Maybe it will be useful at some other time:

struct myStruct {
NSUInteger value;
NSString *s;
} myItem[2] = {
{ 1, @"test"},
{ 2, @"test2"}
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithPointer:&myItem[0]], @"myKey1",
[NSValue valueWithPointer:&myItem[1]], @"myKey2",

Monday, February 1, 2010

How to Disable UITableCell Selection

Sometimes you just want to show a list (UITableView) without selecting any row. By default a table (cell) is selectable, so how to disable it. What worked for me was defining each cell separately as non-selectable:

cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;

You could also try controlling the whole table, but it didn't work for me:

- (void)viewDidLoad
// Prevent message cell selection
self.chatTableView.allowsSelection = NO;
// HOX: not iPhone 2.2.1 compatible --> crash
[super viewDidLoad];

This is also supposed to work, but I recall I still got a selection flash:

- (NSIndexPath *) tableView:(UITableView *)_tableView willSelectRowAtIndexPath:(NSIndexPath *)_indexPath
// Return nil if you don't want the row selected.
// Hox: interferes with didSelectRowAtIndexPath
return nil;