where are the labels among the buttons - ios

In the image below I am attempting to put a single-character label between each button, but as the second image shows, when I do insert the labels, the buttons disappear and (if you look closely) the last button's text moves to the right.
Can you help me get the desired result, please?
buttons without labels
buttons with labels
ViewController.h
#property(nonatomic,assign) UILabel* theSuit;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString* cards = #"AKQJT98765432";
NSInteger yPipsOrigin = 100;
NSInteger xPipsOrigin = 100;
NSInteger xPipsStep = 40.0;
NSInteger xPipsCurrent = xPipsOrigin;
// Do any additional setup after loading the view.
for( int x=0;x<[cards length]; x++ ){
[cards substringWithRange:NSMakeRange(x,1)];
UIButton *b= [UIButton buttonWithType:UIButtonTypeRoundedRect];
xPipsCurrent += xPipsStep;
[b setTitle:[cards substringWithRange:NSMakeRange(x,1)] forState:UIControlStateNormal];
[b setTitle:#" " forState:UIControlStateDisabled];
[b setFrame:CGRectMake(xPipsCurrent, yPipsOrigin, 20, 20)];
[b setEnabled:YES];
[b setUserInteractionEnabled:YES];
[self.view addSubview:b];
[b addTarget:self action:#selector(spadeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
xPipsCurrent = xPipsOrigin + xPipsStep/2;
for( int x=0;x<[cards length]-1; x++ ){
xPipsCurrent += xPipsStep;
UILabel *lab = self.theSuit;
lab.text = #"Z";
lab.backgroundColor = [UIColor clearColor];
lab.center = CGPointMake(xPipsCurrent, yPipsOrigin);
[self.view addSubview:lab];
}
}
}

NSString* cards = #"AKQJT98765432";
NSInteger yPipsOrigin = 100;
NSInteger xPipsOrigin = 100;
NSInteger xPipsStep = 40.0;
NSInteger xPipsCurrent = xPipsOrigin;
for( int x=0;x<[cards length]; x++ )
{
[cards substringWithRange:NSMakeRange(x,1)];
UIButton *b= [UIButton buttonWithType:UIButtonTypeRoundedRect];
xPipsCurrent += xPipsStep;
[b setTitle:[cards substringWithRange:NSMakeRange(x,1)] forState:UIControlStateNormal];
[b setTitle:#" " forState:UIControlStateDisabled];
[b setFrame:CGRectMake(xPipsCurrent, yPipsOrigin, 20, 20)];
[b setEnabled:YES];
[b setUserInteractionEnabled:YES];
[self.view addSubview:b];
[b addTarget:self action:#selector(spadeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(xPipsCurrent+b.frame.size.width, yPipsOrigin, 20, 20)];
lab.text = #"Z";
lab.textAlignment = NSTextAlignmentCenter;
lab.backgroundColor = [UIColor clearColor];
[self.view addSubview:lab];
}

Related

Custom UIView subClass with UIScrollView

I am creating a class, that creates an option bar with multiple scroll views accessible by buttons.
This is how I add the subview with its content:
NSArray *scroll1 = [NSArray arrayWithObjects:#"normal",#"tiltshift",#"zoom",#"normal",#"blur",#"outline",#"coming soon", nil];
NSArray *scroll2 = [NSArray arrayWithObjects:#"Emoji Natur-01",#"Emoji Natur-02",#"Emoji Natur-03",#"Emoji Natur-04",#"Emoji Natur-05",#"Emoji Natur-06",#"Emoji Natur-07",#"Emoji Natur-08",#"Emoji Natur-09",#"Emoji Natur-10",#"Emoji Natur-11",#"Emoji Natur-12", nil];
NSArray *scroll3 = [NSArray arrayWithObjects:#"Linear",#"Parallel",#"Parallel 2",#"Crescendo",#"Dubstep",#"Blackhole",#"coming soon", nil];
NSArray *content = [NSArray arrayWithObjects:scroll1,scroll2,scroll3, nil];
[self.view addSubview:[self.bottomOptionView initWithFrame:self.view.frame withContent:content]];
The Subclass then generate a scrollview for each Array, with the buttons based on the content of the array:
-(void)addScrollViewOfContent:(NSArray*)array{
int viewNumber = array.count;
for (int i = 0 ; i < viewNumber; i++) {
//create the main buttons
NSArray *content = array[i];
UIButton *buttonAccess = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat buttonEdge = 40;
CGFloat buttonSpace = self.frame.size.width /viewNumber;
int placeOfButton = i+1;
CGFloat buttonAnchor = ((placeOfButton*(buttonSpace)) - (buttonSpace /2+ buttonEdge/2));
buttonAccess.frame = CGRectMake(buttonAnchor, 0 , buttonEdge, buttonEdge);
[buttonAccess setBackgroundColor:[UIColor redColor]];
buttonAccess.tag = i;
[buttonAccess addTarget:self action:#selector(buttonAccess:) forControlEvents:UIControlEventTouchDown];
[self addSubview:buttonAccess];
UIScrollView *ScrollView = [[UIScrollView alloc]init];
ScrollView.frame = CGRectMake(0, 50, self.frame.size.width, self.frame.size.height);
ScrollView.showsHorizontalScrollIndicator = YES;
ScrollView.tag = i;
ScrollView.backgroundColor =[UIColor colorWithRed:0 green:0.0 blue:0.0 alpha:0.0];
ScrollView.indicatorStyle = UIScrollViewDecelerationRateNormal ;
//UIImageView *selector = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"FILTER_SELECT.png"]];
CGFloat btnX = 0;
for (int i = 0 ; i < content.count; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(btnX, 2 , 65, 65);
UIImage *img = [UIImage imageNamed:[content objectAtIndex:i]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(subButtonTarget:) forControlEvents:UIControlEventTouchDown];
button.tag = i;
[button setTitle:[content objectAtIndex:i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"MyriadPro-Cond" size:15];
button.titleLabel.textColor = [UIColor whiteColor];
button.titleLabel.textAlignment = UIBaselineAdjustmentAlignBaselines ;
button.showsTouchWhenHighlighted = YES;
[ScrollView addSubview:button];
btnX = btnX + 100;
}
btnX = btnX - 80;
ScrollView.contentSize = CGSizeMake(btnX + 50, 80);
ScrollView.hidden = YES;
[self.scrollViewArray addObject:ScrollView];
[self addSubview:ScrollView];
}
This gives me exactly what I want.
Then I detect which button, of which scroll view is pressed to trigger the action.
-(void)subButtonTarget:(UIButton *)sender{
int button = sender.tag;
int scrollview = self.selectedScrollView;
[self.videoEditor subAction:button ofScrollView:scrollview];
}
-(void)buttonAccess:(UIButton *)sender{
// self.selectedScrollView = sender.tag;
for (int i=0; i< self.scrollViewArray.count; i++) {
UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:i];
scrollview.hidden= YES;
}
int scrollIndex = sender.tag;
UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:scrollIndex];
scrollview.hidden = NO;
}
Basically when one of the button is touched, it calls a function in the view controller in which the sublclass was initially called.
The problem is that, on touchevent nothing happens, except for NSLog. Would that be because of the dependency relation of the subClass and the VC that uses it?
You should set this one for each UIScrollView
ScrollView.delaysContentTouches = NO;
And try to subclass UIScrollView with override method
-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}

Get selected button value from the UIButton

From the array I am creating button with the array values inside each button, Here I want to place a radio button before each button. while clicking the radio button i want to get it selected or not. In the below code I can display the button and the radio button image but cannot get the selected button value. How can i get the radio button selected or not for each button
//My Array
labelArrays = [NSMutableArray arrayWithObjects:#"one", #"two", #"three",#"four",#"five",#"six", nil];
//Creating button with placing a radio button image inside
-(void) createbutton:(CGRect) frame {
CGFloat xCoordinate = frame.origin.x;
CGFloat yCoordinate = frame.origin.y;
CGFloat buttonHeight = frame.size.height;
CGFloat buttonWidth = frame.size.width;
CGFloat gapBetweenTwoButton = 2;
for(int counter = 0; counter < [labelArrays count]; counter++) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button =[UIColor clearcolor];
[button setFrame:CGRectMake(xCoordinate,yCoordinate, 100, 40)];
NSString* titre = [labelArrays objectAtIndex:counter];
[button setImage:[UIImage imageNamed:#"radio_selected.png"] forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(0,10,0,0);
[button setTitle:[NSString stringWithFormat:#"%#",titre] forState:UIControlStateNormal];
[button setUserInteractionEnabled:YES];
[self. button addTarget: self action: #selector(buttonAction:)forControlEvents: UIControlEventTouchUpInside];
[self addSubview: button];
if((counter+1)%3 == 0) {
xCoordinate = 0;
yCoordinate = yCoordinate + buttonHeight + gapBetweenTwobutton;
xCoordinate = xCoordinate + buttonWidth + gapBetweenTwoLabels;
}
}
//my frame
[self createbutton:CGRectMake(32,20,220,40)];
It's very simple to get selected button value by using its tag value. See here and try below code.
Put these line in viewDidLoad()
labelArrays = [NSMutableArray arrayWithObjects:#"one", #"two", #"three", #"four", #"five", #"six", nil];
[self createButtons];
And then define these methods.
- (void)createButtons
{
int x = 30, y = 20;
for (int i = 0; i<labelArrays.count; i++)
{
UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame = CGRectMake(x, y, 100, 40);
btnImage.backgroundColor = [UIColor redColor];
btnImage.tag = 700+i;
[btnImage setImage:[UIImage imageNamed:#"radio_unselect.png"] forState:UIControlStateNormal];
btnImage.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[btnImage setTitle:[NSString stringWithFormat:#"%#", [labelArrays objectAtIndex:i]] forState:UIControlStateNormal];
[btnImage addTarget: self action: #selector(changeValue:)forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:btnImage];
if ((i+1)%2 == 0)
{
x = 30;
y = y+btnImage.frame.size.height+10;
}
else
{
x = x+btnImage.frame.size.width+30;
}
}
}
- (IBAction)changeValue:(UIButton *)sender
{
UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag];
if (btn.selected == TRUE)
{
[btn setImage:[UIImage imageNamed:#"radio_unselect.png"] forState:UIControlStateNormal];
btn.selected = FALSE;
}
else
{
[btn setImage:[UIImage imageNamed:#"radio_select.png"] forState:UIControlStateNormal];
btn.selected = TRUE;
NSLog(#"%#", btn.titleLabel.text);
}
}

how to create action multiple dynamic buttons in ios?

i am creating multiple buttons dynamically now i want to set action for each buttons.
when i press buttons its show result on next screen in my application .
i am using FMDB library to read data from database.(read database according to id on buttons press). i know how to read database from database . i want only fetch data on buttons press and display that
data on table view .how to create action for each buttons?
this my buttons code :
-(void)DynamicButton:(NSMutableArray*)objectName
{
for(UIView *view in [scrollView subviews])
{
[view removeFromSuperview];
}
int yPossion = 100, xPossion = 44; int temp = 0;
for (int i = 0; i<[objectName count]; i++)
{
SMSCategory *cat = [objectName objectAtIndex:i];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTag:i];
[aButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[aButton setBackgroundColor:[UIColor blackColor]];
[aButton setBackgroundImage:[UIImage imageNamed:#"icon-menu.png"]
forState:UIControlStateNormal];
[aButton setTitle:[NSString stringWithFormat:#"%d",i]
forState:UIControlStateNormal];
[aButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)];
aButton.highlighted=YES;
[scrollView addSubview:aButton];
;
xPossion += aButton.frame.size.width+35;
temp++;
if (temp==3)
{
yPossion = aButton.frame.origin.y+aButton.frame.size.height+20;
temp = 0;
xPossion = 44;
yPossion += aButton.frame.size.width-15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width ,yPossion-
50)];
}
UILabel *label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:YES];
[label setText:cat.Name];
[label setTextColor:[UIColor blackColor]];
label.font = [UIFont systemFontOfSize:12];
[label sizeToFit];
[label setFrame:CGRectMake(4, 44, 70, 60)];
[scrollView addSubview:label];
[aButton addSubview:label];
}
}
UIButton *test = [self buttonWithTitle:#"test" target:self selector:#selector(testDoit) frame:CGRectMake(self.view.center.x-50, 270.0, 100.0, 50.0) : 12];
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame :(int)fontSize{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:frame];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
button.userInteractionEnabled = YES;
button.titleLabel.textAlignment = UITextAlignmentCenter;
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
button.titleLabel.textColor = [UIColor blackColor];
button.showsTouchWhenHighlighted = YES;
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleRightMargin|
UIViewAutoresizingFlexibleTopMargin|
UIViewAutoresizingFlexibleHeight|
UIViewAutoresizingFlexibleBottomMargin;
[button addTarget:self action:inSelector forControlEvents:UIControlEventTouchUpInside];
//[button autorelease];
return button;
}

Auto Fill scrollview with UIButtons from NSArray

I need to fill a view becoming the contents of a NSArray object in a UIButtons and if the number of buttons does not fit the view to scroll. For example, if the array.count is 25 and only fit 20 buttons, 4 columns of 5 rows, the 5 remaining buttons are to be displayed by scrolling.
I'm currently testing it so:
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 728, 984)];
sv.backgroundColor = [UIColor greenColor];
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
sv.contentSize = CGSizeMake(PAGE_WIDTH, sv.frame.size.height);
sv.pagingEnabled = YES;
[self.view addSubview:sv]; // assuming a view controller
NSMutableArray *freeHoursList = [NSMutableArray arrayWithObjects:
[NSString stringWithFormat:#"10"],
[NSString stringWithFormat:#"11"],
[NSString stringWithFormat:#"12"],
[NSString stringWithFormat:#"17"],
[NSString stringWithFormat:#"18"],
[NSString stringWithFormat:#"19"],
[NSString stringWithFormat:#"10"],
[NSString stringWithFormat:#"11"],
[NSString stringWithFormat:#"12"],
[NSString stringWithFormat:#"17"],
[NSString stringWithFormat:#"18"],
[NSString stringWithFormat:#"19"],nil];
for (int i = 0; i < freeHoursList.count; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.backgroundColor = [UIColor redColor];
btn.frame = CGRectMake(i * (BUTTON_WIDTH + BUTTON_PADDING), 10, BUTTON_WIDTH, BUTTON_HEIGHT);
btn.tag = i + MAGIC_BUTTON_TAG_OFFSET; // to relate to the array index
NSString *numero = [freeHoursList objectAtIndex:i];
[btn setTitle:[NSString stringWithFormat:#"%#",numero] forState:UIControlStateNormal];
//[btn addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sv addSubview:btn];
}
And this is what I get:
I would go with adding variable that keeps track of an actual Y position and an actual column. So you would check if the next button fits the width of the scroll and if its not - set column to 0 and add X to the button Y position. It would looks like this:
int BUTTON_Y = 10;
int column = 0;
for (int i = 0; i < freeHoursList.count; i++, column++)
{
if(column*(BUTTON_WIDTH + BUTTON_PADDING) + BUTTON_WIDTH > sv.contentSize.width) {
BUTTON_Y += BUTTON_HEIGHT;
column = 0;
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.backgroundColor = [UIColor redColor];
btn.frame = CGRectMake(column * (BUTTON_WIDTH + BUTTON_PADDING), BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
btn.tag = column + MAGIC_BUTTON_TAG_OFFSET; // to relate to the array index
NSString *numero = [freeHoursList objectAtIndex:i];
[btn setTitle:[NSString stringWithFormat:#"%#",numero] forState:UIControlStateNormal];
//[btn addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sv addSubview:btn];
}

UIScrollView with a large number of UIButtons

What I want is a UIView with lots of UIButtons in it. They get placed and arranged according to data stored in an NSArray. Because there are quite a lot of buttons they don't fit on the screen all at once. The user should be able to zoom out to see all the buttons or to zoom in to see details (the label on them) and to easily select them.
I tried two different approaches:
1) I constructed a UIView subclass, put the buttons in it and an instance of this View inside a UIScrollview.
Effect: I can access all Buttons via their tag and scrolling and zooming works fine. BUT I can't get the buttons to handle any events (press on them)...
2) I wrote a UIViewController with exactly the same functionality and added an instance of it to the UIScrollView.
Effect: I can press the buttons now, but scrolling and zooming have stopped to work.
Here the relevant Code of the View:
- (UIView *)initWithArray:(NSArray *)nArray{
self = [super init];
if (self) {
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:(__some Code to place the Button__);
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
}
}
return self;
}
And the Code for the matrixController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *nArray = [[NSMutableArray alloc] __some code___];
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:CGRectMake(__some Code to place the Button__];
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
}
And the code for the ScrollViewController:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 970)];
[self.view addSubview:scrollView];
[scrollView setBackgroundColor:[UIColor blackColor]];
//Zooming
[scrollView setMinimumZoomScale:0.25];
[scrollView setMaximumZoomScale:4.0];
[scrollView setDelegate:self];
// constructing the view
[scrollView addSubview:chartView];
[scrollView bringSubviewToFront:chartView];
OR
[scrollView addSubview:[matrixController view]];
How can I get this to work??
I'm able to get a scroll view containing multiple buttons to pan and zoom just fine, with the buttons still handling touch events:
- (void)didTapButton:(UIButton *)button
{
NSLog(#"Button %ld", (long)button.tag);
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f, scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
EDIT: I said not to rely on tag in my comment below, yet I'm doing in here. :) It's merely so I could log the button number, so that part should be ignored.
for (int i=0; i<10; i++)
{
UIButton *scrollingbutton_outlet = [[UIButton alloc] init];
scrollingbutton_outlet = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"box_normal.png"];
scrollingbutton_outlet.frame = CGRectMake(0, 100, img.size.width, img.size.height);
[scrollingbutton_outlet setTitle:[NSString stringWithFormat:#"%d",i+1] forState: UIControlStateNormal];
scrollingbutton_outlet.tag=i+1;
[buttonArray addObject:[NSString stringWithFormat:#"%d",i+1]];
buttonArray = [[NSMutableArray alloc] init];
[scrollingbutton_outlet setBackgroundImage:img forState:UIControlStateNormal];
[scrollingbutton_outlet addTarget:self
action:#selector(scrollbuttonpress:)
forControlEvents:UIControlEventTouchUpInside];
scrollingbutton_outlet.frame = CGRectMake(img.size.width*i,0, img.size.width, scrollviewoutlet.frame.size.height);
[scrollviewoutlet addSubview:scrollingbutton_outlet];
width = scrollingbutton_outlet.frame.size.width;
height = scrollingbutton_outlet.frame.size.height;
scrollviewoutlet.contentSize = CGSizeMake(width*i+scrollingbutton_outlet.bounds.size.width+5, height);
}

Resources