UITextView with Placeholder

Traditionally, UITextField comes with placeholder field (a default text displayed in the textfield that disappears once the user inputs some text). The UITextField, however, limits the display to be one line. It might not be enough if you have want to display multiple lines of text.

In our case, it was the description field of a Event.

Most of the information can be found on this stackoverflow post

http://stackoverflow.com/questions/1328638/placeholder-in-uitextview

I tried out the second option provided, SSTextView, https://github.com/samsoffes/sstoolkit/blob/master/SSToolkit/SSTextView.h

by soffes.

It worked out mostly well with one small problem. The default text is lower than the cursor. (The cursor is not flashing at the beginning of the placeholder text).

The reason for this misalignment is here

if (_shouldDrawPlaceholder) {
[_placeholderTextColor set];
[_placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width – 16.0f, self.frame.size.height – 16.0f) withFont:self.font];
}
The rectangle is drawn in the right location, but the text displayed is relative to the rectangle and not the UITextView object. As a result, the cursor, which is at the starting position of UITextView, not the starting point of the text in the rectangle.
In order to align the text of the rectangle to the cursor in SSTextView, I played with the first two parameters in
 CGRectMake(8.0f, 8.0f, self.frame.size.width – 16.0f, self.frame.size.height – 16.0f)
which corresponds to the lower left point of the rectangle. I set it to 4.0 and 8.0 and it worked for my case. But this would be my solution to the misalignment using SSTextView.
Advertisements
This entry was posted in IPhone App Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s