Shifting text in a UITextField when using leftView - ios

Currently I am setting a leftView with an image and I am finding that the image is too close to the text input view (which in this case is the standard UITextField text input view). Is there a way to shift the frame of the text input view slightly so as to increase the distance between it and the leftView?

You should be able to modify the frame property for the UIImageView you're assigning to the UITextField's leftView.
For example you might be able to do something like this:
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;
Note that you may also need to modify the UIImageView's contentMode property to get the desired effect (e.g. you can use UIViewContentModeLeft to left-align the image or UIViewContentModeCenter to center-align it).

Related

Why Imageview always stretched show image stretched iOS?

I am using an imageView and it contains different images, but some images are stretched. How do I display the images without any stretch? I have set the contentMode to aspect fit. However, the images are not in the size of the imageView, and it is in different sizes. I want to show the images as the same size without stretching.
imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];
imageview1.contentMode = UIViewContentModeScaleAspectFit;
imageview1.clipsToBounds = YES;
Please try setting content mode before setting frame like below
imageview1.contentMode = UIViewContentModeScaleAspectFit;
imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];
imageview1.clipsToBounds = YES;
If you want to display each image without stretching and of same size, than set contentMode property of UIImageView's instance to UIViewContentModeScaleAspectFill. For instance-
imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];
imageview1.contentMode = UIViewContentModeScaleAspectFill;
imageview1.clipsToBounds = YES;
UIViewContentModeScaleAspectFill, will fill the entire area of image view, keeping the aspect ratio intact. In the process of filling entire image view, either vertical or horizontal length will be fully covered and the filling will continue till the time other dimension is fully filled. In the process, your content(either across vertical or horizontal) will be visible outside the frame. To clip this extra content we have set clipsToBounds property of image view to YES.
UIViewContentModeScaleAspectFit, will fill the image view's area till the time any one length either vertical or horizontal is fully filled keeping the aspect ratio intact. Its useful, if you are not required to show each image as same size as the other direction (if vertical is fully filled than horizontal or vice versa), is not fully covered. Since this will show blank spaces in the direction which is not fully covered.

Textfield Background Image size

I want to set textfield background image same as Default contact app.
Is there any built-in type for that?
If I take the custom image then what size should be taken? because iPad has variable length of textfield.
You change the two way one is code used to change
UITextField *textField;
textField.background = [UIImage imageNamed:#"imageName"];
textField.borderStyle = UITextBorderStyleNone;
Another one way is you to change in text property at attribute inspector
I think you have to strech image from center and place as textfield.background.
[[UIImage imageNamed:#"image"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10) resizingMode:UIImageResizingModeStretch];
You can also refers this from link.
Also here is the best example.

How to Clone A UIImageView

I have a UIImageView and I want to make a copy of it and place it somewhere on the screen. How do I do this?
I currently only know how to copy and paste the image manually and make a separate IBOutlet for each one, but this is very inefficient because I want to make a game that generates obstacles (UIImageViews) forever so I can't do it the manual way.
You want to make sure you match all of the properties up as well, like size, clipping, image aspect, opacity, etc.
CGPoint locationOfCloneImageView = CGPointMake(0, 0);//x and y coordinates of where you want your image. (More specifically, the x and y coordinated of where you want the CENTER of your image to be)
UIImageView *cloneImageView = [[UIImageView alloc] initWithImage:originalImageView.image];
cloneImageView.frame = CGRectMake(0, 0, originalImageView.frame.size.width, originalImageView.frame.size.height);//same size as old image view
cloneImageView.alpha = originalImageView.alpha;//same view opacity
cloneImageView.layer.opacity = originalImageView.layer.opacity;//same layer opacity
cloneImageView.clipsToBounds = originalImageView.clipsToBounds;//same clipping settings
cloneImageView.backgroundColor = originalImageView.backgroundColor;//same BG color
cloneImageView.tintColor = originalImageView.tintColor;//matches tint color.
cloneImageView.contentMode = originalImageView.contentMode;//matches up things like aspectFill and stuff.
cloneImageView.highlighted = originalImageView.highlighted;//matches whether it's highlighted or not
cloneImageView.opaque = originalImageView.opaque;//matches can-be-opaque BOOL
cloneImageView.userInteractionEnabled = originalImageView.userInteractionEnabled;//touches are detected or not
cloneImageView.multipleTouchEnabled = originalImageView.multipleTouchEnabled;//multi-touches are detected or not
cloneImageView.autoresizesSubviews = originalImageView.autoresizesSubviews;//matches whether or not subviews resize upon bounds change of image view.
//cloneImageView.hidden = originalImageView.hidden;//commented out because you probably never need this one haha... But if the first one is hidden, so is this clone (if uncommented)
cloneImageView.layer.zPosition = originalImageView.layer.zPosition+1;//places it above other views in the parent view and above the original image. You can also just use `insertSubview: aboveSubview:` in code below to achieve this.
[originalImageView.superview addSubview:cloneImageView];//adds this image view to the same parent view that the other image view is in.
cloneImageView.center = locationOfCloneImageView;//set at start of code.
You will need to create one new UIImageView with its new frame where you want to place it. set its image property of your existing imageView's image, and after that add it to your view.
UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
newImageView.image = oldImageView.image;
[self.view addSubView:newImageView]

Programmatically create subview that covers the top 50% of the iPhone screen

I have a subview and I have been customizing its size by using the frame property and setting its value to the CGRectMake function's parameter values.
I have slowly but surely been changing the CGRectMake parameters and re-running the app to get the subview to the correct position on the screen but I know there has to be an easier way.
Here is what I am currently doing:
UIImageView *halfView = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:halfView];
halfView.frame = CGRectMake(0, 0, 320, 270);
Is there a way that I can stop having to manually enter those 4 parameters into CGRectMake, and just set it to the top 50% of the screen?
Here is what I want the subview to look like on the iphone's screen:
halfView.frame = CGRectMake(0, 0, 320, self.view.bounds.size.height/2);
you just take the height and divide it by two
You should also probably change 320 to self.view.bounds.size.width
I suggest reading this post to really get a grasp of UIViews and working with them:
UIView frame, bounds and center

Applying transform to UITextView - prevent content resizing

When I apply a rotation transform to a UITextView and then click inside to begin editing, it appears that the content size is automatically being made wider. The new width of the content view is the width of the rotated view's bounding box. For example, given a text box of width 500 and height 400, and rotated by 30 degrees, the new content width would be:
(500 * cos(30)) + (400 * sin(30)) = 633
Or graphically:
Interestingly, if you are already editing the text view and THEN apply the transform, then it appears that no modification is made to the content size. So it appears that sometime around the start of text editing, the text view looks at its frame property and adjusts the content size based on the frame width. I imagine the solution to this is to tell it to use the bounds property instead, however I don't know where to do this, as I'm not sure exactly where the text view is deciding to modify the content size.
I have googled but can't seem to find any references to using transformed UITextViews. Does anybody have any ideas about this?
EDIT (button action from test project):
- (IBAction)rotateButtonTapped:(id)sender {
if (CGAffineTransformIsIdentity(self.textView.transform)) {
self.textView.transform = CGAffineTransformMakeRotation(30.0 * M_PI / 180.0);
}
else {
self.textView.transform = CGAffineTransformIdentity;
}
NSLog(#"contentsize: %.0f, %.0f", textView.contentSize.width, textView.contentSize.height);
}
I was also stuck with this problem.
The only solution which I found was to create an instance of UIView and add the UITextView as a subview. Then you can rotate the instance of UIView and UITextView will work just fine.
UITextView *myTextView = [[UITextView alloc] init];
[myTextView setFrame:CGRectMake(0, 0, 100, 100)];
UIView *myRotateView = [[UIView alloc] init];
[myRotateView setFrame:CGRectMake(20, 20, 100, 100)];
[myRotateView setBackgroundColor:[UIColor clearColor]];
[myRotateView addSubview:myTextView];
myRotateView.transform = CGAffineTransformMakeRotation(0.8);
[[self view] addSubview:myRotateView];
Have you tried applying the rotation by doing a layer transform rather than a transform on the view?
#import <QuartzCore/QuartzCore.h>
mytextField.layer.transform = CATransform3DMakeRotation (angle, 0, 0, 1);
This might be enough to trick whatever broken logic exists inside the core text field code.

Resources