Wednesday, 14 September 2011

iPhone SDK Troubles: ios SDK blurry fonts

A very annoying thing is when you programmatically create contents for a UITableViewCell and text in UIlabels appears blurry. Why? The answer is simpler than you can think: you positioned your UILabels in the wrong place. Probably, you positioned your UILabel:
//At allocation time
UILabel* l=[[UILabel alloc] initWithRect:CGRectMake(x,y,w,h)];

//or after
l.frame=CGRectMake(x,y,w,h);
Now, be careful: what's the meaning of (x,y,w,h)? They're floating point values. When placed in a UIView, they must have no fractional part. If you specify it with (e.g.)
(1.0f, 3.0f, 200.0f, 400.0f)
your label will not appears blurry. If you calculate UILabel's frame, you must call floor function to force to zero fractional part.
float a=1.0f;
float b=3.0f;
float c=200.0f;
float d=400.0f;

l.frame=CGRectMake(
        floor(x/3.0f),
        floor(y/3.0f),
        floor(w/3.0f),
        floor(h/3.0f)
    );
Texts appear blurry when coordinates aren't approximate to nearest integer. Floor force this approximation. Now your text will appear sharp as a japanese sword :)

Tuesday, 9 August 2011

iPhone SDK Troubles: Rotating scrollview image gallery



Everybody love iOS interface and its "magic" appeal: fluid rotations, fluid swipe, ecc. A iOS programmer knows that there's a lot of work under that magic. If you try to realize a simple image gallery, you'll have some surprises when you'll try to rotate it:

  1. your image will not be located on view's center, but on a corner.
  2. if you forced your gallery to scroll to a certain page with scrollToRect, then you'll see all your precedent images in a fast scroll, even if you set parameter Animated to NO
  3. if you need to scroll after rotation, you'll see a rotation around a corner
Solutions I found are:
  1. Point 1: when detecting a rotation, (willRotateToInterfaceOrientation), you have to resize:
    • Images on the scrollview
    • scrollview content

  2. Points 2 & 3: you must cover gallery's scrollview with a UIImageView containing current gallery image, hiding all scrollview animations. The "cover" will rotate around screen center, giving a more professional (and Apple) look.



Thursday, 4 August 2011

Hey Emacs! (an excursion on Emacs' tab-key)

One on main difficulties on Emacs is how it manages tab-key. A user whom comes from VI/VIM or another editor feels disoriented because Emacs ignores a tab and mantain stubbornly the selected line in the same place.
A bug? No. It's a feature. Hard to understand, but a feature. This is because there's three ways to manage a key-tab event:

  1. a way to insert a TAB (\t) character
  2. a way to insert 4 (or more) spaces
  3. a command to indent selected line(s)

VIM and other editors mean tab-key as 1 or 2. Emacs, instead, binds tab-key whith the "indent according to mode" command. This means a more sophisticated (and elegant) tool, but also means to understand it.

Modes

Emacs works with Modes. A C file will be interpreted with the c-mode; a Java file with java-mode, ecc. Every mode has its own configuration. E.g., a C file could be indented according to GNU style, BSD style, Kerningan and Ritchie style and others (a full list is avaiable on wikipedia).
But is an advantages? Well, yes, when you understand how a mode works. If it's properly configured, it helps programmer as no other editor. You can detect if you forget a parenthesis becaus Emacs will indent wrong a line. It's usefull.

But my colleagues uses VIM

If a VIM users sends you a C file, all new lines you'll add will be indented using current Emacs mode and style. It's boring, but there's a solution. You can change Emacs configuration just for a file writing on top of it the famous "Hey Emacs" line. If you write on top

/* Hey Emacs! -*- mode: java; c-basic-offset:4; indent-tabs-mode: t;default-tab-width:4 -*- */


Emacs will understand:
  • it's reading a Java file (enable Java mode)
  • indent lenght is 4 spaces (c-basic-offset)
  • tabs are 4 spaces long
  • when idents, Emacs must use a tab character (indent-tabs-mode:t)

This is just an example: there's many other variables you can set. I think adding this line it's a small price to pay. Emacs is really good.
And, if you want, there's always VIM ;)

Update
There's a way to insert brutally a tab character: pressing META+i. But I suggest you to use it carefully.

Thursday, 28 July 2011

iPhone SDK Troubles: Error from debugger: Error launching remote program: security policy error


When I started programming with Cocoa-Touch a guy who teached me Objective-C's basis said «Everybody loves Cocoa. Not much people loves XCode».
Forgetting some boring and stressful troubles (mainly about memory management), I agree with him, because last "iPhone SDK Troubles" came from XCode. It gives me this error:

Error from debugger: Error launching remote program: security policy error

It taken me a lot to resolve it, because it seemed an error related to Info.plist or a bad-configured provisioning profile.

Instead it was easy: just delete all expired provisioning profiles from your device (iPhone/iPad). Go to Configuration->General->Profiles and delete them.
I was tired, so I deleted all profiles and I rebooted the device, but after I was able to install applications from XCode to iPad.