Sunday, June 2, 2013

UITextField leftView example in Objective C (iOS).


UITextField leftView

The overlay view displayed on the left side of the text field.

@property(nonatomic, retain) UIView *leftView

Discussion of [UITextField leftView]
You can use the left overlay view to indicate the intended behavior of the text field. For example, you might display a magnifying glass in this location to indicate that the text field is a search field.

The left overlay view is placed in the rectangle returned by the leftViewRectForBounds: method of the receiver. The image associated with this property should fit the given rectangle. If it does not fit, it is scaled to fit.[UITextField leftView]

If your overlay view does not overlap any other sibling views, it receives touch events like any other view. If you specify a control for your view, the control tracks and sends actions as usual. If an overlay view overlaps the clear button, however, the clear button always takes precedence in receiving events.

UITextField leftView example.
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 60, 25) ];
[lbl setText:@"Partenza:"];
[lbl setFont:[UIFont fontWithName:@"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentRight];
_txtFrom.leftView = lbl;
_txtFrom.leftViewMode = UITextFieldViewModeAlways;
_txtFrom.delegate = self;
[lbl release];  

Example of [UITextField leftView].
UIImageView *someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage"]];

// add 5 points to each side of the frame while keeping the same center point
someImageView.frame = CGRectInset(someImageView.frame, -5, 0);

textField.leftViewMode = UITextFieldViewModeAlways;
textField.leftView = someImageView;

UITextField leftView example.
CGFloat leftInset = 5.0f;
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, leftInset, self.bounds.size.height)];
self.leftView = leftView;
self.leftViewMode = UITextFieldViewModeAlways;
[leftView release];

End of UITextField leftView example article.