Trying to place UILabel on top of UIButton in a custom UIAlertView - ios

What I'm trying to do is place a UILabel on top of a UIButton.
The UIButton have an image as a background and the result is I cannot see the normal text on the UIButton, therefore I think to place the UILabel on top of it.
My code is:
NAlertView *customAlert = [[NAlertView alloc] initWithTitle:#"title" message:#"message" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#"cancel", nil];
[customAlert show];
NAlertView.m:
- (void)layoutSubviews
{
for (id obj in self.subviews) //Search in all sub views
{
if ([obj isKindOfClass:[UIImageView class]]) // Override UIImageView (Background)
{
UIImageView *imageView = obj;
[imageView setImage:[UIImage imageNamed:#"CustomAlertView"]];
[imageView setAlpha:0.7];
[imageView sizeToFit];
}
if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
{
UILabel *label = (UILabel*)obj;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor clearColor];
}
if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
{
UIButton *button = (UIButton*)obj;
CGRect buttonFrame = button.frame; // Change UIButton size
buttonFrame.size = CGSizeMake(127, 35);
CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
buttonFrame.origin.y = newYPos;
button.frame = buttonFrame;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
switch (button.tag)
{
case kFIRST_BUTTON:
{
[button setImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"short_button_gold_on.png"] forState:UIControlStateHighlighted];
}
break;
case kSECOND_BUTTON:
{
[button setImage:[UIImage imageNamed:#"short_button_black_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"short_button_black_on.png"] forState:UIControlStateHighlighted];
}
break;
}
}
}
[self updateConstraints];
}
Result:

That means you just want to add Title on UIButton?
Try This -
[button setBackgroundImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setTitle:#"Title" forState:UIControlStateNormal];

Replace
[button setImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
to
[button setBackgroundImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
change all such possibilities.
and use the below method to change the text of the button
[button setTitle:#"text" forState:UIControlStateNormal];
Hope it helps.

Related

UIButton set title color not working

I have created the array of UIButtons , having the same action,
if i click the first button, the first button Colour should be change, other button Colour should not be changed. how to achieve this?
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Thanks advance
for that you have to take one extra UIButton Object in you header file.
While user click on any button you have to store that selected Button in refbutton object and use like below.
#interface YourViewController ()
{
UIButton *btnSelected;
}
Now Move to your Button Action:
-(IBAction)buttonTapped:(UIButton *)sender {
if(_btnSelected){
_btnSelected.titleLabel.textColor = [UIColor blackColor];
}
_btnSelected = sender;
sender.titleLabel.textColor = [UIColor whiteColor];
}
Hope this will help you.

How to store all selected buttons values and show them on UILabel?

Actually I just want to make it like:
When user taps button 1 , it stores its value in a string and when user selects another button; it also store its value also and when he de-select the button again it must be removed from string and also from uiLabel
How it will be possible ?
I have to know all possible ways
1) Declare in your ViewController.m
#interface ViewController ()
{
UIButton *button1;
UIButton *button2;
NSString *btn1String;
NSString *btn2String;
BOOL btn1isClicked;
BOOL btn2isClicked;
UILabel *label;
}
2) Buttons and label (can be in your ViewDidLoad)
btn1isClicked = NO;
btn2isClicked = NO;
//initialy set to nothing
btn1String = #"";
btn2String = #"";
//create first button
button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 50, 200, 50)];
//button 1 clicked
[button1 addTarget:self action:#selector(button1Clicked) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Button One" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[self.view addSubview:button1];
//create second button
button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 150, 200, 50)];
//button 2 clicked
[button2 addTarget:self action:#selector(button2Clicked) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"Button Two" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:button2];
//create label
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 320, 50)];
[label setText:#""];
[self.view addSubview:label];
3) Button Clicked methods
- (void) button1Clicked
{
if (!btn1isClicked)
{
//set bool to yes
btn1isClicked = YES;
[button1 setTitle:#"Button One Clicked" forState:UIControlStateNormal];
//set button 1 value
btn1String = #"btn1value";
}
else
{
//set bool to no
btn1isClicked = NO;
[button1 setTitle:#"Button One" forState:UIControlStateNormal];
btn1String = #"";
}
//update label
[label setText:[NSString stringWithFormat:#"%# %#", btn1String, btn2String]];
}
- (void) button2Clicked
{
if (!btn2isClicked)
{
btn2isClicked = YES;
[button2 setTitle:#"Button Two Clicked" forState:UIControlStateNormal];
//set button 2 value
btn2String = #"btn2value";
}
else
{
btn2isClicked = NO;
[button2 setTitle:#"Button Two" forState:UIControlStateNormal];
btn2String = #"";
}
//update label
[label setText:[NSString stringWithFormat:#"%# %#", btn1String, btn2String]];
}
Hope this helps
-(IBActions)buttonDidTap:(UIButton*)btn { //the target selector the button is attached to
if ([btn isSelected]) { //check if the button is selected
NSString *buttonTitleValue = btn.currentTitle; //Get the button title value
myLabel.text = buttonTitleValue; //set the UIlabel text to the title of the button
}
}
See: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instp/UIButton/currentTitle

Dealing with a position for a single UIButton in a custom UIAlertView

I've a nice function for customizing UIAlertView, it works perfect with 2 buttons but the issue is, how do I know there is only 1 button and not 2 buttons?
Or how do I solve the problematic positioning with only 1 button?
My code is:
- (void)layoutSubviews
{
for (id obj in self.subviews) //Search in all sub views
{
if ([obj isKindOfClass:[UIImageView class]])
{
UIImageView *imageView = (UIImageView*)obj;
float width = 284.0;
float height = 141.0;
CGSize size = CGSizeZero;
size.width = width;
size.height = height;
UIImage *newImage = [self imageWithImage:[UIImage imageNamed:#"CustomAlertView"] scaledToSize:size];
imageView.image = newImage;
[imageView sizeToFit];
}
if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
{
UILabel *label = (UILabel*)obj;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor clearColor];
label.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
}
if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
{
UIButton *button = (UIButton*)obj;
CGRect buttonFrame = button.frame; // Change UIButton size
buttonFrame.size = CGSizeMake(127, 35);
CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
buttonFrame.origin.y = newYPos;
button.frame = buttonFrame;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
switch (button.tag)
{
case kFIRST_BUTTON:
{
[button setBackgroundImage:[UIImage imageNamed:#"short_orange_button_off"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"short_orange_button_on"] forState:UIControlStateHighlighted];
}
break;
case kSECOND_BUTTON:
{
[button setBackgroundImage:[UIImage imageNamed:#"short_button_black_off"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"short_button_black_on"] forState:UIControlStateHighlighted];
}
break;
}
}
}
[self updateConstraints];
}
OK, just figure it out by myself, I added:
// Determine how many buttons we deal with
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
for (id obj in self.subviews)
{
if ([obj isKindOfClass:[UIButton class]])
{
self.numOfButtons++;
}
}
NSLog(#"self.numOfButtons: %d", self.numOfButtons);
});
Right after:
- (void)layoutSubviews
Then I know how many buttons I deal with.

How to change button image added to TableView section header

I've added a button to section header which shows a view on clicking it. My need is that I've to show an "Up-arrow" image while view is shown and "Down-arrow" image when view is hidden.
How can I achieve this? Please help me ...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 316, 60)];
[btn setTag:section];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
btn.backgroundColor=[UIColor clearColor];
}
I will suggest that you can have 2 images - 1 for 'Up' and 1 for 'Down' arrow.
Set default image to the btn for state UIControlStateNormal and set the other image for state UIControlStateSelected.
Now in your sectionTapped: method just change the state of the button.
So when you show or hide your view you need to set the button selected YES/NO.
Hope this helps.
I had done this before in one of my app.
Take this code as Reference
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}
You can trying using BOOL to check if the button has been pressed or not and animate to rotate the image in your button to point up or down.
-(IBAction)dropdownBtnClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (!isDropdownEnabled)
{
//BOOL - checking if the button has been pressed or not
isDropdownEnabled=TRUE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(270/180*M_PI);
}
else
{
isDropdownEnabled=FALSE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(0);
}
[UIView commitAnimations];
}

I have to perform togling for single UIButton which I have generated from loop for different frame

In my iOS application, I have to change image as per button click.
I have to perform togling for single UIButton which I have generated from loop for different frame
It is working fine if I perform action on single button, if I have performing action for multiple button the image is not properly changing as per button action
Here is my button
btnFullScreen = [[UIButton alloc] init];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[btnFullScreen setFrame:CGRectMake(self.view.frame.size.width-30,8,16,14)];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[btnFullScreen addTarget:self action:#selector(showPopUp:) forControlEvents:UIControlEventTouchUpInside];
btnFullScreen.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:btnFullScreen];
Here is my button action code
-(void)showPopUp:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
[self.parent.parent addPopOversExcept:self.parent];
}
Thanks
I had done this before in one of my app.
I was creating button in each tableview cell.
So,Take this code as Reference.
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}

Resources