Issue with CGRectMake for Button Size - cocoa-touch

I am having an issue with resizing my button with CGRectMake. As you can see below, it should be working, but it doesn't change at all.
GameTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
GameTwo.titleLabel.font = [UIFont systemFontOfSize:22];
GameTwo.titleLabel.lineBreakMode = UILineBreakModeHeadTruncation;
[GameTwo setTitle:(#"Game Two") forState:UIControlStateNormal];
GameTwo.frame = CGRectMake(10, 10, 100, 50);
[GameTwo addTarget:self action:#selector(gameTwo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:GameTwo];
I guess i have two questions as well. How does one change button size depending on portrait mode and then for landscape mode.

A good rule of thumb for "It should be working, but it doesn't change at all" bugs is to search for the bug elsewhere.
Did you check the GameTwo class implementation (and superclasses) thoroughly?
Did you set an autoresizing mask?
Do you somewhere set the frame on a bounds change notification?

GameTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
GameTwo.titleLabel.font = [UIFont systemFontOfSize:22];
GameTwo.titleLabel.lineBreakMode = UILineBreakModeHeadTruncation;
[GameTwo setTitle:(#"Game Two") forState:UIControlStateNormal];
GameTwo.frame = CGRectMake(10, 10, 100, 50); [GameTwo addTarget:self
action:#selector(gameTwo)
forControlEvents:UIControlEventTouchUpInside]; [self.view
addSubview:GameTwo];
Works perfect. (This is what was in the original question)
The issue was I was trying to use another function to place were the button should go. I didn't understand that the first two variables in this case (10, 10...) were the placement of the button. Now i do.
Cheers.

Related

how to fit image inside a circular button 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)

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.

iOS dynamic addition of UIControls

I'm trying to add a bunch of UIButtons to a screen at runtime. Based on user input I want to show different buttons in different locations. Is this possible?
Yes, it's possible.
Creating a button in objective-C:
UIButton *btnPrefix = [UIButton buttonWithType:UIButtonTypeCustom];
[btnPrefix setFrame:CGRectMake(0, 0, 20, 20)];
[btnPrefix setUserInteractionEnabled: YES];
[btnPrefix addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: btnPrefix];

iOS UIButton with UITextAlignmentLeft still centering text?

Problem:
After setting a button's titleEdgeInset and UITextAlignmentLeft the button's title is still centered, not left-aligned.
Details:
I have a button 200px wide using a custom background with an image on the left. Since I don't want the button title to cover the image, I inset the title using titleEdgeInset=UIEdgeInsetsMake(0.0, 45.0, 0.0, 0.0);. This should make title label's x position start at 45px and end at 200px. I also want the button's title to be left-aligned to the image, so I also set textAlignment = UITextAlignmentLeft. However, the title text is still centered between 45px and 200px, not left aligned.
Why?
Here's the relevant code:
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 45.0, 0.0, 0.0);
button.titleLabel.textAlignment = UITextAlignmentLeft;
[button setTitle:#"X"];
My test code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 200, 72);
UIImage *image = [UIImage imageNamed:#"rob.png"];
[button setImage:image forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.view addSubview:button];
Note that I set button.contentHorizontalAlignment, not button.titleLabel.textAlignment. My result:
set contentHorizontalAlignment property for button, it will work.
YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Swift 4.0
YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.center
Put a background color on the titleLabel. I'm betting it's being sized to fit, so the text alignment won't matter. It's only as long as it needs to be. What you need to fix is the position of the label itself. You might have to extend the UIButton and override layoutSubviews to get it right. I've never seen a button with anything but a centered label.
Set contentHorizontalAlignment property for a button:
yourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Swift 3
Set contentHorizontalAlignment property do the work. Thanks
yourButton.contentHorizontalAlignment = .left
Just override the button's setter and try to set the contentHorizontalAlignment property in the setter:
-(UIButton *)yourButton {
[_yourButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
return _yourButton;
}

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.