objective c finding pixel width of a string - objective-c

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.

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)

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

Is it Possible to Change Action For UIButton?

I am trying to change Action For the UIButton in ios applicatio. I did Following Code
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethodShow:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
In particular Section I want to change Action for this Button .So i did this
[button addTarget:self action:#selector(aMethodHide:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Hide View" forState:UIControlStateNormal];
Unfortunately This code note Working?
I suggest before adding new target, first invoke removeTarget on UIbutton object then add new target with other action.
I think it will help you
[yourButton removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents];
[yourButton addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlEventTouchUpInside];
You can use same action target instead of using two target. In one target you have to differentiate like below
-(void)btnAction
{
if(target1)
{
// code for target 1
}
else
{
// code for target 2
}
}
Here target1 is BOOL value which value first set to be YES. And change its value NO whenever you want to perform target 2 code.
I hope this will helps You.
I made an app recently and i had the same situation, but i found another way to solve it so i decided to share my solution with people who may be in the same situation.
I'll try to explain what i did with the context of this question:
I added a tag to the button and i associated it with one of the functions that button needs to call (aMethodShow: for example).
button always call the same function (callSpecificFunc: for example). What callSpecificFunc: does is call either function aMethodShow: or aMethodHide according with the current button tag.
In the particular section in which the button needs to call a different function, i only change the tag of button.
Something like this:
NSInteger tagOne = 1000; //tag associated with 'aMethodShow' func
NSInteger tagTwo = 1001; //tag associated with 'aMethodHide' func
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(callSpecificFunc:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
...
// in some part of code, if we want to call 'aMethodShow' function,
// we set the button's tag like this
button.tag = tagOne
...
//Now, if we want to call 'aMethodHide', just change the button's tag
button.tag = tagTwo
...
-(void) callSpecificFunc:(UIButton*)sender
{
NSInteger tagOne = 1000;
NSInteger tagTwo = 1001;
if([sender tag] == tagOne){
//do whatever 'aMethodShow' does
}else {
//do whatever 'aMethodHide' does
}
}
Of course it could be applied for more than 2 functions :)

Issue with CGRectMake for Button Size

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.

Is it possible to change a UIButtons background color?

This one has me stumped.
Is it possible at all to change the background color of a UIButton in Cocoa for iPhone.
I've tried setting the background color but it only changes the corners. setBackgroundColor: seems to be the only method available for such things.
[random setBackgroundColor:[UIColor blueColor]];
[random.titleLabel setBackgroundColor:[UIColor blueColor]];
This can be done programmatically by making a replica:
loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
[loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
loginButton.backgroundColor = [UIColor whiteColor];
loginButton.layer.borderColor = [UIColor blackColor].CGColor;
loginButton.layer.borderWidth = 0.5f;
loginButton.layer.cornerRadius = 10.0f;
edit: of course, you'd have to #import <QuartzCore/QuartzCore.h>
edit: to all new readers, you should also consider a few options added as "another possibility". for you consideration.
As this is an old answer, I strongly recommend reading comments for troubleshooting
I have a different approach,
[btFind setTitle:NSLocalizedString(#"Find", #"") forState:UIControlStateNormal];
[btFind setBackgroundImage:[CommonUIUtility imageFromColor:[UIColor cyanColor]]
forState:UIControlStateNormal];
btFind.layer.cornerRadius = 8.0;
btFind.layer.masksToBounds = YES;
btFind.layer.borderColor = [UIColor lightGrayColor].CGColor;
btFind.layer.borderWidth = 1;
From CommonUIUtility,
+ (UIImage *) imageFromColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
// [[UIColor colorWithRed:222./255 green:227./255 blue: 229./255 alpha:1] CGColor]) ;
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Don't forget to #import <QuartzCore/QuartzCore.h>
I assume you're talking about a UIButton with UIButtonTypeRoundedRect?
You can't change the background color of that. When you try changing it's background color you're rather changing the color of the rect the button is drawn on (which is usually clear).
So there are two ways to go. Either you subclass UIButton and overwrite its -drawRect: method or you create images for the different button states (which is perfectly fine to do).
If you set the background images in Interface Builder you should notice that IB doesn't support setting images for all the states the button can have, so I recommend setting the images in code like this:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:[UIImage imageNamed:#"normal.png"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"disabled.png"] forState:UIControlStateDisabled];
[myButton setBackgroundImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
[myButton setBackgroundImage:[UIImage imageNamed:#"higligted.png"] forState:UIControlStateHighlighted];
[myButton setBackgroundImage:[UIImage imageNamed:#"highlighted+selected.png"] forState:(UIControlStateHighlighted | UIControlStateSelected)];
The last line shows how to set an image for the selected & highlighted state (that's the one IB can't set).
You don't need the selected images (line 4 & 6) if you're button dosn't need a selected state.
Another possibility:
Create a UIButton in Interface builder.
Give it a type 'Custom'
Now, in IB it is possible to change the background color
However, the button is square, and that is not what we want. Create an IBOutlet with a reference to this button and add the following to the viewDidLoad method:
[buttonOutlet.layer setCornerRadius:7.0f];
[buttonOutlet.layer setClipToBounds:YES];
Don't forget to import QuartzCore.h
Subclass UIButton and override setHighlighted and setSelected methods
-(void) setHighlighted:(BOOL)highlighted {
if(highlighted) {
self.backgroundColor = [self.mainColor darkerShade];
} else {
self.backgroundColor = self.mainColor;
}
[super setHighlighted:highlighted];
}
-(void) setSelected:(BOOL)selected {
if(selected) {
self.backgroundColor = [self.mainColor darkerShade];
} else {
self.backgroundColor = self.mainColor;
}
[super setSelected:selected];
}
My darkerShade method is in a UIColor category like this
-(UIColor*) darkerShade {
float red, green, blue, alpha;
[self getRed:&red green:&green blue:&blue alpha:&alpha];
double multiplier = 0.8f;
return [UIColor colorWithRed:red * multiplier green:green * multiplier blue:blue*multiplier alpha:alpha];
}
If you are not wanting to use images, and want it to look exactly like the Rounded Rect style, try this. Just place a UIView over the UIButton, with an identical frame and auto resize mask, set the alpha to 0.3, and set the background to a color. Then use the snippet below to clip the rounded edges off the colored overlay view. Also, uncheck the 'User Interaction Enabled' checkbox in IB on the UIView to allow touch events to cascade down to the UIButton underneath.
One side effect is that your text will also be colorized.
#import <QuartzCore/QuartzCore.h>
colorizeOverlayView.layer.cornerRadius = 10.0f;
colorizeOverlayView.layer.masksToBounds = YES;
Another possibility (the best and most beautiful imho):
Create a UISegmentedControl with 2 segments in the required background color in Interface Builder. Set the type to 'bar'. Then, change it to having only one segment. Interface builder does not accept one segment so you have to do that programmatically.
Therefore, create an IBOutlet for this button and add this to the viewDidLoad of your view:
[segmentedButton removeSegmentAtIndex:1 animated:NO];
Now you have a beautiful glossy, colored button with the specified background color.
For actions, use the 'value changed' event.
(I have found this on http://chris-software.com/index.php/2009/05/13/creating-a-nice-glass-buttons/). Thanks Chris!
Well I'm 99% percent positive that you cannot just go and change the background color of a UIButton. Instead you have to go and change the background images yourself which I think is a pain. I'm amazed that I had to do this.
If I'm wrong or if theres a better way without having to set background images please let me know
[random setBackgroundImage:[UIImage imageNamed:#"toggleoff.png"] forState:UIControlStateNormal];
[random setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
[random setBackgroundImage:[UIImage imageNamed:#"toggleon.png"] forState:UIControlStateNormal];
[random setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
Per #EthanB suggestion and #karim making a back filled rectangle, I just created a category for the UIButton to achieve this.
Just drop in the Category code: https://github.com/zmonteca/UIButton-PLColor
Usage:
[button setBackgroundColor:uiTextColor forState:UIControlStateDisabled];
Optional forStates to use:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
You can also add a CALayer to the button - you can do lots of things with these including a color overlay, this example uses a plain color layer you can also easily graduate the colour. Be aware though added layers obscure those underneath
+(void)makeButtonColored:(UIButton*)button color1:(UIColor*) color
{
CALayer *layer = button.layer;
layer.cornerRadius = 8.0f;
layer.masksToBounds = YES;
layer.borderWidth = 4.0f;
layer.opacity = .3;//
layer.borderColor = [UIColor colorWithWhite:0.4f alpha:0.2f].CGColor;
CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.cornerRadius = 8.0f;
colorLayer.frame = button.layer.bounds;
//set gradient colors
colorLayer.colors = [NSArray arrayWithObjects:
(id) color.CGColor,
(id) color.CGColor,
nil];
//set gradient locations
colorLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
[button.layer addSublayer:colorLayer];
}
add a second target for the UIButton for UIControlEventTouched and change the UIButton background color. Then change it back in the UIControlEventTouchUpInside target;
For professional and nice looking buttons, you may check this custom button component. You can use it directly in your views and tableviews or modify the source code to make it meet your needs. Hope this helps.
This isn't as elegant as sub-classing UIButton, however if you just want something quick - what I did was create custom button, then a 1px by 1px image with the colour I'd want the button to be, and set the background of the button to that image for the highlighted state - works for my needs.
I know this was asked a long time ago and now there's a new UIButtonTypeSystem. But newer questions are being marked as duplicates of this question so here's my newer answer in the context of an iOS 7 system button, use the .tintColor property.
let button = UIButton(type: .System)
button.setTitle("My Button", forState: .Normal)
button.tintColor = .redColor()
[myButton setBackgroundColor:[UIColor blueColor]];
[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
It's possible change this way or going on Storyboard and change background on options in right side.
Swift 3:
static func imageFromColor(color: UIColor, width: CGFloat, height: CGFloat) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: width, height: height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()!
context.setFillColor(color.cgColor)
context.fill(rect)
let img = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return img
}
let button = UIButton(type: .system)
let image = imageFromColor(color: .red, width:
button.frame.size.width, height: button.frame.size.height)
button.setBackgroundImage(image, for: .normal)
For iOS 15+ Apple provides a simple button configuration to accomplish this.
Objective-C:
randomButton.configuration = [UIButtonConfiguration filledButtonConfiguration];
Swift:
randomButton.configuration = .filled()