UIPickerview image with label in component - iphone

I have UIPickerView with two columns and in one column I want to display image and label next to it. It works. Display and image and label, just label is under image how to put image on the left, and label on the right side? I tried to changle label align to right but it doesnt work.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
if(component == kStateComponent)
{
UIImage *img = [UIImage imageNamed:#"rts1.png"];
UIImageView *temp = [[UIImageView alloc] initWithImage:img];
temp.frame = CGRectMake(0, 0, 29, 29);
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
channelLabel.text = #"sdfsdfdsf";
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:temp atIndex:0];
[tmpView insertSubview:channelLabel atIndex:1];
return tmpView;
}
...
}

Set appropriate frame for your channelLabel. To position it to the left of the imageView set correct frame origin x coordinate value (1st parameter in CGRectMake function), e.g:
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 60, 60)];

Related

UITextView subview UIImageView hiding entered text and scrolling with texts iPhone?

I have UITextView in my iPhone app. In the UITextView i have added UIImageView as subview. When the entered text reaches the final height the texts are scrolling to top. Instead the UIImageView (with image) also scrolling top. How can i handle to stop the image scroll and allow the text to scroll? Here is my code for your reference,
messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
messageTextView.delegate = self;
messageTextView.backgroundColor = [UIColor clearColor];
messageTextView.clipsToBounds = YES;
messageTextView.font = [UIFont fontWithName:#"Helvetica" size:14];
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textViewImageView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 30)];
textViewImageView.image = [UIImage imageNamed: #"textbg.png"];
textViewImageView.contentMode = UIViewContentModeScaleToFill;
textViewImageView.contentStretch = CGRectMake(0.5, 0.5, 0, 0);
[messageTextView addSubview: textViewImageView];
[messageTextView sendSubviewToBack: textViewImageView];
[messageTextView addSubview:textViewLabel];
Can anyone please help me to solve this? Thanks in advance. Looking forward your reply.
have you considered to position the imageView behind the textView? Or is there any reason that forbids such a layout?
something like that:
UITextView *messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:messageTextView];
UIImageView *textViewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
textViewImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:textViewImageView];
[self.view sendSubviewToBack:textViewImageView];
Maybe this could help.
Use the scrollview delegate to reposition your image when it reaches the bounds.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
CGRect imageFrame = textViewImageView.frame;
imageFrame.origin.y = scrollView.contentOffset.y - (scrollView.contentSize.height - scrollView.frame.size.height);
textViewImageView.frame = imageFrame;
}
}
This should work:
[yourTxtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"your image.png"]]];

iOS - how to put UIImage and UILabel as a footer on the same line?

I need to have a footer in my view that says "Powered by: {image_logo_of_my_company}" How do I achieve that?
Make a wrapper view of type UIView and add a UILabel and the logo in a UIImageView inside that view at the appropriate coordinates.
UIView *wrapper = [[UIView alloc] initWithFrame:footerFrame];
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(0, 0, footerFrame.size.width*0.6, footerFrame.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(footerFrame.size.width*0.6, 0,
footerFrame.size.width*(1.0-0.6), footerFrame.size.height)];
[wrapper addSubView:label];
[wrapper addSubView:imageView];
label.text = #"Powered by: ";
imageView.image = [UIImage imageNamed:#"logo"]; // assumes logo.png & logo#2x.png
[viewNeedingFooter addSubView:wrapper];
Assuming you need 60% of the width of the footer for the label and 40% for the logo.

customize section header uilabel background and text

I am currently creating my own custom section headers but I have never dont any text editing via code.. and my new method that I am using to populate my custom header is doing some weird things as shown below
I would like to change the text to white and be slightly bolder and also make the white background transparent..
this is the code I am using to do this
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor grayColor]];
// Add the label
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.5, 20, 20)];
// do whatever headerLabel configuration you want here
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
[headerView addSubview:headerLabel];
// Return the headerView
return headerView;
}
I have tried this
[headerLabel.backgroundColor:[UIColor clearColor]];
etc but its not working :(
I would like to change the text to white...
UILabel's textColor property is your friend here.
And slightly bolder...
No problem! headerLabel.font = [UIFont boldSystemFontOfSize:mySize];
And make a white transparent background.
Whoa, whoa, that is the worst setter syntax ive ever seen!!! My lord, myLabel.backgroundColor is a getter, change:
[headerLabel.backgroundColor:[UIColor clearColor]];
to:
[headerLabel setBackgroundColor:[UIColor clearColor]];
Lucky for you, using your syntax would have just sent a message to nil, which is defaulting the background color of your label to white.
Use following code...
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headername = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 270, 34)];
headername.backgroundColor = [UIColor clearColor];
headername.textColor = [UIColor blackColor];
if(section == 0)
{
headername.text = #"Name that u wish";
}
else
{
headername.text = #"Name that u wish";
}
UIView *headerView = [[UIView alloc] init];
UIImageView *tempimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 300,34)];
tempimage.image = [UIImage imageNamed:#"whitebackground.png"];
[headerView addSubview:tempimage];
[headerView addSubview:headername];
return headerView;
}
Hope, this will help you...chill
all i have done to get the customized header in table is as following and it is working fine for me
UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 60)]
autorelease];
UILabel *headerLabel =
[[[UILabel alloc]
initWithFrame:CGRectMake(10, 20, 300, 40)]
autorelease];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
self.tableView.tableHeaderView = containerView;
i just put it in viewDidLoad: method

How to set Background image in UITableView cell?

I have Created custom cell.And i have table view with Grouped style.Now i want to put Backgroung image to each cell of different section.How to set the background image of custom cell.
Here is the code for creating the custom cell.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 90)];
// view.backgroundColor=[UIColor darkGrayColor];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
imageV.image = [UIImage imageNamed:img];
[view addSubview:imageV];
[imageV release];
UILabel *lblAchievementName = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 168, 21)];
lblAchievementName.text = [play.listData objectAtIndex:indexPath.row];
lblAchievementName.backgroundColor=[UIColor clearColor];
lblAchievementName.textColor=[UIColor orangeColor];
[lblAchievementName setFont:[UIFont fontWithName:#"Arial Black" size:16.5]];
[view addSubview:lblAchievementName];
[lblAchievementName release]
[cell.contentView addSubview:view];
return cell;
Now how to set the backgroung image for this custom view?
You can try this code that will help you.
Put this code in cellForRowAtIndexPath method
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"categorytab1.png"];
cell.backgroundView = av;
set thru setBackgroundView method.Form the ImageView with ur need Image aand set that as,
[cell setBackgroundView:urBgImgView];
You can use following code for different image in different section.
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
if (indexPath.section == 0) {
imageV.image = [UIImage imageNamed:img0];
}
else if (indexPath.section == 1) {
imageV.image = [UIImage imageNamed:img1];
}
else if (indexPath.section == 2) {
imageV.image = [UIImage imageNamed:img2];
}
[view addSubview:imageV];
[imageV release];
i'd rather to add a CALayer or UIImageView to be the backgroud view. Because i used to set the BackgroundColor, but it never workd for me. so i changed my mind by this way.

An iphone uipickerview that rotates horizontally?

I've only seen it in a VERY few iPhone apps... but it looks like a picker that rotates left/right (instead of top/bottom).
They usually put it on 1 line of a tableView... to allow the user to quickly pick between a small number of choices (like 3-10).
How is that coded?
Continuing the answer by Dave DeLong I got it working like this......
In viewDidLoad i did this...
CGRect frame = horizontalPickerView.frame;
frame.size.width = 50;
frame.size.height = 216;
frame.origin.x=90;
frame.origin.y = 200;
horizontalPickerView.frame = frame;
horizontalPickerView.transform = CGAffineTransformMakeRotation(3.14159/2);
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)] autorelease];
lbl.transform = CGAffineTransformMakeRotation(-3.14159/2);
lbl.text = #"hi";
return lbl;
}
Hope this helps
Here you will find source code for Picker which is horizontally aligned.
You can do this by taking a regular UIPickerView, adjusting its width (via setFrame:), and then applying an NSAffineTransform to rotate it 90º. You'll then need to rotate each item in the picker 90º the other way.
It's a little tedious to do it properly, but it can be done.
#Madhup's code lead me in the general direction I wanted when I searched for the horizontal UIPickerView but I then realized the question asked wasn't really addressed so for anyone who was looking for a more suitable answer to the left-to-right rotation. The code in the answers I'd read were all to enable left-to-right swiping, causing the picker to push the labels/rows with higher values to the left of the view. Any ways here's my contribution:
In the viewDidLoad method:
yourPickerView.frame = frame;
yourPickerView.transform = CGAffineTransformMakeRotation(4.71238898); //Instead of rotating clockwise 90° we're rotating 90° counterclockwise. 4.71238898 being ≈270° in radians.
[self.view addSubview:self.picker];
self.yourPickerView.delegate = self;
self.yourPickerView.dataSource = self;
self.yourPickerView.showsSelectionIndicator = YES;
self.yourPickerView.userInteractionEnabled = YES;
The pickerView's method:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)] autorelease];
yourLabel.transform = CGAffineTransformMakeRotation(1.57079633); //Instead of rotating counterclockwise 90° we're rotating 90° clockwise 1.57079633 being ≈90° in radians.
yourLabel.font = [UIFont fontWithName:#"System-Bold" size:18]; //Your font here.
yourLabel.text = #"yourLabel's text"; //or like me [NSString stringWithFormat:#"%#, [yourArray objectAtIndex:row]]
yourLabel.backgroundColor = [UIColor clearColor];
return label;
}
Try a paged scrollview that has the items you want on one per page, and perhaps overlay an image above it if you want nicer graphics for your control, and only allow for horizontal scrolling (don't make the contentSize of the scrollview taller than the size of the actual view, and disable vertical scroll bouncing on the control).
You will have to create picker programitcally, so that you can create your own sized picker with CGRectMake(x, y, width, height) then, you will have to rotate it, but rotating it will also rotate in the Picker's dataSources methods, you will have to rotate the view inverse of picker's rotation, I am including code hopfully it will help
.....
...
...
NSArray *arr = [NSArray arrayWithObjects:#"1 mi", #"2 mi", #"5 mi", #"10 mi", #"15 mi", #"20 mi", #"25 mi",
#"30 mi", #"35 mi", #"40 mi", #"45 mi", #"50 mi", #"75 mi", #"99 mi", nil];
radiusDefaults = [[NSMutableArray alloc] initWithArray:arr] ;
radiusPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 100, 150)];
radiusPicker.delegate = self;
radiusPicker.dataSource = self;
radiusPicker.showsSelectionIndicator = NO;
//Resize the picker, rotate it so that it is horizontal and set its position
CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57);
rotate = CGAffineTransformScale(rotate, .1, .5);
CGAffineTransform t0 = CGAffineTransformMakeTranslation(-61, 0);
radiusPicker.transform = CGAffineTransformConcat(rotate,t0);
// [theNavigationBar.topItem setTitleView:radiusPicker] ;
UIView *pickerWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 215)];
[self.view addSubview:radiusPicker];
[radiusPicker selectRow:6 inComponent:0 animated:NO];
[radiusPicker release];
.....
.......
....
#pragma mark -
#pragma mark UIPickerView
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView *viewForRow = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 400)] autorelease];
UILabel *label;
UIFont *font = [ UIFont fontWithName:#"ArialRoundedMTBold" size:22];
label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 70, 350)] autorelease];
[label setText:[NSString stringWithFormat:#"%#", [radiusDefaults objectAtIndex:row]]];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blueColor];
label.font = font;
label.backgroundColor = [UIColor clearColor];
// label.opaque = NO;
[viewForRow addSubview:label];
CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57);
rotate = CGAffineTransformScale(rotate, 1, 6.5);
[viewForRow setTransform:rotate];
return viewForRow;
}
Try this->
Create Plain UIVew of size of UIPickerview, add picker on it.
set numberOfComponentsInPickerView = 1.
set componant width.
Then add small sub views on it to Hide rest of picker. Only rotating wheel of componant should visible.
Transform plain view to rotate it through 90 degree.
Make sure to apply tranform in:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lbl = nil;
if(view)
{
lbl = (UILabel *) [view viewWithTag:11];
}
else
{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 0, 30, 30)];
lbl.tag = 11;
lbl.backgroundColor = [UIColor clearColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
lbl.transform = CGAffineTransformRotate(lbl.transform, M_PI +M_PI/2);
[view addSubview:lbl];
[lbl release];
}
lbl.text = [dataSourceArray objectAtIndex:row];
return view;
}
Now you can add palin view as a subview for horizontal picker on any view.
Have you ever considered taking a table view and rotating it?
Didn't think so. Go try that. :)