How big in points is an imag in a UITextfield

53 Views Asked by At

I have a UITextfield like so:

UITextfield *name = [[UITextField alloc] initWithFrame:CGRectMake(30, 210, 100, 34)];
name.borderStyle = UITextBorderStyleRoundedRect;
name.backgroundColor = [UIColor whiteColor];
name.font = [UIFont systemFontOfSize:12];
name.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];

If I have the following UITextfield setup, how many points is the UIImage suppossed to be? Is it 34 by 34 or a little smaller? I know, if it's 34 points by 34 points I will have to make .pngs 34x34, 68x68 and 92x92 for x, 2x, and 3x pixel displays.

3

There are 3 best solutions below

0
On
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];

It's frame will be x=0, y=0, width = width of the image, height = height of the image

0
On

You can set any image of any resolution according to your requirement. Please also add the following line before the last line of your code for the image to be visible.

[name setLeftViewMode:UITextFieldViewModeAlways];
2
On

From the documentation:

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. If you specify a control for your view, the 
control tracks and sends actions as usual.

It means that you can change size of leftView.

On iOS 8.3 "leftViewRectForBounds:" value is based on the size of the leftView. If I use the leftView with size: 300x300 then the frame will be (0, -125, 300, 300). If I use the view with size 30x30, then the frame will be (0, 10, 30, 30).
A frame of my textField is (0.0, 0.0, 300.0, 50.0).