Macros for Xcode

These are some of the macros I use with Xcode:

CMLog: I use this macro to replace NSLog:

#define CMLog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);

When you use this macro, it outputs text to the console, including the class and method from where it was called. So, if you call this macro from the class MyAppDelegate and the method applicationDidFinishLaunching,

CMLog(@"My iPhone is an %@, v %@", [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion]);

you get this in the console:
Continue reading Macros for Xcode

HowTo fit text inside a UIWebView

If, like me, you want to make sure your text will fit inside your UIWebView (without scrolling), you can implement the following javascript:

    <script language='javascript' type="text/javascript">
    function adjustHeight(maxHeight) {
      elem = document.getElementById("sign");
      height = elem.offsetHeight;
      current_size = elem.style.fontSize.replace('px','')/1;
      while ((current_size-- > 10) && (height > maxHeight)){
        elem.style.fontSize = current_size + 'px';
        height = elem.offsetHeight;
      }
    }
    </script>

Continue reading HowTo fit text inside a UIWebView

Bring any window to the front

A question was asked on the apple dev mailing list on how to bring a window with a certain name to the front.

Since I was already working on how to get the list of windows on the screen, I just added some AppleScript to bring the window to the front.

This snippet doesnt use any undocumented techniques, it uses the CGWindow API introduced in Leopard.

I dont consider this code to be production ready, but it works.

Of course, TIMTOWTDI 😉

See the code after the jump:
Continue reading Bring any window to the front