how to fit image inside a circular button objective c - objective-c

I created a button in storyboard with a circular form using layer.cornerRadius in the key path. I want to add an image to it. How can I add an image to my button and the image is fit the same size of my button

Use this to set image size to adjust button size:
[btn setImage:[UIImage imageNamed:#"image_name"] forState:UIControlStateNormal];
btn.imageView.contentMode = UIViewContentModeScaleAspectFit; //set to fit button size

It should be like this
UIImage *btnImage = [UIImage imageNamed:#"image.png"];
[buttonName setImage:btnImage forState:UIControlStateNormal];
Write the below code in viewDidAppear instead of viiewDidLoad
buttonName.layer.masksToBounds = YES;
buttonName.layer.cornerRadius = 20;//half of the button height

Try this to fit image to Button :
UIButton *button =[[UIButton alloc]initWithFrame: CGRectMake(10, 10, 50, 32)];
[button setImage:[UIImage imageNamed:#"image_name"] forState:UIControlStateNormal];
// to set image to fit button size
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
//ScaleToFill (UIViewContentModeScaleToFill)
//ScaleAspectFit (UIViewContentModeScaleAspectFit)
//ScaleAspectFill (UIViewContentModeScaleAspectFill)

Related

Changing the size of a Round Button in Cocoa

Is there a way by which I can increase the size of a Round Button in Cocoa? I am doing a Cocoa Application in which one of the view contains some of the user's avatars. I would like to use a Round Button and set the images to it. But I can't find any way to increase the size of the Round Button.
Is there some way of doing it?
yourButton.frame = CGRectMake(0, 0, 20, 20);
//or
yourButton.frame = CGRectMake(0, 0, 40, 60);
//or
yourButton.frame = CGRectMake(0, 0, 100, 80);
To resize the Rounded Rect NSButton you need to customize and you need to draw your own button.
To solve your problem use Gradient button resize what you want and setImage:an image should be rounded rect. then make borderless button
[button setBordered: NO];
now it will appear like rounded rect button.
To remove gray highlight use
[[button cell] setHighlightsBy:0];
Try this...
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 150, 60)];
myButton.enabled = YES;
myButton.backgroundColor = [UIColor lightGrayColor];
[myButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[myButton setTitle:#"My Button" forState:UIControlStateNormal];
[self.view addSubview:myButton];
Change the 150 and 60 values to alter the shape of your button.
In case you are using images, they try with UIButton's method setBackgroundImage:forState:.
With this method, whenever you change the frame, it will adjust the image according to that.

How to add image as button in objective c?

I want to add image as button, and when it is clicked it should open a new view controller...
Does anyone know how to do?
I created a button and added image, but its not nice, because when I click on the image it animates a square arround the image, which I do not want. This what I did.
- (void)setTutorialButtonImage
{
[self.tutorialButton setImage:[UIImage imageNamed:#"app_logo.png"] forState:UIControlStateNormal];
}
Does any one know how to do... just an image and when I click on it start a new controller
You can disable the highlighting effect by using
self.tutorialButton.adjustsImageWhenHighlighted = NO;
Also try setting the same image for selected and highlighted states too
[button setBackgroundImage:[UIImage imageNamed:#"app_logo.png"] forState: UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"app_logo.png"] forState: UIControlStateSelected];
For better look and feel you can apply insets on image using
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(x, y, width, height)];
[sampleButton setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];

Title in UIButton with Image

I have set an image in a UIButton but I need to set the title above this image. How can I do this?
[button setImage:[UIImage imageNamed:#"btn_select_s.png"] forState:0];
[button setImage:[UIImage imageNamed:#"btn_select_p.png"] forState:1];
[button setTitle:#"my title" forState:0];
You can use UIButton's titleEdgeInsets and imageEdgeInsets to position title and image as you want.
See Apple's UIButton reference documentation
For example, in a UIButton subclass:
[self setImageEdgeInsets:UIEdgeInsetsMake(imageFrame.origin.y, imageFrame.origin.x, 0, 0)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(labelFrame.origin.y, labelFrame.origin.x, 0, 0)];
You need to set the background image of the button instead:
[button setBackgroundImage:[UIImage imageNamed:#"foo"] forState:UIControlStateNormal];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
//Use you values for edge insets here.
[button setTitleEdgeInsets:UIEdgeInsetsMake(20.0f, 10.0f, 0.0f, 0.0f)];
There is a convenient UIButton subclass here:
https://github.com/lionhylra/FlexButton
It has features like:
- It provides 4 layout styles.
- It works as simple as UIButton.
- When you adjust the size of button, the size of imageView and titleLabel will change accordingly and correctly. They will not be put into disorder. This is the essential difference to using edge inset.
- TitleLabel and imageView will always be centered vertically and horizantally.

Distorted custom Navigation buttons on non-retina display (using stretchable image method)

I am adding custom navigation buttons to my navigation bars via the following code.
//Instance method in CustomNavButton Class
-(UIButton*)setupButtonWithImage:(UIImage*)image andFrame:(CGRect)frame
{
UIButton *button = [[[UIButton alloc]initWithFrame:frame]autorelease];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-20)/2, (frame.size.height-20)/2, 20, 20)];
imageView.image = image;
UIImage *buttonImageNormal = [UIImage imageNamed:#"customBtn_black"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[button setBackgroundImage:stretchableButtonImageNormal
forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addSubview:imageView];
return button;
}
//Call CustomNavButton and add to Navbar
- (void)viewDidLoad
{
[super viewDidLoad];
//Add left invite friends button
CustomNavButton *leftButton = [[CustomNavButton alloc]initWithImage:[UIImage imageNamed:#"friends_1"] andFrame:CGRectMake(0, 0, 40, 32)];
[leftButton.customNavButton addTarget:self action:#selector(inviteButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = leftBarButton;
[leftButton release];
}
The navigation buttons appear fine on my iPhone (IOS5 with Retina Display)
However, the buttons look distorted on my simulator (or non-retina display)
How can I resolve this? How can I display the buttons properly even for non-retina displays?
Note that I have created the #2x buttons for this as well
EDIT:
It seems like the issue likes with the stretching of the image
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
If I change the leftCapWidth value to 0, the buttons on the simulator looks better (but still bad).
But by doing this, it will cause my retina display button look a little distorted (seems like I can't win).
Can anyone advise if the problem does really lie here, and how I can alter the values so that it works well for both retina and non-retina displays?
Do you have two files? MyImage.png and MyImage#2x.png? It looks like the system is trying to resize the #2x file by scaling it down, which usually causes some jaggedness like this when it is simply scaled down.
MyImage.png should be half the size of MyImage#2x.png.
Your not setting the file format .png or whatever it is you have. And as Chris said you need two files regular and #2x.

objective c finding pixel width of a string

I have a UIButton of fixed width and I want to place text in it. Since the size of the String is unknown in advance, I will have to resize the font as I read the String from a flat file. How can I do that? A function similar to the following will be great:
UIFont resizeFontAs:(UIFont)initialFont andStringAs:(NSString*)string andWidthAs:(int)width
Thanks in advance
Edit : This code doesnt seem to work:
// Method for creating button, with background image and other properties
- (UIButton *) getCallAgentButtonWithTitleAs:(NSString *)aTitle andImageAs:(UIImage*)bgImg atIndex:(int)index{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(10, 2+index*50, 300, 48);
aButton.titleLabel.frame = CGRectMake(aButton.titleLabel.frame.origin.x + 25.0, aButton.titleLabel.frame.origin.y, aButton.titleLabel.frame.size.width - 50.0, aButton.titleLabel.frame.size.height);
aButton.titleLabel.adjustsFontSizeToFitWidth = YES;
[aButton setTitle:aTitle forState:UIControlStateNormal];
//aButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[aButton setTitleColor:UIColorFromRGB(0xFDD428) forState:UIControlStateNormal];
[aButton setBackgroundImage:bgImg forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(callAgent:) forControlEvents:UIControlEventTouchUpInside];
// set the tag as the index and use it later to obtain the phoneNo
[aButton setTag:index];
return aButton;
}
You can tell the button's label to resize the font itself.
myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
myButton.titleLabel.minimumFontSize = 6.0; // Pick your own value here.
Use the following method:
- (CGSize)sizeWithFont:(UIFont *)font
minFontSize:(CGFloat)minFontSize
actualFontSize:(CGFloat *)actualFontSize
forWidth:(CGFloat)width
lineBreakMode:(UILineBreakMode)lineBreakMode
That'll allow you to specify both a default size (the first arg) and a minimum size, and the font will be automatically scaled if necessary.