How big in points is an imag in a UITextfield - ios

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.

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).

[[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

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];

Related

Set a UIView in a fixed proportional position from the Right Edge of screen iOS

Say, I have a UIImageView or any object and I set it in a UIView as subView with CGRectMake like this:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(266, 0, 30, 30)];
[imgView setImage:[UIImage imageNamed:[self.imageDicKey objectAtIndex:section]]];
[headerView addSubview:imgView];
Here I set the position of my imgView like CGRectMake(266, 0, 30, 30)]; and it counts it's position form the left x position (which is 266). What If I want to set my imgView's position from right side of the screen? So that,in different width of iPhone screen it shows in a same ratio position. But which will be counted it's position from right edge.
Thanks a lot in advanced.
I would recommend not hard coding the exact positions and instead calculating the x and y. That way it will help for different sized screens so it's not an exact position and rather it's based upon the views' size.
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
CGFloat padding = 20; // amount for padding
CGSize size = CGSizeMake(30, 30); // size of imageView
CGPoint startPos = CGPointMake(headerView.frame.size.width - padding - size.width, 0); // starting position for imageView
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(startPos.x, startPos.y, size.width, size.height)]; [imgView setImage:[UIImage imageNamed:[self.imageDicKey objectAtIndex:section]]]; [headerView addSubview:imgView];
Well, since the ImageView's width is 30 and the x coordinate is 266 from the left side, it makes the x coordinate from the right side 296.

Rotate a UIlabel to make it vertical

I have a UILabel. I need to rotate it programmatically.
I have my horizontal UILabel, for example with frame: x:0, y:0, w: 200, h:80.
Now I would like to rotate the label to make it vertical:
I try this code:
[self setTransform:CGAffineTransformMakeRotation(M_PI_2 / 2)];
I can see the contained text rotated. But I would like to rotate the whole frame: With my code, the UILabel continues to have the same frame.
Try this working code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 200, 80)];
//set background color to see if the frame is rotated
[label setBackgroundColor:[UIColor redColor]];
[label setText:#"Text Here"];
label.transform=CGAffineTransformMakeRotation( ( 90 * M_PI ) / 180 );
[self.view addSubview:label];
Hope it helps
If you prefer set the label position and size visually in you xib or storyboard do the following:
Set the labels position and size in the interface builder like you want them to stay after the rotation.
Rotate the label and set the frame again:
-(void)rotateLabel:(UILabel*) label
{
CGRect orig = label.frame;
label.transform=CGAffineTransformMakeRotation(M_PI * 3/2);//270º
label.frame = orig;
}
Your Label is a square(w:100,h:100).So your label has transformed, but you can't see the change,because the width is equal to the height.

Centered Single Character in UILabel off by 1 pixel

I am setting up the following UILabel:
UIImageView *textImageBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"top100bar"]];
textImageBackground.frame = CGRectMake( 104.0f, 6.0f, 215.0f, 104.0f);
[self.contentView addSubview:textImageBackground];
_lblRank = [[UILabel alloc] init];
self.lblRank.textColor = [UIColor whiteColor];
self.lblRank.textAlignment = NSTextAlignmentCenter;
self.lblRank.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:10];
self.lblRank.frame = CGRectMake(153.0f, 45.0f, 41.0f, 41.0f);
self.lblRank.backgroundColor = [UIColor colorWithRed:.09 green:.62 blue:.11 alpha:1.0];
[textImageBackground addSubview:_lblRank];
If the text of _lblRank is set to a string 2 or more characters long, it centers the text perfectly, with an equal amount of pixels to the left and right of the text. However, if the string only has a single character, the text favors the left by 1 or 2 pixels. I measured this by grabbing a screenshot of the simulator and zooming and measuring in Preview.
I have attached screenshots of single and multi character versions.
Thanks so much!
Stephen
First size to fit the label:
[self.lblRank sizeToFit]
Then center it horizontally:
CGRect frame = self.lblRank.frame
frame.origin.x = self.lblRank.superview.frame.size.width/2.0 - frame.size.width/2.0;
self.lblRank.frame = frame
Make sure the label has a superview before you use the code above.

Add label at certain point on top of an UImage

I have a ViewController that has a UIImageView as a background. What is the proper way to add a label on top of the image if I have the coordinates? My program is adding additional images on top of the UIImageView with:
UIGraphicsBeginImageContext(self.myBackground.image.size);
[newImage drawAtPoint: point];
// Draw a textlabel below the new image
// Some additional code
I have managed to draw the new Image on top of the original, but I don't know how to add the label.
Hank
[#"YOUR TEXT" drawAtPoint:point withFont:[UIFont boldSystemFontOfSize:15.0]];
You can use addSubview: method for UIImageView to add label or any other subview.
Set up label frame to be relative to uiimageview and add it as a subview:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
// To use point you can specify label size and after that you can set center:
label.center = point;
//set up other label properties
[self.myBackground addSubview:label];
//I assume self.myBackground is type of UIImageView

uiimageview within a uiview also within a button shows uiimage blurred

So I have a button with a custom uiview that contain a uiimageview.
UIButton *myButton = [[UIButton alloc] init];
myButton.frame = CGRectMake(0, 0, 29, 29);
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 29, 29)];
UIImage *myImage = [UIImage imageNamed:#"sexy-pic-of-me"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
myImageView.frame = CGRectMake(0,0,29,29);
[myView addSubview:myImageView];
[myButton addSubview:myView];
[self.view addSubview: myButton];
My image is blurry no matter how much in increase or decrease the size. What am i doing wrong here?
Try setting a content mode i.e.:
myImageView.contentMode = UIViewContentModeScaleAspectFit;
Read more about content modes:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/c/tdef/UIViewContentMode
UIViewContentMode
Options to specify how a view adjusts its content when its size changes.
typedef enum {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
} UIViewContentMode;
The other folks have mentioned two of the possible causes of fuzziness
the image size doesn't match the frame size, resulting in scaling
the image aspect ratio is being adjusted due to the content mode
The third possible cause is the use of odd numbers in the frame. From my experience, if the center.x and/or center.y of a UIView is not a whole number, then fuzziness results. For example, if you set the frame as
someView.frame = CGRect( 100, 100, 29, 29 );
then things get fuzzy because the center point of said view will be CGPoint( 114.5, 114.5 ). This problem is mainly seen on the non-retina devices. Retina devices have two pixels per point, so half-a-point coordinates are ok, but other off-grid coordinates could still cause fuzziness.

Resources