UIButton different alignment for title and image - ios

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

Related

How do I place a button at the end of the text in a UITextView?

I can't really think of a way on how to put a custom UIButton at the end of the text of the UITextView. Not its trailing ending, not its frame, but its text.
If only I could use UILabel, then I could just implement this functionality using TTTAttributedLabel.
My goal is just like that, not just a simple hide/show READ MORE functionality or whatsoever.
Here's the goal:
And here's a simple code, I'm using frame for now for some experiments instead of constraints - and besides, I can't see the use of constraints if I don't even know where to constraint the button.
UIImage *toggleDescriptionFieldButtonImage = self.isFieldDescriptionShown ?
[UIImage imageNamed:#"ic_field_desc_shown"] : [UIImage imageNamed:#"ic_field_desc_hidden"];
toggleDesciptionFieldButton = [UIButton buttonWithType:UIButtonTypeCustom];
toggleDesciptionFieldButton.frame = CGRectMake(0, 0, 25, 25);
[toggleDesciptionFieldButton setImage:toggleDescriptionFieldButtonImage forState:UIControlStateNormal];
[toggleDesciptionFieldButton addTarget:self
action:#selector(toggleDescriptionFieldPressed:)
forControlEvents:UIControlEventTouchUpInside];

Custom keyboard buttons iOS

I am trying to setup a highlighted state for keyboard buttons, but have some difficulties with it:
I can't set backgroundImage to be beneath the spaceButton Image, so it won't overlap shift_1 like on the image below (button is tapped).
I can't set up layer margins, so that there would be a space from the bottom and other buttons, and at the same time that spacing would function as that button.
Below is my code:
[self.spaceButton setImage:[UIImage imageNamed:#"shift_1"] forState:UIControlStateNormal];
[self.spaceButton setBackgroundImage:[KeyboardViewController imageFromColor:[UIColor colorWithWhite:0.4 alpha:0.5]]
forState:UIControlStateHighlighted];
self.spaceButton.layer.cornerRadius = 7.0;
self.spaceButton.layer.masksToBounds = YES;
I would be very thankful if anyone could help me.
You would have two ways:
By Image : Just add transparent pixel at bottom,left,right in image and create new image then set that image as in background image of button.
By imageEdgeInsets : Set button imageEdgeInsets and use to image property for set image(Highlighted/Normal state).

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

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 .

UIButton titleLabel not vertically centered when using minimumScaleFactor

I have a UIButton and the text is centered within the button normally until the text string gets longer and it starts to scale down the size of the text. Then the text seems to stick to the bottom of the textField instead of staying centered in the button.
self.titleBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
self.titleBtn.titleLabel.minimumScaleFactor = .2;
[self.descriptionBtn setTitle:#"T" forState:UIControlStateNormal];
//works normally
but
[self.descriptionBtn setTitle:#"This a longer title" forState:UIControlStateNormal];
//is vertically lower than center
As noted here :)
You need to user baselineAdjustment propery of the UILabel
label.baselineAdjustment = UIBaselineAdjustmentAlignCenters
Swift 3
label.baselineAdjustment = .alignCenters
For Button
button.titleLabel?.baselineAdjustment = .alignCenters

Center custom TitleView containing an UIImage and UILabel

I checked all the other answers, but I cannot get it to work using what they say:
I have a UIView myTitle, with Frame (0,0,320,44) which contains an UIImage and a UILabel. The UIImage and the UILabel are fine and look fine, but I cannot seem to figure out how to center myTitle.
self.navigationItem.titleView = myTitle;
It shows up and looks ok, but much too far to the left. It seems to (logically) be related to the left bar button (the back button) which is auto generated by the framework when I push a controller. That barbutton has a varying width based on that and it changes the x of myTitle view based on that.
So, where can I get the width of the left (and right) barbuttons so I can figure out how to put myTitle that in the center?
I notice when I change the width of myTitle it (logically) will center (because the UIImage and UILabel are centered within myTitle).
To have more control, instead of having the left bar button auto -generated, set it using code as below.
UIButton *leftButton =[UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setFrame:CGRectMake(0, 0, 100, 44)];
[leftButton setTitle:#"back" forState:UIControlStateNormal];
[leftButton setBackgroundColor:[UIColor greenColor]];
[leftButton addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:leftButton];
Now you can perfectly adjust the frame of titleView with out much problem.
I found two ways in which it works;
Like Cy-4AH suggested, using autolayout; her/his answer would be the answer
Not using autolayout it was much simpler than I thought; if i make the titleView Width to fit it's content exactly, it automatically works; is that done with auto layout internally?
Hope this helps someone.

Resources