How to add image UIImage with indent in UITextField in iOS 7? - ios

I have a UITextField in which I want to add an image on the left side. I do this using UIImageView which is set in leftView property of UITextField. The image is resized in Image View to proper size. The problem is that it is stick to the border. How can I set left indent of the image. I suppose that one solution is to set some blank space in the left side of the image, but I want to use indent. Also I want some indent after the image, so the text is not so close to it.
I use following code:
[textField setLeftViewMode:UITextFieldViewModeAlways];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37, 20)];
imageView1.image = [UIImage imageNamed:#"custom_image"];
textField.leftView = imageView1;

You can set the UIImageview frame more than the original UIImage and set the UIImageview contentMode property to centre.
Please refer below answer.
UIImageView *imgSearch=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 28)]; // Set frame as per space required around icon
[imgSearch setImage:[UIImage imageNamed:#"search.png"]];
[imgSearch setContentMode:UIViewContentModeCenter];// Set content mode centre
self.tfSearch.leftView=imgSearch;
self.tfSearch.leftViewMode=UITextFieldViewModeAlways;

You need subclass UITextField and implement in it
- (CGRect)leftViewRectForBounds:(CGRect)bounds

Maybe you should have a subclass UITextField and overrides
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (void)drawTextInRect:(CGRect)rect;
These will change the rect for the leftView .

Related

UITextFiled without border unexpected behavior on unicode characters

I need to add unicode characters like "îăâșț" in my textfield, but i notice that if textfield is without border you can see adjusting effect and if textField has border the behaviour is as expected no adjusting.
I have tried to add border with white color but there is some shadow left, Also tried self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
How can I have the same behaviour for UITextField without border as UITextField with border?
So after playing with settings i notice if you adjust the content insets by overriding:
- (CGRect)textRectForBounds:(CGRect)bounds
- (CGRect)editingRectForBounds:(CGRect)bounds
or simply by adding a left View
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
self.textFieldName.leftView = paddingView;
self.textFieldName.leftViewMode = UITextFieldViewModeAlways;
and also keep in mind that self.textField.minimumFontSize should be smaller otherwise the jumping effect will persist

alignment in textfield does not work

I want both my placeholder and typed text to be aligned in the center.
I tried both of these options for the two textfields I have, but still the placeholder and whatever I type are aligned left.
self.teamA_name.textAlignment=NSTextAlignmentCenter;
self.teamB_name.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
What am I missing?
Textfields are created through IB.
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,15, 20)];
_txtCityName.leftView = paddingView;
_txtCityName.leftViewMode = UITextFieldViewModeAlways;
Use this code to set inset..This works for me
Currently I'm setting padding view width as 15...change this as your requirement and make it in the center

UIButton different alignment for title and image

Hello I am creating UIButton which is displaying image and title at the same time. I want title and image to be both centred, button to be above image. Problem is that because of localisation of string from button, string length is changing, and alignment from UIStoryboard combined with edge insets is not working as I want it to work. Can someone help me and point me in the right direction?
set these two.
1. set Align controls of UIButton
set Image and Text Inset of UIButton
The first thought is that you could build a UIView showing image and text, then attach a UITapGestureRecognizer to it, which would be more flexible than UIButton.
I am not sure, but you can try to set title like this:
[someButton setTitle:#"\nsome title" forState:UIControlStateNormal];
where \n is a new line.
Adjust titleEdgeInsets and imageEdgeInsets accordingly
[self.facebookButton setImage:[UIImage imageNamed:#"facebook_icn"] forState:UIControlStateNormal];
[self.facebookButton setTitle:NSLocalizedString(#"Invite via Facebook1",nil) forState:UIControlStateNormal];
self.facebookButton.titleEdgeInsets = UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, -5.0f);
self.facebookButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);

How to place UILabel in the center of navigation bar

How to place UILabel in the center of navigation bar in XCode 6? Is it possible at all? I can place here, for example, UIButton, but unable to place UILabel. If no, what can I do then? Place a UIButton with the appropriate text and make it non-clickable?
Thanks in advance.
In Xcode 6 if you want to put a label inside the UINavigationBar firstly you have to put a UIView there then put the UILabel inside the UIView (this is from the Storyboard btw).
If you do not put the UIView first then the UILabel will never get put onto the UINavigationBar.
Create a UIView and add UILabel as it's subview and then set your NavigationItem's titleView as previously created UIView.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
label.text = #"Hello";
label.textAlignment = NSTextAlignmentCenter;
[view addSubview:label];
self.navigationItem.titleView = view;
Note: Don't forget to set your own frame values to get better result.
Just make sure you try viewWillLayoutSubviews method instead of viewDidLoad that was what worked for me. Cheers!

How do I put images and text in the same layout in XCode

this is my first post. How can I add text and images to an app. i am only making an image, text and button based app and have made it for android as the picture shows below. I am now trying to do this in iOS but i am unable to place image after text followed by text again.
In android I was able to put multiple ImageView and TextViews inside a scroll layout, but I am unable to do this in storyboard Xcode 4.5/4.6.
If you could give me simple ideas to fix reproduce the same style of layouts in Xcode for iPhone and iPad, it would be great. I am not familiar with Xcode, so if it is possible to solve this easily, could you please help.
I want to be able to make a long scrollable page with many text line and images as you may have already understood from the image below.
Thanks in advance
Sorry was unable to post image. may try again or post website link.
You need to create a UIScrollView -> init UIScrollView with frame and add it to your app, set the content size of your scroll view.
Create the UILabel for the text. -> init UILabel with frame and text, add to your scroll view as a subview.
Create the UIImageView for the image -> init UIImageView with frame and image, add to your scroll view as a subview.
Typically, that is all if you do it in the way programmatically.
some code (without test):
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 100)];
label1.text = #"label1";
[scrollView addSubview:label1];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 500)];
imageView.image = [UIImage imageNamed:#"imagename.png"];
[scrollView addSubview:imageView];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, label1.frame.size.height+imageView.frame.size.height);
[self.view addSubview:scrollView];
ps: If you want do it with nib, I could find some capture for you.

Resources