create hard custom button - objective-c

I can not figure out how to construct this button. I need help in the creation of this, note that the strip is not painted when the button is active. What elements should I use?
on link you can look this button
http://i.stack.imgur.com/0mmJ0.png
UIButton *clone = [[UIButton alloc] initWithFrame:frame];
[clone setTitle:strDate forState:UIControlStateNormal];
[clone setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
clone.frame = CGRectMake(19, post_offer_top*(i-1)+22, 281, 75);
clone.tag = my_id;
// [clone setTitle:[[buttons objectAtIndex:i-1] objectForKey:#"title"] forState:UIControlStateNormal];
clone.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
clone.contentEdgeInsets = UIEdgeInsetsMake(44, 31, 0, 0);
[clone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clone setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[clone.titleLabel setFont:[UIFont fontWithName:#"PF DinDisplay Pro" size:10]];
[clone.titleLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
any ideas?

You should initialize buttons like this [UIButton buttonWithType:UIButtonTypeCustom]

Related

UIButton setTitle Colour Changing not working

I have created scroll view programmatically and created N number of buttons using for loop, but now I need to change setTitle color accordingly the button selected or unselected
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(40,50,self.view.frame.size.width,40)];
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.showsHorizontalScrollIndicator = NO;
for (int i = 0; i<_pagesNameArray.count; i++) {
self.button = [[UIButton alloc]init];
self.button.frame = CGRectMake(i*150, 0, 150, 40);
self.button.tag = i;
self.button.backgroundColor = [UIColor clearColor];
[self.button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:[_pagesNameArray objectAtIndex:i] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[_scrollView addSubview:self.button];
}
[self setupSelector];
CGRect contentRect = CGRectZero;
for (UIView *view in _scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
_scrollView.contentSize = contentRect.size;
[self.view addSubview:_scrollView];
I also tried this method but, it's not working. Can anyone help me out?
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
-(void)tapSegmentButtonAction:(UIbutton *)sender
{
UIbutton *button=sender.tag;
[self.button setTitleColor:[UIColor blackColor]
forState:UIControlStateSelected];
}
I have created 3 different type of buttons and given tag to each button
self.button = [[UIButton alloc]init];
self.button.frame = CGRectMake(0, 0, 180, 30);
self.button.tag = 0;
self.button.backgroundColor = [UIColor clearColor];
[self.button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:#"LOCAL REPORTS" forState:UIControlStateNormal];
// [self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
self.button.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.button];
self.buttonNews = [[UIButton alloc]init];
self.buttonNews.frame = CGRectMake(135, 0, 180, 30);
self.buttonNews.tag = 1;
self.buttonNews.backgroundColor = [UIColor clearColor];
[self.buttonNews addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonNews setTitle:#"NEWS" forState:UIControlStateNormal];
//[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.buttonNews.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
self.buttonNews.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.buttonNews];
self.buttonTop = [[UIButton alloc]init];
self.buttonTop.frame = CGRectMake(135*2, 0, 180, 30);
self.buttonTop.tag = 2;
self.buttonTop.backgroundColor = [UIColor clearColor];
[self.buttonTop addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonTop setTitle:#"TOP PHOTOS" forState:UIControlStateNormal];
//[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.buttonTop.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
self.buttonTop.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.buttonTop];
[self animateButton];
-(void) animateButton
{
[self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
if (self.currentIndex == 0) {
[self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
if (self.currentIndex == 1) {
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
if (self.currentIndex == 2) {
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
}
}

Image in UIButton is darker tharn original

I'm trying to add a UIButton in a cell which already has a UIImageView in the background.
I put this code in configureCell
UIButton *mail = [[UIButton alloc] initWithFrame:CGRectMake(10, 270, 40, 40)];
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
mail.backgroundColor = [UIColor whiteColor];
[mail addTarget:self action:#selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:mail];
It works but the image in the UIButton is seems darker
I tried these but it didn't changed anything
mail.alpha = 1;
[cell.contentView bringSubviewToFront:mail];
Does anyone have an explanation for this, and a solution as well.
I had the same problem once and I solved it by setting the button's image for the UIControlStateHighlighted
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateHighlighted];
Hope it works for you
Try this
UIButton *mail=[UIButton buttonWithType:UIButtonTypeCustom];
mail.frame= CGRectMake(10, 270, 40, 40);
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
mail.backgroundColor = [UIColor clearColor];
[mail addTarget:self action:#selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:mail];

UIButton to show BackgroundImage, Image and Title

I want to create a UIButton that uses its setBackgroundImage, setImage, and setTitle properties, but when I add setTitle in combination with setImage and setBackgroundImage the title doesn't appear.
I've tried the following.
[button setBackgroundImage:[UIImage imageNamed:#"image3.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"month_pct_general.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%d",[button tag]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
[button setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 25)];
your title on button and on title your all image it there so add label on the image like this.
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(6, 5, 60, 20)];
[Label setText:#"label"];
Label.backgroundColor=[UIColor clearColor];
[Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
Label.textColor=[UIColor whiteColor];
[myButton addSubview:Label];
your all image above the title only so your title is in background so use label on image view.
If you are making GUI using interface builder, then click on the label and go to Editor-> Arrange -> Send to Front.

Programmatically Create Button to URL

I've managed to create a simple rounded rect button in Xcode through code but I'm having a little trouble actually getting it to do more what I want.
Ideal situation is a custom button type with image in both states as image.png linking to a URL (say www.google.com) can anyone help me out? I presume I need to write a method to do the link. That I can handle, just not sure how to hook it up without Interface Builder (targetting iOS)
Existing Code
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(supporturl) forControlEvents:UIControlEventTouchDown];
btn.frame = CGRectMake(20, 20, 150, 50);
[btn setTitle:#"Support and Help" forState:UIControlStateNormal];
[self addSubview:btn];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 150.0f, 50.0f)];
[btn setTitle:#"Support and Help" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(openWebPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
and in function
-(void) openWebPage
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}

placing image on table view iphone

I have three rows which are configured programmatically. I want to place an image only on the first row of the table view. The image has to cover the entire first row(not on the left side of table view cell by default)
Here is my code:
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case 1:
if(indexPath.row==1)
{
return 230.0; // first row is 123px high
}
default:
return 100.0; // all other rows are 40px high
}
}
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 0:
break;
case 1:
if (indexPath.row==1) {
UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(5, 10, 310, 40)];
[button1 setTitle:#"Give" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(give)
forControlEvents:UIControlEventTouchUpInside];
[button1.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button1];
UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(5, 60, 310, 40)];
[button2 setTitle:#"YouTube" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(Announcements)
forControlEvents:UIControlEventTouchUpInside];
[button2.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button2];
UIButton *button3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 setFrame:CGRectMake(5, 110, 310, 40)];
[button3 setTitle:#"Follow us on Twitter" forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(Twitter)
forControlEvents:UIControlEventTouchUpInside];
[button3.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button3];
UIButton *button4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 setFrame:CGRectMake(5, 160, 310, 40)];
[button4 setTitle:#"Follow us on FaceBook" forState:UIControlStateNormal];
[button4 addTarget:self action:#selector(Facebook)
forControlEvents:UIControlEventTouchUpInside];
[button4.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:button4];
/*[button1 release];
[button2 release];
[button3 release];
[button4 release];*/
}
break;
default:
break;
}
}
You can add a UIImageView to your contentview.