Sunday, June 2, 2013

UITextField leftViewRectForBounds example in Objective C (iOS).


UITextField leftViewRectForBounds

Returns the drawing rectangle of the receiver’s left overlay view.

- (CGRect)leftViewRectForBounds:(CGRect)bounds

Parameters
bounds
The bounding rectangle of the receiver.

Return Value
The rectangle in which to draw the left overlay view.

Discussion of [UITextField leftViewRectForBounds]
You should not call this method directly. If you want to place the left overlay view in a different location, you can override this method and return the new rectangle.

UITextField leftViewRectForBounds example.
@interface MyTextField : UITextField
{
   //Declare your variable  here   
}
@end

@implementation MyTextFiled

// override leftViewRectForBounds method:
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
    CGRect leftBounds = CGRectMake(bounds.origin.x + 30, 0, 45, 44);
    return leftBounds;
}

Example of [UITextField leftViewRectForBounds].

- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
    return CGRectMake(10,
                      0,
                      [self.label.text sizeWithFont:self.label.font constrainedToSize:CGSizeMake(MAX_LABEL_WIDTH, bounds.size.height)].width,
                      bounds.size.height);
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds
{
    if (!self.showButton) { return CGRectZero; }

    return CGRectMake(bounds.size.width-BUTTON_WIDTH,
                      0,
                      BUTTON_WIDTH,
                      bounds.size.height);
}

End of UITextField leftViewRectForBounds example article.