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 #"%#".
Related
I am new to iPhone,
How do I vertically align my text in the UILabel?
Here is my Code snippet,
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,100);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
[scrollVw addSubview:lbl];
text displayed is in the format of 123456 but i want text should be display vertically like,
6
5
4
3
2
1
Any help will be appriciated.
Its impossible to align the text in UILabel vertically. But, you can dynamically change the height of the label using sizeWithFont: method of NSString, and just set its x and y as you want.
As an alternative you can use UITextField. It supports the contentVerticalAlignment peoperty as it is a subclass of UIControl. You have to set its userInteractionEnabled to NO to prevent user from typing text on it.
EDIT 1
Well instead of a UILabel , Make a UITableView like :-
TableActivityLevel=[[UITableView alloc] initWithFrame:CGRectMake(224, 203, 27, 0) style:UITableViewStylePlain];
TableActivityLevel.delegate=self;
TableActivityLevel.dataSource=self;
TableActivityLevel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
TableActivityLevel.rowHeight = 17;
TableActivityLevel.backgroundColor=[UIColor clearColor];
TableActivityLevel.layer.borderColor = [[UIColor clearColor]CGColor];
TableActivityLevel.separatorStyle = UITableViewCellSeparatorStyleNone;
EDIT 2 Found the method using UILabels too !! :) Check this out....
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 10.0, 100.0)];
[label1 setBackgroundColor:[UIColor clearColor]];
[label1 setNumberOfLines:0];
[label1 setCenter:self.view.center];
[label1 setFont:[UIFont fontWithName:#"Helvetica" size:12.0]];
[label1 setTextColor:[UIColor whiteColor]];
[label1 setTextAlignment:UITextAlignmentCenter];
[label1 setText:#"1 2 3 4 5 6"];
[self.view addSubview:label1];
If it just a single lined text as in your example you can use various workarounds. I personally would create a UILabel subclass as follows,
#implementation VerticalLabel
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.numberOfLines = 0;
}
return self;
}
- (void)setText:(NSString *)text {
NSMutableString *newString = [NSMutableString string];
for (int i = text.length-1; i >= 0; i--) {
[newString appendFormat:#"%c\n", [text characterAtIndex:i]];
}
super.text = newString;
}
#end
You now just have to replace
UILabel *lbl = [[UILabel alloc] init];
with
UILabel *lbl = [[VerticalLabel alloc] init];
to get vertical text.
Hi the following code is work well
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,300);
lbl.backgroundColor=[UIColor clearColor];
lbl.numberOfLines=6; // Use one to 6 numbers
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
NSString *reverse=lbl.text;
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [reverse length];
while (charIndex > 0) {
charIndex--;
NSRange subStrRange = NSMakeRange(charIndex, 1);
[reversedString appendString:[reverse substringWithRange:subStrRange]];
}
NSLog(#"%#", reversedString);
CGSize labelSize = [lbl.text sizeWithFont:lbl.font
constrainedToSize:lbl.frame.size
lineBreakMode:lbl.lineBreakMode];
lbl.frame = CGRectMake(
lbl.frame.origin.x, lbl.frame.origin.y,
lbl.frame.size.width, labelSize.height);
lbl.text=reversedString;
[scrollview addSubview:lbl];
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,100);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
lbl.numberOfLines = 10;
[self.view addSubview:lbl];
You cant do vertical allignment but use another way to display it
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,400);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"1\n2\n3\n4\n5\n6";
[scrollVw addSubview:lbl];
So I have setup my UILabel, but when I set the text in my game loop to a string that is the score (so the Text is reset every loop) my app crashes.
This is the error I get:
0x15560b0: cmpl (%eax), %ecx Thread 1
The Breakpoint says this:
EXC_BAD_ACCESS(code=1, address=0x67b30064
Here is how I am setting up my UILabel (in my init method) :
//SET UP THE SCORE LABEL HERE
scoreLabel = [[UILabel alloc] init];
scoreString = [NSString stringWithFormat:#"%d", score];
[scoreLabel setFont: [UIFont fontWithName: #"TimesNewRoman" size: 10.0f]];
[scoreLabel setFrame: CGRectMake(262, 250, 100, 40)];
[scoreLabel setBackgroundColor:[UIColor clearColor]];
[scoreLabel setTextColor: [UIColor clearColor]];
[scoreLabel setTextColor: [UIColor whiteColor]];
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview: scoreLabel];
//[scoreLabel setText: scoreString];
Thank You!
Are you doing your label update off the main queue? I had these crashes until I dispatched the update on the main queue.
dispatch_async(dispatch_get_main_queue(), ^{
self.lblSyncProgress.text = #"Some string";
});
//in your .h file
UILabel *scoreLabel;
NSString *scoreString;
//in your .m file
//SET UP THE SCORE LABEL HERE
scoreLabel = [[UILabel alloc] init];
[scoreLabel setFont: [UIFont fontWithName: #"TimesNewRoman" size: 10.0f]];
[scoreLabel setFrame: CGRectMake(262, 250, 100, 40)];
[scoreLabel setBackgroundColor:[UIColor clearColor]];
[scoreLabel setTextColor: [UIColor clearColor]];
[scoreLabel setTextColor: [UIColor whiteColor]];
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview: scoreLabel];
//In your update method
scoreString = [NSString stringWithFormat:#"%d", score];
scoreLabel.text = scoreString;
I noticed in your code that you had
scoreString = [NSString stringWithFormat:#"%d", score];
Make sure that "score" is a char. If it is not have a look at this
I am new to this iphone development .I am now stuck with a problem .In my application i require coverflow in which multiple labels should be shown.So for that i added an image as a sub view for view and then added the labels as a sub view for the image view.Every thing works fine for the first 7 elements But when i scroll it is showing the same elements again
I am using
https://github.com/nicklockwood/iCarousel
this to attain coverflow
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIImageView *imageView =nil;
UILabel *yourLabel=nil;
UILabel *yourLabel1=nil;
UILabel *yourLabel2=nil;
cardAvailableDataObject *singleCard=[self.cards objectAtIndex:index];
if (view == nil)
{
//set up reflection view
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0,0,200,self.view.frame.size.width/1.42)] autorelease];
imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(0,0,200,self.view.frame.size.width/1.42)];
[view addSubview:imageView];
yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -5, 200, 50)];
yourLabel.textAlignment = UITextAlignmentCenter;
[yourLabel setTextColor:[UIColor colorWithRed:(38/255.f) green:(171/255.f) blue:(226/255.f) alpha:1.0f]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: #"Trebuchet MS" size: 17.0f]];
[imageView addSubview:yourLabel];
yourLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(90, 40, 300, 20)];
[yourLabel1 setTextColor:[UIColor blackColor]];
[yourLabel1 setBackgroundColor:[UIColor clearColor]];
[yourLabel1 setFont:[UIFont fontWithName: #"Trebuchet MS" size: 12.0f]];
[imageView addSubview:yourLabel1];
yourLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(90, 61, 300, 20)];
[yourLabel2 setTextColor:[UIColor blackColor]];
[yourLabel2 setBackgroundColor:[UIColor clearColor]];
[yourLabel2 setFont:[UIFont fontWithName: #"Trebuchet MS" size: 12.0f]];
[imageView addSubview:yourLabel2];
}
else
{
imageView = [[view subviews] lastObject];
yourLabel = [[imageView subviews] lastObject];
yourLabel1 = [[imageView subviews] lastObject];
yourLabel2= [[imageView subviews] lastObject];
}
yourLabel.text=singleCard.name;
yourLabel2.text=singleCard.companyname;
yourLabel1.text=singleCard.designation;
imageView.image=[profile objectAtIndex:index];
return view;
}
This is my code .Can any one suggest how to solve this
I have made a UIScrollView which holds four UILabels. I would like that the selected UILabel is not the one in the left of the screen but in the center. I have attached two images to illustrate what I mean. On the first image, the green label would be the selected one and on the next image the blue label would be the selected one.
Is there any way to do this?
This is my code:
[scrollView setContentSize:CGSizeMake(428, 65)];
[scrollView setDelegate:self];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 107, 65)];
[label1 setBackgroundColor:[UIColor redColor]];
[label1 setTextAlignment:UITextAlignmentCenter];
[label1 setText:#"Wednesday"];
[scrollView addSubview:label1];
[label1 release];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(107, 0, 107, 65)];
[label2 setBackgroundColor:[UIColor greenColor]];
[label2 setTextAlignment:UITextAlignmentCenter];
[label2 setText:#"Thursday"];
[scrollView addSubview:label2];
[label2 release];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(214, 0, 107, 65)];
[label3 setBackgroundColor:[UIColor blueColor]];
[label3 setTextAlignment:UITextAlignmentCenter];
[label3 setText:#"Friday"];
[scrollView addSubview:label3];
[label3 release];
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(321, 0, 107, 65)];
[label4 setBackgroundColor:[UIColor orangeColor]];
[label4 setTextAlignment:UITextAlignmentCenter];
[label4 setText:#"Saturday"];
[scrollView addSubview:label4];
[label4 release];
It seems to be the easiest to rotate the UIPickerView. This can be done like this.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
arrayDays = [[NSMutableArray alloc] init];
[arrayDays addObject:#"ONSDAG"];
[arrayDays addObject:#"TORSDAG"];
[arrayDays addObject:#"FREDAG"];
[arrayDays addObject:#"LØRDAG"];
arrayDates = [[NSMutableArray alloc] init];
[arrayDates addObject:#"29. JUNI"];
[arrayDates addObject:#"30. JUNI"];
[arrayDates addObject:#"1. JULI"];
[arrayDates addObject:#"2. JULI"];
pickerViewDay = [[UIPickerView alloc] initWithFrame:CGRectZero];
[pickerViewDay setDelegate:self];
[pickerViewDay setShowsSelectionIndicator:NO];
CGAffineTransform rotate = CGAffineTransformMakeRotation(-M_PI/2);
rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
[pickerViewDay setTransform:rotate];
[pickerViewDay setCenter:CGPointMake(self.view.frame.size.width/2, (pickerViewDay.frame.size.height/2)-3)];
[self.view addSubview:pickerViewDay];
// Adding selection indicator to pickerview
UIImage *selectorImage = [UIImage imageNamed:#"DayPickerView_SelectionIndicator.png"];
UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
[customSelector setFrame:CGRectMake(0, 0, 120, 74)];
[customSelector setCenter:CGPointMake(self.view.frame.size.width/2, customSelector.frame.size.height/2)];
[self.view addSubview:customSelector];
[customSelector release];
// Adding background to pickerview
UIImage *backgroundImage = [UIImage imageNamed:#"DayPickerView_Background.png"];
UIView *custombackground = [[UIImageView alloc] initWithImage:backgroundImage];
[custombackground setFrame:CGRectMake(0, 0, 320, 74)];
// [self.view addSubview:custombackground];
[custombackground release];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIView *viewRow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];
CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
// Date
CGRect rectDate = CGRectMake(30, 0, 150, 80);
UILabel *date = [[UILabel alloc]initWithFrame:rectDate];
[date setTransform:rotate];
[date setText:[arrayDates objectAtIndex:row]];
[date setFont:[UIFont fontWithName:#"Arial-BoldMT" size:37.0]];
[date setShadowColor:[UIColor whiteColor]];
[date setShadowOffset:CGSizeMake(0, -1)];
[date setTextAlignment:UITextAlignmentCenter];
[date setBackgroundColor:[UIColor clearColor]];
[date setClipsToBounds:YES];
[viewRow addSubview:date];
// Day
CGRect rectDay = CGRectMake(-30, 0, 150, 80);
UILabel *day = [[UILabel alloc]initWithFrame:rectDay];
[day setTransform:rotate];
[day setText:[arrayDays objectAtIndex:row]];
[day setFont:[UIFont fontWithName:#"Arial-BoldMT" size:21.0]];
[day setTextColor:[UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1]];
[day setTextAlignment:UITextAlignmentCenter];
[day setBackgroundColor:[UIColor clearColor]];
[day setClipsToBounds:YES];
[viewRow addSubview:day];
return viewRow;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayDays objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayDays count];
}
Have a problem where my Label is not appearing. But the slider does. Its one of these bizarre cases that seems there is no logical reason. Anybody able to make sense of this?
else if (indexPath.row == 4)
{
Filters *filters = [Filters sharedInstance];
CGRect frame = CGRectMake(100, 10, 80, 23);
costSlider = [[[UISlider alloc] initWithFrame:frame] autorelease];
costSlider.maximumValue = [[NSNumber numberWithDouble:
filters.maxCarRentalCost] floatValue];
costSlider.minimumValue = [[NSNumber numberWithDouble:
filters.minCarRentalCost] floatValue];
[costSlider addTarget:self action:#selector(costSliderChanged:)
forControlEvents:UIControlEventValueChanged];
[costSlider setValue:[filters.maxCarCostPerDay floatValue] animated:YES ];
costSlider.continuous = NO;
costSlider.enabled = YES;
costSlider.value = [filters.maxCarCostPerDay floatValue];
costSlider.tag = 5;
[cell addSubview:costSlider];
self.costLabel.textColor = [UIColor greenColor];
self.costLabel.text = #"what"; //[NSString stringWithFormat:#"Cost per day:
%d", filters.maxCarCostPerDay ];
[self.costLabel setBackgroundColor:[UIColor blueColor]];
self.costLabel.frame = CGRectMake(10, 10, 75, 20);
[self.costLabel setFont:[UIFont boldSystemFontOfSize:10.0]];
[cell addSubview:self.costLabel];
}
Thanks
-Code
From the code you posted it does not look like you have alloc'd the label. Unless you are doing that elsewhere in the code, that is probably your issue