— Category: Tutorials —

Dock bug in Mountain Lion

Just a second ago I logged in as guest in my mom’s 13″ 2009 MacBook (model MacBook6,1) and I noticed that Dock is missing its background.

Missing Dock background in OSX Mountain Lion [click for full size]

Read more »

How to create a UITableViewCell with attributed text and make it highlightable

When I was creating my new app, iRachunek using iOS6 developer betas I wanted to make use of the new NSAttributedString class and create some cells using UILabel’s new attributedText property. As soon as I ran my project in Simulator I found out that when highlighting cells the attributed text doesn’t turn white. I asked on devforums.apple.com but nobody answered my question. So I decided to make it myself and ended in subclassing both UILabel and UITableViewCell. Here’s what I figured:

1. There can’t exist anything like highlightedAttributedTextColor

Since NSAttributedString is a string with many attributes, I can’t assign a ‘highlighted’ text color to it. What I have to do, is create a new NSAS property called highlightedAttributedText in my UILabel’s subclass. The next thing was making sure that I sore somewhere the original attributedText contents, so I’ve created a private NSAS iVar called attributedTextCopy. And then the method subclassing mayhem began.

The first method in the queue was -setAttributedText:, because I had to store the copy instance. The next, most important method, was, of course, -setHighlighted:. If you look into the code, you will se a strange BOOL: shouldChangeCopyInstance. What’s the point? Well, the point is, that when we change the attributedText property the attributedTextCopy mustn’t take the new value and I suppose you know why. The last method is the common -initWithFrame: to make sure that our BOOL is true on init.

So, we’ve successfully created an UILabel subclass. The next thing I’ve figured out was…

2. UITableViewCell labels must be instances of our new subclass

So, nothing difficult. Just to make sure you understand: I am using -setValue:ForKey: because both textLabel and detailTextLabel are readonly properties. I also can’t write simple _textLabel = ..., because this iVar is private*. After initialization, we set the highlightedColors to white for both labels.

* – this is not part of any private api, it’s a visibility aspect.

And we’re… done.

Now all you have to do is to import KSHighlightableAttributedCell.h and create the cells:

static NSString *cellID = @"Cell";
KSHighlightableAttributedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil) cell = [[KSHighlightableAttributedCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:@"Your email (required)"];
[textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(11, 10)];
cell.textLabel.attributedText = textLabelStr;
NSMutableAttributedString *textLabelStrH = [[NSMutableAttributedString alloc] initWithString:@"Your email (required)" attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:17]}];
[textLabelStrH setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(11, 10)];
cell.textLabel.highlightedAttributedText = textLabelStrH;
cell.detailTextLabel.text = @"n/a";
Interface Builder compatibility
You may also use Storyboards and NIBs to initialize the cell.

I hope this tutorial was useful. If so, don’t forget to follow us on Twitter and/or like us on Facebook.

The full source code is available on GitHub.

Create a Twitter-like UITabBar using UIAppearance proxy

iOS5 brings us many different methods to customize the appearance of our apps. Lots of apps use these fabulous features. Including one popular app: Twitter. I’ve seen many tutorials about using the UIAppearance proxy, but only a few of them were ‘here’s-the-result’-type. One of them is User Interface Customization in iOS 5 by Ray Wenderlich, which is brilliant example of clear tutorial with nasty result.

Today I want to teach you how to make a Twitter-like UITabBar which is very clean and good-looking interface element using only Photoshop and Xcode.

Read more »

Reset root password using single-user boot in OSX Lion

Short Version

OSX Lion

# /sbin/fsck -fy
# /sbin/mount -uw /
# launchctl load /System/Library/LaunchDaemons/com.apple.opendirectoryd.plist
# passwd root
# exit

Mac OS X Tiger – Snow Leopard

# /sbin/fsck -fy
# /sbin/mount -uw /
# launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
# dscl .
 > passwd root
 > exit
# exit

Many people have been wondering what would happen if they forgot their admin password in Mac OS X. There is a solution: you have to own a Mac OS X Install DVD which provides a Password Utility application. But what if you don’t have one? There is a solution.

Important Note: This works only if there is no Firmware Password set. Firmware Password blocks the ability to boot your computer in single-user mode.

All you have to do is turn on your computer holding CMD + S. This will boot your Mac in Single-User Mode. Next, you have to wait a second while kernel will load command-line interface. It there is :/ root# line, you’re ready to go!

Read more »

When sudo doesn’t work…

Today I wanted to install some mkv tools via macports and received strange response:

$ sudo port install mkvtoolnix
sudo: can't open /private/etc/sudoers: Permission denied
sudo: no valid sudoers sources found, quitting

After running $ ls -lad it turned out that something is wrong with boot volume permissions. Theese are corect ones:

$ ls -lad / /private /private/etc
d--xrwxr-t+  36 root  wheel  1292 25 sty 12:34 /

This problem cannot be solved by simple Repair Permissions in Disk Utility. The only way to solve it is booting your computer in Single-User Mode (hold cmd+s while booting) and entering theese commands:

# /sbin/fsck -fy
# /sbin/mount -wu /
# /bin/chmod 1775 /
# /bin/sync
# exit

Everything should work now. :)