Sunday, June 2, 2013

UITextField rightViewRectForBounds example in Objective C (iOS).


UITextField rightViewRectForBounds

Returns the drawing location of the receiver’s right overlay view.

- (CGRect)rightViewRectForBounds:(CGRect)bounds

Parameters
bounds
The bounding rectangle of the receiver.

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

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

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

@implementation MyTextFiled

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

Example of [UITextField rightViewRectForBounds].

- (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 rightViewRectForBounds example article.