Gentle programmers, here for your inspection I present code that creates a scrolling view and populates it with buttons:
UIScrollView * testScroll =
[[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, 80, 90)];
CGFloat y = 0;
for (int i = 0;i<5;i++) {
UIButton *mybut = [[UIButton alloc] initWithFrame:CGRectMake(0, y, 75, 25)];
NSString *title = [NSString stringWithFormat:#"button: %d",i]
[mybut setTitle:title forState:UIControlStateNormal];
[mybut addTarget:self action:#selector(Ok:)
forControlEvents:UIControlEventTouchUpInside];
y = y+35;
}
This is all well and good, for those that like scrolling lists of buttons. However, I wish to have a scrolling list of labels! How might I adjust this code to achieve this, my ultimate goal?
Well, if I could understand it right, you want something like that:
UIScrollView * testScroll =
[[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, 80, 90)];
CGFloat y = 0;
for (int i = 0;i<5;i++) {
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, y, 75, 25)];
NSString *text = [NSString stringWithFormat:#"Label: %d",i]
[myLabel setText:title];
[testScroll addSubview:myLabel];
[myLabel release];
y = y+35;
}
Let me know if it is what you want.
Cheers,VFN
PS: Don't you prefer to use a table to do this, in the case you have many labels?
Related
I have created the buttons using the loop and setting the properties and synthesize the buttons. Now i want to change the button color in the another view controller. I am setting the tag values for each button and i can get the tag values properly in another view controller, when select the buttons. Now i want to change the background color of each buttons.
Here it's sample code,
In CustomView.h
UIButton *customBtn;
property (nonatomic, strong) UIButton *customBtn;
#synthesize customBtn;
In CustomView.m
for (int i=0; i<=[resultArray count]; i++)
{
customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
[customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customBtn];
X = X + 30;
}
In View controller:
viewController.customBtn.backgroundColor = [UIColor blackColor];
It does affects all the buttons background color, so how can i change the background color of each buttons. If i am creating seperate instance for all the buttons and i can change the background color for the buttons. Using single instance of the buttons, how can we change the color of the button background.
Please Help me out.
Thanks!
for (int i=0; i<=[resultArray count]; i++)
{
customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
[customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customBtn];
X = X + 30;
customBtn.tag = i;
[buttonAry addObject:customBtn];
}
By End of the loop you will have n number of buttons in buttonAry and each button will have unique tag.
You can read that array in another class
for (int i=0; i<=[buttonAry count]; i++)
{
UIButton *button = [buttonAry objectAtIndex:i];
UIColor *color = [colorAry objectAtIndex:i];
button.backgroundColor = [UIColor color];
}
In colorAry you can have different colors
Use the viewWithTag method. e.g.
UIButton *button = (UIButton *)[viewController.view viewWithTag:aTag];
Either you can do with viewWithTag.
Or, you can create an array of IBOutlets, then your work will be done.
Either follow Anoop Vaidya, or use NSNotification to send your button object to another class, it'll hold all properties of your button.
for (int i=0; i<=[resultArray count]; i++)
{
customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
[customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];
customBtn.tag = i;
[self.view addSubview:customBtn];
X = X + 30;
}
-(IBAction)MyAction:(id)sender{
UIButton *btn = (UIButton *)sender;
NSLog:(#"tag:%d"btn.tag);
// here you find your specific button,
}
I successfully created a UIscrollview (horizontal) for 50 images and also created a label of "1 of 50" page number displaying at the top of the image along with the scroll view(for example user scroll to next image it would show 2 of 50, 3 of 50, so forth). I am able to display a label at the bottom of the image as a description. My problem is how can I create or code to display different description for each image? Im thinking of NSarray but it shows some random numbers. Is there a sample I can look at to see what I am doing wrong. you can see the description part where I am lost from there. Thanks =)
CGFloat xOffset = 0;
NSInteger pageNumber = 1;
NSUInteger i;
for (i = 1; i <= 5; i++)
{
NSString *imageName = [NSString stringWithFormat:#"creative%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(xOffset, 0, 320, 288.5)];
[ourScrollView addSubview:imageView];
[imageView release];
UILabel *pageNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, -10, 320, 40)];
[pageNumberLabel setText:[NSString stringWithFormat:#"%d of 5", pageNumber]];
[pageNumberLabel setTextAlignment:UITextAlignmentLeft];
[pageNumberLabel setTextColor:[UIColor whiteColor]];
[pageNumberLabel setBackgroundColor:[UIColor clearColor]];
[pageNumberLabel setFont:[UIFont fontWithName:#"MarkerFelt-Thin" size:18]];
[ourScrollView addSubview:pageNumberLabel];
[pageNumberLabel release];
NSArray *description = [NSArray arrayWithObjects:#"wow", #"bam", #"pow", #"zing", #"bling",nil];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, 250, 320, 40)];
[descriptionLabel setText:[NSString stringWithFormat:#"%#", description]];
[descriptionLabel setTextAlignment:UITextAlignmentLeft];
[descriptionLabel setTextColor:[UIColor whiteColor]];
[descriptionLabel setBackgroundColor:[UIColor clearColor]];
[ourScrollView addSubview:descriptionLabel];
[descriptionLabel release];
xOffset = xOffset + 320;
pageNumber = pageNumber + 1;
You're setting the descriptionLabel with a format of #"%d", which is a decimal format. What is "description"? If it's a NSString then you should be using #"%#".
I would like to create 10 buttons programmatically. Each button has a subview with two labels (one string and one integer).
After I have created these 10 buttons in a loop, I would like to access both labels. I tried to NSLog the labels of the button with tag 0, but it didn't work.
All of this feels a bit clumsy, so please correct me if I'm going nowhere with this:
for (int i = 0; i < 9; i++) {
UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease];
btn.tag = i;
[btn setBackgroundImage:nil forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// LABELS
UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
btnTitle.text = #"Empty";
UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
pageTitle.text = #"x";
[indexView addSubview:btn];
[btn addSubview:btnTitle];
[btn addSubview:pageTitle];
}
This is what I've tried and where my problem is:
NSLog (#"Accessing label 1 in button with tag 0 in indexView: '%#'", [[indexView viewWithTag:0] btnTitle.text]);
NSLog (#"Accessing label 2 in button with tag 0 in indexView: '%#'", [[indexView viewWithTag:0] pageTitle.text]);
"UILabel created programmatically - find it again?" is the closest question I could find, but it doesn't really answer the question of how to access labels in a subview of a btn.
You could try assigning a tag to each of the labels too, something like 100 for the first one and 101 for the second one, then your code would look like this for retrieving the labels
NSLog (#"Accessing label 1 in button with tag 0 in indexView: '%#'", [[indexView viewWithTag:0] viewWithTag:100].text);
NSLog (#"Accessing label 2 in button with tag 0 in indexView: '%#'", [[indexView viewWithTag:0] viewWithTag:101].text);
1
Add this ivar in your header file:
NSMutableArray *buttonArray;
2
Initialize this array in your .m file's init:
buttonArray = [NSMutableArray array];
[buttonArray retain];
3
Change your for loop as follows:
for (int i = 0; i < 9; i++)
{
UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease];
btn.tag = i;
[btn setBackgroundImage:nil forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// LABELS
UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
btnTitle.text = #"Empty";
UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
pageTitle.text = #"x";
[indexView addSubview:btn];
[btn addSubview:btnTitle];
[btnTitle release]; //avoiding leaks
[btn addSubview:pageTitle];
[pageTitle release]; //avoiding leaks
//The changes are here.....
[buttonArray addObject:btn];
[btn release];
}
4
To access a button from your array do (button 5 for example):
UIButton *thisButton = (UIButton *)[buttonArray objectAtIndex:5];
5
Do whatever you want with this button :)
PS:
You're allocating those btn objects inside a for loop (and seems like they aren't ivars).
They will lose scope once you're outside the for loop so you won't be able to access them anyway.
The above method for using an NSMutableArray addresses this problem also as now you've created the btn's and added them to an array which is an ivar so you don't lose scope outside the for loop.
Furthermore, you should probably be subclassing UIButton if you want to have custom subviews on it.
The following for sure works if indexHeight,indexSpacing,indexWidth, are set.
for (int i = 0; i < 9; i++) {
UIButton* btn = [[[UIButton alloc] initWithFrame:CGRectMake(55, i*(indexHeight+indexSpacing), indexWidth, indexHeight)] autorelease];
btn.tag = i;
[btn setBackgroundImage:nil forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
// LABELS
UILabel *btnTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, indexWidth, indexHeight)] autorelease];
btnTitle.text = #"Empty";
btnTitle.tag = i+100;
UILabel *pageTitle = [[[UILabel alloc] initWithFrame:CGRectMake(190, 0, 30, indexHeight)] autorelease];
pageTitle.text = #"x";
pageTitle.tag = i+101;
[indexView addSubview:btn];
[btn addSubview:btnTitle];
[btn addSubview:pageTitle];
}
for (int i = 0; i < 9; i++) {
UILabel *btnTitle = (UILabel *)[[indexView viewWithTag:i] viewWithTag:100+i];
UILabel *pageTitle = (UILabel *)[[indexView viewWithTag:i] viewWithTag:101+i];
NSLog (#"btnTitle.text: '%#'", btnTitle.text);
NSLog (#"pageTitle.text: '%#'", pageTitle.text);
}
Is there an equivalent of Adjust to Fit for UILabel text created dynamically in Xcode?
ie:
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 35, 18)];
someLabel.font = [UIFont boldSystemFontOfSize:14];
someLabel.FOO_SIZETOFIT = YES; //FOO CODE
[someLabel release];
add this line
someLabel.adjustsFontSizeToFitWidth = YES;
I've created a UITextField programmatically with the following code:
self._maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
self._maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
self._maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
self._maxPriceField.font = fieldFont;
self._maxPriceField.delegate = self;
The problem I'm having is that my UITextField ends up having these strange black pixels on the edges. This happens both on the device and in the simulator. You can see in the screenshot below:
When I create the same UITextField using IB, with the same specs and background, I have no problem. Unfortunately I need to create this UITextField programmatically.
Has anybody seen this before? What to do?
It seems this is the way textfields are drawn on the screen. I played around with your code a little bit, and if I set the text field height lower than about 20, you start to see a noticeable "shadow" that is clearly not drawn correctly.
My suggestion is to either use a height of 20 or higher for the text field, or to use a different style, such as Bezel or Line and set the background to white.
Here is a screenshot to demonstrate:
And here is the code I used to draw these:
int labelWidth = 100;
int labelHeight = 10;
_maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, labelWidth, labelHeight)];
_maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
_maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
//_maxPriceField.font = fieldFont;
//_maxPriceField.delegate = self;
[self.view addSubview:_maxPriceField];
UITextField *secondField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, labelWidth, labelHeight + 10)];
secondField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:secondField];
[secondField release];
UITextField *thirdField = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, labelWidth, labelHeight + 20)];
thirdField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:thirdField];
[thirdField release];
UITextField *fourthField = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, labelWidth, labelHeight + 30)];
fourthField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:fourthField];
[fourthField release];
UITextField *noRoundFirst = [[UITextField alloc] initWithFrame:CGRectMake(10, 160, labelWidth, labelHeight)];
noRoundFirst.borderStyle = UITextBorderStyleBezel;
noRoundFirst.backgroundColor = [UIColor whiteColor];
[self.view addSubview:noRoundFirst];
[noRoundFirst release];
Hope this helps.
Mk