I'm working on this for loop and it works perfectly .. I regularly returns the list of found numbers, but I need that for loop give me back the numbers in descending order ... from 30 to 18 and not by the 18-30 as now ... Someone can 'give me a help in this?
I post the code in question
for (NSInteger i = 18; i <= 30; i += 2) {
self.axisY_Label = [[UILabel alloc] initWithFrame:CGRectMake(10, paddingValue, 35, 20)];
self.axisY_Label.text = [NSString stringWithFormat:#"%ld", (long)i];
self.axisY_Label.textColor = [UIColor whiteColor];
self.axisY_Label.font = [UIFont fontWithName:#"HelveticaNeue" size:13];
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(_axisY_Label.frame.size.width, _axisY_Label.center.y, self.frame.size.width, 0.3)];
line.backgroundColor = [UIColor whiteColor];
line.alpha = .2;
[self addSubview:line];
paddingValue += _axisY_Label.frame.size.width;
[self addSubview:_axisY_Label];
}
Exchange from and to parameters and count backwards
for (NSInteger i = 30; i >= 18; i -= 2) { ...
Related
I have a small problem and hope someone can help me solve it ... I searched the net and made some attempts, but I could not solve my problem.
I created a FOR loop that plays a specified number of labels obtained through an array, as you can see from the code that I posted below ...
Now my problem is how to insert constraint programmatically. I need that between a label and the other (horizontal) there is a specific space.
Example:
if I label 4 I would like these would take around the horizontal space is in the iPhone display SE both iPhone 7 Plus ..
So my question is how do I set a specific space between each label and pass in the FOR loop?
CGFloat padding = 0;
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"INFO UNIVERSITARIE",#"INFO GENERALI", #"TROVA SEDI", nil];
for (NSInteger i = 0; i < array.count; i ++) {
NSString *valueText = [array objectAtIndex:i];
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(padding *i, 0, 50, 30);
label.text = [NSString stringWithFormat:#"%#", valueText];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:12];
[label sizeToFit];
[_baseView addSubview:label];
padding = label.frame.size.width;
label.translatesAutoresizingMaskIntoConstraints = NO;
}
Researching this, I remembered that the UIStackView is our new friend, designed specifically for this problem.
NSArray *array = #[ #"INFO UNIVERSITARIE",#"INFO GENERALI", #"TROVA SEDI" ];
NSMutableArray *labels = [NSMutableArray array];
for (NSString *string in array) {
UILabel *label = [[UILabel alloc] init];
label.text = string;
[labels addObject:label];
}
UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:labels];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.spacing = 12.0;
stackView.frame = self.view.bounds;
[self.view addSubview:stackView];
Just learned about this myself, but I tried this code and it works nicely.
Hello to all I created a UIScrollView that shows a series of UIView that represent graifici according to your entry ...
The multiplication of view was performed using a FOR loop
for (NSInteger i = 0; i < value.count; i ++) {
UILabel *valueX_label = [[UILabel alloc] initWithFrame:CGRectMake(xPadding, self.scrollChart.frame.size.height-30, 50, 30)];
NSString *valueText = [value objectAtIndex:i];
valueX_label.text = [NSString stringWithFormat:#"%#", valueText];
valueX_label.textColor = [UIColor whiteColor];
valueX_label.font = [UIFont fontWithName:#"HelveticaNeue" size:9];
valueX_label.textAlignment = NSTextAlignmentCenter;
valueX_label.numberOfLines = 2;
[self.scrollChart addSubview:valueX_label];
xPadding += valueX_label.frame.size.width;
self.scrollChart.contentSize = CGSizeMake(xPadding, self.scrollChart.frame.size.height);
_backgroundBar = [[UIView alloc] initWithFrame:CGRectMake(valueX_label.center.x - 20 , 13, 40, (self.scrollChart.frame.size.height - valueX_label.frame.size.height) -10)];
_backgroundBar.backgroundColor = [UIColor grayColor];
_backgroundBar.layer.cornerRadius = 2;
_backgroundBar.alpha = .1;
[self.scrollChart addSubview:_backgroundBar];}
Now I have a problem ... After entering the paging of ScrollView I have some display problems, ie each size page a few columns as you can see from the pictures ...
I need each page displays only six columns at a time ... you can help me in any way?
Wrong pagination
Ok pagination
I want to use label to create a 15 * 15 matrix world.How to loop the row and column?
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 15, 15)];
myLabel.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:myLabel];
Assuming you need to create 50 Label of size 18x98
int x = 0;
int y = 0;
for (int i = 0; i < 50; i++) {
if (20 * x > self.view.frame.size.width) {
x = 0;
y++;
}
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * x, 100 * y, 18, 98)];
myLabel.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:myLabel];
x++;
}
Name count i get after parsing json is random i.e. it can display any number of values ranging from 1 to 100. I created that many number of labels as shown in the code below, but only the last iterated value is displayed in the label when i pass NSString *name to putLabelsInScrollView method. Can any one help me out to fix this logic to display different name in different created labels? I cannot create tableview which would have been easy and will rectify the cGRects of label and textfields later.
int i = 0;
for(NSDictionary *myJsonDictionary in myJsonArray)
{
//UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++];
//[label setText:myJsonDictionary[#"Name"]];
NSUserDefaults *defaultNames = [NSUserDefaults standardUserDefaults];
NSString *name = myJsonDictionary[#"Name"];
[defaultNames setObject:name forKey:#"QUESTIONNAME"];
NSLog(#"Value is %# \n", name);
i++;
}
NSLog(#"Number of cycles in for-each = %d", i);
[self putLabelsInScrollView:i];
- (void) putLabelsInScrollView:(int)numberOfLables
{
for(int i = 0 ; i < numberOfLables ; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)];
[label setFont:[UIFont fontWithName:#"Helvetica Neue" size:12.0f]];
label.numberOfLines = 2;
NSUserDefaults *defaultNameFetch = [NSUserDefaults standardUserDefaults];
NSString *fetchedString = [defaultNameFetch objectForKey:#"QUESTIONNAME"];
[label setText:[NSString stringWithFormat:#"%#", fetchedString]];
//[label setText:myJsonDictionary[#"Name"]];
[self.scroll addSubview:label];
yPosition_label += 80;
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
text.textColor = [UIColor blackColor];
text.font = [UIFont systemFontOfSize:12.0];
text.backgroundColor = [UIColor clearColor];
text.keyboardType = UIKeyboardTypeDefault;
text.delegate = self;
[self.scroll addSubview:text];
yPosition_text += 100;
yPosition_result = yPosition_label + yPosition_text;
}
[self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)];
[self.view addSubview:self.scroll];
}
Just Try this...
for(NSDictionary *myJsonDictionary in myJsonArray)
{
NSString *name = myJsonDictionary[#"Name"];
[self putLabelsInScrollView:name];
NSLog(#"Value is %# \n", name);
}
[self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)];
[self.view addSubview:self.scroll];
- (void) putLabelsInScrollView:(NSString *)labelText
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)];
[label setFont:[UIFont fontWithName:#"Helvetica Neue" size:12.0f]];
label.numberOfLines = 2;
[label setText:labelText];
//[label setText:myJsonDictionary[#"Name"]];
[self.scroll addSubview:label];
yPosition_label += 80;
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
text.textColor = [UIColor blackColor];
text.font = [UIFont systemFontOfSize:12.0];
text.backgroundColor = [UIColor clearColor];
text.keyboardType = UIKeyboardTypeDefault;
text.delegate = self;
[self.scroll addSubview:text];
yPosition_text += 100;
yPosition_result = yPosition_label + yPosition_text;
}
Replace your code with this:
Fetching results:
int i = 0;
NSMutableArray *texts = [NSMutableArray array];
for(NSDictionary *myJsonDictionary in myJsonArray)
{
//UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++];
//[label setText:myJsonDictionary[#"Name"]];
NSUserDefaults *defaultNames = [NSUserDefaults standardUserDefaults];
NSString *name = myJsonDictionary[#"Name"];
[texts addObject:name];
NSLog(#"Value is %# \n", name);
i++;
}
NSLog(#"Number of cycles in for-each = %d", i);
[self putLabelsInScrollView:i withTexts:texts];
And method for putting labels' texts
- (void) putLabelsInScrollView:(int)numberOfLables withTexts:(NSArray *)texts
{
for(int i = 0 ; i < numberOfLables ; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)];
[label setFont:[UIFont fontWithName:#"Helvetica Neue" size:12.0f]];
label.numberOfLines = 2;
NSUserDefaults *defaultNameFetch = [NSUserDefaults standardUserDefaults];
NSString *fetchedString = texts[i];
[label setText:fetchedString];
//[label setText:myJsonDictionary[#"Name"]];
[self.scroll addSubview:label];
yPosition_label += 80;
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
text.textColor = [UIColor blackColor];
text.font = [UIFont systemFontOfSize:12.0];
text.backgroundColor = [UIColor clearColor];
text.keyboardType = UIKeyboardTypeDefault;
text.delegate = self;
[self.scroll addSubview:text];
yPosition_text += 100;
yPosition_result = yPosition_label + yPosition_text;
}
[self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)];
[self.view addSubview:self.scroll];
}
I have this code in my project that creates UIButtons with labels and images with values coming from a web service.
- (void)getMoviePosterImages
{
for (int i = 0; i < [self.rowCount intValue]; i++)
{
NSArray *postersArray = [self.jsonMutableArray objectAtIndex:i];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[postersArray objectAtIndex:6]]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
[moviePosterButton setBackgroundImage:image forState:UIControlStateNormal];
}
failure:nil];
[operation start];
}
}
Here's the code where I created the UILabels:
- (void)displayMovieTitlesAndFrames
{
int imageCount = [self.rowCount intValue];
int offset_x = 21;
int offset_y = 24;
for (int i = 0; i < imageCount; i++)
{
// compute x & y offset
if (i % 3 == 0)
{
offset_x = 21;
}
else if (i % 3 == 1)
{
offset_x = 115;
}
else
{
offset_x = 210;
}
whiteBackgroundLabel = [[[UILabel alloc] initWithFrame:CGRectMake(offset_x, offset_y, MOVIE_THUMBNAIL_W, MOVIE_THUMBNAIL_H)] autorelease];
whiteBackgroundLabel.backgroundColor = [UIColor whiteColor];
whiteBackgroundLabel.layer.cornerRadius = 3.0;
whiteBackgroundLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
whiteBackgroundLabel.layer.borderWidth = 1.0;
[scrollview addSubview:whiteBackgroundLabel];
moviePosterButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
moviePosterButton.frame = CGRectMake(offset_x + 5, offset_y + 5, MOVIE_THUMBNAIL_W - 10, MOVIE_THUMBNAIL_H - 10);
moviePosterButton.backgroundColor = [UIColor clearColor];
[moviePosterButton setBackgroundImage:[UIImage imageNamed:#"placeholder.png"] forState:UIControlStateNormal];
moviePosterButton.tag = i;
[moviePosterButton addTarget:self
action:#selector(imageButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:moviePosterButton];
movieTitleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(offset_x, offset_y + 143, 90, 20)] autorelease];
movieTitleLabel.backgroundColor = [UIColor whiteColor];
movieTitleLabel.layer.cornerRadius = 3.0;
movieTitleLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
movieTitleLabel.layer.borderWidth = 1.0;
movieTitleLabel.font = [UIFont fontWithName:#"Helvetica" size:11.0];
movieTitleLabel.textColor = [UIColor darkGrayColor];
movieTitleLabel.textAlignment = UITextAlignmentCenter;
movieTitleLabel.numberOfLines = 1;
movieTitleLabel.text = [[self.jsonMutableArray objectAtIndex:i] objectAtIndex:1];
[scrollview addSubview:movieTitleLabel];
quickBuyButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
quickBuyButton.frame = CGRectMake(offset_x + 1 + (MOVIE_THUMBNAIL_W - QUICK_BUY_BUTTON_W - 2), offset_y - 1 + 2, QUICK_BUY_BUTTON_W, QUICK_BUY_BUTTON_H);
quickBuyButton.backgroundColor = [UIColor clearColor];
[quickBuyButton setBackgroundImage:QUICK_BUY_BUTTON forState:UIControlStateNormal];
quickBuyButton.tag = i;
[quickBuyButton addTarget:self
action:#selector(quickBuyButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:quickBuyButton];
// increment offset
if (offset_x == 210)
{
offset_y += 171;
}
}
if ((offset_x == 21) || (offset_x == 115))
{
offset_y += 171;
}
[self adjustScrollViewAndBackground:offset_y];
}
All my UIButtons are created with their respective tags and I want to put their images on each on those buttons. The code above works only on the last UIButton created. I want to populate all of my buttons with images using the code above. Can someone tell me where I'm getting it all wrong?
In your getMoviePosterImages method you are setting an image to the moviePosterButton which might be the last button you just created so this is normal if only the last button gets its image. You need either to retrieve each buttons by their tags or build an array with all you buttons so you can easily access them. You could maybe do something like that:
- (void)getMoviePosterImages
{
for (int i = 0; i < [self.rowCount intValue]; i++)
{
NSArray *postersArray = [self.jsonMutableArray objectAtIndex:i];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[postersArray objectAtIndex:6]]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
[(UIButton *)buttonsArray[i] setBackgroundImage:image forState:UIControlStateNormal];
// or
// [(UIButton *)[self.view viewWithTag:i] setBackgroundImage:image forState:UIControlStateNormal];
}
failure:nil];
[operation start];
}
}
Hope this helps ;)