Set custom titleLabel properties for multiple UIButtons - ios

I've got a custom button like:
UIButton *catBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
catBtn1.frame = CGRectMake(0, 344, 427, 67);
catBtn1.titleLabel.font = categoryFont;
catBtn1.titleLabel.textColor = [UIColor whiteColor];
catBtn1.titleLabel.frame = CGRectMake(52, 21, 150, 15);
catBtn1.titleLabel.textAlignment = UITextAlignmentLeft;
Is there a quick way to set all these titleLabel properties for all my buttons without doing this for each button? I tried assigning an instance of UILabel to catBtn1.titleLabel but that didn't work...

You can create method for button like this...
-(UIButton *) customBtn:(NSString *)btntxt:(CGFloat)btnx:(CGFloat)btny:(CGFloat)btnwidth
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//[btn setTitle:btntxt forState:UIControlStateNormal];
btn.titleLabel.text = btntxt;
btn.titleLabel.font = categoryFont;
btn.titleLabel.textColor = [UIColor whiteColor];
btn.titleLabel.frame = CGRectMake(52, 21, 150, 15);
btn.titleLabel.textAlignment = UITextAlignmentLeft;
[btn setFrame:CGRectMake(btnx, btny, btnwidth, 40)];
return btn;
}
and you can call it like this any number of time when you need it
[YOURVIEW_OBJECT addSubview:[self customBtn:#"Samanmalliset" : 165:20 :115]];

You can create method that sets these properties and call it for each of your buttons.

Related

How to Add Button to Textfield in view

UITextField *tf3 = [[UITextField alloc] initWithFrame:CGRectMake(10, tf2.frame.origin.y+55,100, 30)];
tf3.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
//tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
tf3.backgroundColor=[UIColor whiteColor];
tf3.borderStyle=UITextBorderStyleLine;
tf3.placeholder=#"Save";
tf3.textAlignment=UITextAlignmentCenter;
view1 = [[UIView alloc] initWithFrame:CGRectMake(0,270, 100, 40)];
[view1 addSubview:tf3];
how can I add save button to this textField without using storyboard.
Use this Code,
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(65, 0, 35, 30);
button.backgroundColor = [UIColor blueColor];
[button setTitle:#"Save" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[tf3 addSubview:button];
hope its helpful
You can add it something like
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[tf3 addSubview:btn]
The right way:
In UITextField you have two properties / left view and right view / see how to enable them.
i think is something like:
tf3.leftViewMode = .AlwaysEnabled // Here you can place a enum options, such as always visible, visible only while edit and stuff
Next Step:
Create a UIButton:
var saveButton = UIButton(frame: CGRect(x: 0, y: 0:, width: 30, height: 30)
saveButton.setImage(named: "your_imgae", state: .Normal)
saveButton.addTarget(self, action: #selector(saveButtonPressed(_:) ...)
tf3.leftView = saveButton
Next Step
Create a function saveButtonPressed(sender: UIButton) and listen there for click event.
I haven't test it, but i think it should work.

Button Text not showing

When I try to post the following code, my app just shows an empty rectangle where the button's text should be. I am a beginner at xcode and I have determined this code from other tutorials.
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *player1 = [[UILabel alloc]initWithFrame:CGRectMake(35, 120,130, 30)];
[player1 setText:#"Player"];
player1.layer.borderWidth = 1.0;
player1.layer.borderColor = [UIColor blackColor].CGColor;
player1.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player1];
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
[score1 setTitle:#"0" forState:UIControlStateNormal];
score1.layer.borderWidth = 1.0;
score1.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview: score1];
UILabel *player2 = [[UILabel alloc]initWithFrame:CGRectMake(35, 150, 130, 30)];
[player2 setText:#"Opponent"];
player2.layer.borderWidth = 1.0;
player2.layer.borderColor = [UIColor blackColor].CGColor;
player2.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player2];
}
Like I said, everything but the text in the button is showing up.
The reason why the button looks blank is because its text color is actually white. Just give it a different color and 0 will now show:
score1.layer.backgroundColor = [UIColor blackColor].CGColor;
If you don't want to change the button background color, you may want to set the text color:
[score1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Read Question: [initWithFrame:frame] vs. [UIButton buttonWithType], for more about creating a UIButton with these different methods.
The problem is the way you're creating the button:
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
That's not how you do it. Do it like this:
UIButton *score1 = [UIButton buttonWithType:UIButtonTypeSystem];
score1.frame = CGRectMake(165, 120, 60, 30);
All will be well after that!

ios : subview of a button not showing up

My code is as follows:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect frame = CGRectMake(50, 125, 220, 40);
button.frame = frame;
button.layer.cornerRadius = 20;
button.backgroundColor = [UIColor colorWithRed:0.42 green:0.66 blue:0.31 alpha:1.0];
button.layer.masksToBounds = YES;
[self.view addSubview:button];
button.autoresizesSubviews = NO;
button.clipsToBounds = YES;
UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, 100)];
addLabel.text = #"+";
addLabel.textColor = [UIColor blackColor];
addLabel.textAlignment = NSTextAlignmentCenter;
addLabel.font = [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:80.0f];
[button addSubview:addLabel];
addLabel.autoresizesSubviews = NO;
addLabel.clipsToBounds = YES;
My button is showing up fine in iOS/iPhone simulator, however addLabel label is not showing up at all.
What am I missing?
The frame on your label is too large. The label text is being drawn outside the frame of your button as a result. Ideally, the frame of your label would be at most as tall as the button height.
Well UIButton already has a titleLabel. Have you tried just using that instead?
button.titleLabel.text = "+";
or even using a different button type:
UIButton* button = [UIButton buttonWithType:UIButtonTypeContactAdd];

Title of a Custom UIBarButtonItem from a UIButton not showing

I made a custom UIBarButtonItem from a UIButton. The custom bar button is visible, the action works, the size is correct, and the background color can be seen; however the manually set title cannot be seen. maybe background is laid over the text? I'm not sure, help?
In viewDidLoad:
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
resetButton.titleLabel.text = #"RESET";
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
You should set the title for button of using setTitle:forState: for particular control states, replace your code with below
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
[resetButton setTitle:#"RESET" forState:UIControlStateNormal]; //change this line in your code
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
Use setTitle:forState to set button title. For example
[resetButton setTitle:#"text" forState:UIControlStateNormal]

Avoid code repetition when coding UIButtons in iOS

I'm pretty new to iOS programming and Objective-C in general, so this question has probably been asked quiet a few times. Anyway:
In my iOS app, I have several UIButtons that I create the following way:
UIButton *webButton = [UIButton buttonWithType:UIButtonTypeCustom];
[webButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
webButton.frame = CGRectMake(10, 315, 300, 44);
[webButton setBackgroundImage:[UIImage imageNamed:#"WebButton.png"] forState:UIControlStateNormal];
[webButton setBackgroundImage:[UIImage imageNamed:#"WebButtonPressed.png"] forState:UIControlStateHighlighted];
Since I want the title of the button to be easily editable, I then add UILabels to the button instead of having them be part of the image used as the buttons background image.
UILabel *webLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 300, 15)];
webLabel.text = #"Some website";
webLabel.font = [UIFont boldSystemFontOfSize:16.0];
webLabel.textColor = [UIColor colorWithRed:62.0 / 255 green:135.0 / 255 blue:203.0 / 255 alpha:1];
webLabel.textAlignment = UITextAlignmentCenter;
webLabel.backgroundColor = [UIColor clearColor];
[webButton addSubview:webLabel];
[webLabel release];
This gets very tedious, when you have to go through this procedure every single time you want to create a new button. What is the best way to simplify this process, so I don't have to repeat myself over and over again when coding buttons?
Thanks.
I suspect want you want to do is to create a subclass of UIButton. That way you write your code once, but use it for any number of buttons. Something a bit like this:
// in your .h file
#import <UIKit/UIKit.h>
#interface WebButton : UIButton
+ (WebButton*) webButtonWithBackgroundImageNamed: (NSString*) imageName title: (NSString*) string andTarget: (id) target;
#end
// in your .m file
#import "WebButton.h"
#implementation WebButton
+ (WebButton*) webButtonWithBackgroundImageNamed: (NSString*) imageName title: (NSString*) string andTarget: (id) target;
{
WebButton *webButton = [WebButton buttonWithType:UIButtonTypeCustom];
[webButton addTarget:target action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
webButton.frame = CGRectMake(0, 0, 300, 44);
[webButton setBackgroundImage:[UIImage imageNamed: imageName] forState:UIControlStateNormal];
[webButton setBackgroundImage:[UIImage imageNamed: [NSString stringWithFormat:#"%#pressed", imageName]] forState:UIControlStateHighlighted];
UILabel *webLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 300, 15)];
webLabel.text = string;
webLabel.font = [UIFont boldSystemFontOfSize:16.0];
webLabel.textColor = [UIColor colorWithRed:62.0 / 255 green:135.0 / 255 blue:203.0 / 255 alpha:1];
webLabel.textAlignment = UITextAlignmentCenter;
webLabel.backgroundColor = [UIColor clearColor];
[webButton addSubview:webLabel];
[webLabel release];
return webButton;
}
And then to create and add the button to a view something a bit like this:
WebButton* webButton = [WebButton webButtonWithBackgroundImageNamed:#"WebButton" title:#"testing" andTarget:self];
CGRect webButtonFrame = [webButton frame];
webButtonFrame.origin.x = 10;
webButtonFrame.origin.y = 30;
[webButton setFrame:webButtonFrame];
[[self window] addSubview:webButton];

Resources