I want to center (both x and y axis) this + sign within my UIButton CGRect but it keeps getting pushed down the y-axis.
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
[aButton setTitle:#"+" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:(40.0)]];
[aButton setBackgroundColor:[UIColor grayColor]];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
aButton.layer.cornerRadius = 25.0;
aButton.frame = CGRectMake(75.0, 100.0, 50.0, 50.0);
This would look more correct to you if you had ascenders and descenders. There's a label inside the button and it is sized to accommodate the height of the full range of possible characters of this font. If you don't like the position of the label, you're free to move it. There are a lot of ways to do this; you might try playing with the button's titleEdgeInsets, for example.
Add this line after setting the title and font:
aButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 9, 0);
Related
I am creating a UIButton programatically, when i set the EdgeInsets of button title and image, the button title and image got BLUR. (See reference screenshot)
Why title and image gets blur?
Here is my code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
CGRect buttonFrame = CGRectMake(0.0, 0.0, mainFrame.size.width, mainFrame.size.height);
button.frame = buttonFrame;
[button setTitle:#"Press me" forState:UIControlStateNormal];
[button setTitle:#"Pressed" forState:UIControlStateSelected];
[button.titleLabel setFont:[UIFont fontWithName:FNT_ALL_TEXTS size:10]];
[button setImage:[UIImage imageNamed:imageNormal] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:imageSelected] forState:UIControlStateSelected];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage * refimage = [UIImage imageNamed:imageNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(buttonFrame.size.height*0.5, -refimage.size.width*1.0, 0.0f, 0.0f)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, buttonFrame.size.height*0.35, 0.0)];
[self addSubview:button];
dear just make sure that it is content vertical and horizontal alignment for button however u use image or text in button.
UIButton *btn;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
// horizontal Alignment
UIControlContentHorizontalAlignmentLeft
UIControlContentHorizontalAlignmentRight
UIControlContentHorizontalAlignmentFill
UIControlContentHorizontalAlignmentCenter
// Vertical Alignment
UIControlContentVerticalAlignmentTop
UIControlContentVerticalAlignmentFill
UIControlContentVerticalAlignmentBottom
UIControlContentVerticalAlignmentCenter
I have a custom button with the font.
customButtonSynchronize = [UIButton buttonWithType:UIButtonTypeCustom];
customButtonSynchronize.frame=CGRectMake(578, 27.5, 91, 29) ;
[customButtonSynchronize setBackgroundImage:[UIImage imageNamed:#"synchronize.png"]
forState:UIControlStateNormal];
customButtonSynchronize.titleLabel.font = [UIFont fontWithName:#"HelveticaLTStd-Roman" size:14.0f];
[customButtonSynchronize setTitle:#"Sincronizzare" forState:UIControlStateNormal];
/////HERE//////////
customButtonSynchronize.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
customButtonSynchronize.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
customButtonSynchronize.contentEdgeInsets = UIEdgeInsetsMake(8, 0, 0, 0);
customButtonSynchronize.backgroundColor=[UIColor clearColor];
[customButtonSynchronize addTarget:self action:#selector(synchronizeDB:) forControlEvents:UIControlEventTouchUpInside];
But the text above seems to be "Cut off" a little.See the top of "S".
I dont want to change frame of button,size of the font.Changing the inset DOESNOT avoid the problem. Is it a simulator bug or problem with helvetica font? Any work around?
set uibutton title to nil then create label with text and add subview to uibutton thats all, now it will display text correctly as you required
I'm trying to programmatically create a UIButton. However by default it's turning up white (which is the default color for my navigation bar, I feel that's relevant). I want it to just be Apple's default blue color in iOS7. I've managed to change the color for it's default state, but the moment I select it the text becomes white again. I cannot figure out how to keep it blue.
Could someone please explain to me how I can programmatically create a UIButton and have it act the same as though I created it in storyboard?
Current code:
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = CGRectMake(320 - 150, 0, 140, 40);
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
cancelButton.titleLabel.tintColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
cancelButton.titleLabel.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
Thank you.
Try this code :
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setFrame:CGRectMake(320 - 150, 0, 140, 40)];
[cancelButton setBackgroundColor:[UIColor clearColor]];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; // This will helps you during click time title color will be blue color
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton addTarget:self action:#selector(button_Action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelButton];
Just tried with
[cancelButton setTitleColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0/255.0 alpha:1.0] forState:UIControlStateNormal];
For more information read official documentation of UIButton.
Have you tried this
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
This will solve your problem,
[cancelButton setTitleColor:YOUR COLOR forState:SPECIFIED STATE];
The state values are :
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
Try using
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
for specifying colors for all the required states.
Swift:
let button = UIButton (type: UIButtonType.system)
This will create a button that uses the standard tint color just like a button added to a storyboard in interface builder.
I just switched from a UILabel to UIButton, intending to use the button's titleLabel property to display the information formerly displayed in the label replaced by the button.
Problem is, the text doesn't show up. I've checked to see if the button itself (normally with a transparent background, changed to red to check) is appearing, and it is, but the text isn't there. Here's a sample of the relevant code, which is identical to the original (working) code from the label, changing only lines like:
UILabel *firstLabel;
(...)
firstLabel.textColor = [UIColor blackColor];
to:
UIButton *firstLabel;
(...)
firstLabel.titleLabel.textColor = [UIColor blackColor];
Here's a full chunk for clarity:
firstLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -focusBarEndHeight - 55, barWidth, 60);
firstLabel.backgroundColor = [UIColor clearColor];
firstLabel.titleLabel.textColor = [UIColor blackColor];
firstLabel.titleLabel.text = [NSString stringWithFormat:#"%#\n%.2f%%\nTime--%#",focusItemName,focusItemPercent,actualDurationFocusItem];
[firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
firstLabel.titleLabel.numberOfLines = 0;
firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
[firstLabel setHidden:NO];
What am I missing? Any ideas?
Thanks!
UPDATE --
This may be related to a known bug!
Many thanks to the responders below. I implemented the first recommended fix, which resolved my initial problem, so my text now appears in the label. However, the UIControlStateHighlighted and UIControlStateSelected don't work.
For example, this code produces no discernible effect to the text when clicked:
firstLabel.backgroundColor = [UIColor clearColor];
[firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
After searching around SO for a while, I'm coming to the conclusion that there is a bug (at least in iOS 7) that prevents proper functioning of these methods. Unfortunately, I'm not smart enough to provide more detailed information, but plenty of recent posts point to a major problem in this area of UIControl.
I'll be ecstatic to be proven wrong, because I'd like to use this functionality.
UIButton responds to different messages, you can use the button state to change its title.
You can set the title like this;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:#"Title goes here" forState:UIControlStateNormal];
See Control State: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState
To set a title in an UIButton, you have to init the button:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];
and then use:
[button setTitle:#"text" forState:UIControlStateNormal];
where you must specific the control state (highlighted, selected, etc.)
try for example,
UIButton *firstLabel = [UIButton buttonWithType:UIButtonTypeCustom];
firstLabel.frame = CGRectMake(0, 60, 100, 50); //set the frame
[firstLabel setTitle:#"Hello" forState:UIControlStateNormal];
[firstLabel setBackgroundColor:[UIColor redColor]];//for test
[firstLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; //it will always displays green for normal
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];//if u touch it text changed to white instantly
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];//if touch it and hold it shows white color
I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.