Basically i have a uicollection view with uiDynamics, and i feed in text to every cell. When i run, everything looks great ! until i scroll then the text looks deformed !
This is how i insert the texts
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 20)];
// title.backgroundColor = [UIColor blueColor];
title.tag = 1;
[title setText:#"Title"];
title.font = [UIFont fontWithName:#"Corbert-Regular" size:13];
[cell.contentView addSubview:title];
UILabel *description = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, cell.bounds.size.width-100, 40)];
description.tag = 2;
[description setText:#"description"];
description.font = [UIFont fontWithName:#"Corbert-Regular" size:16];
[cell.contentView addSubview:description];
After scroll:
Related
My goal is to use title and subtitle with different font sizes in navigation controller page title (title should be bigger, subtitle should be lower accordingly).
I found example of code to implement this. The only one problem is that font size is not applied - both title and subtitle have the same font size. Like the code for font size doesn't work.
How to fix that? Thank you
// prepare title label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:#"HelveticaNeueLight" size:19.0];
titleLabel.text = locationInfo;
[titleLabel sizeToFit];
// prepare subtitle label
UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 18, 0, 0)];
subtitleLabel.backgroundColor = [UIColor clearColor];
subtitleLabel.textColor = [UIColor whiteColor];
subtitleLabel.font = [UIFont fontWithName:#"HelveticaNeueLight" size:12.0];
subtitleLabel.text = dateInfo;
[subtitleLabel sizeToFit];
UIView *twoLineTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAX(subtitleLabel.frame.size.width, titleLabel.frame.size.width), 30)];
[twoLineTitleView addSubview:titleLabel];
[twoLineTitleView addSubview:subtitleLabel];
float widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width;
if (widthDiff > 0) {
CGRect frame = titleLabel.frame;
frame.origin.x = widthDiff / 2;
titleLabel.frame = CGRectIntegral(frame);
} else{
CGRect frame = subtitleLabel.frame;
frame.origin.x = fabs(widthDiff) / 2;
subtitleLabel.frame = CGRectIntegral(frame);
}
self.navigationItem.titleView = twoLineTitleView;
It's with the setFont method, not .font
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:12.0]];
And also you have an error in the font name:
it's HelveticaNeue-Light
Use SetFont instead of Font::
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-UltraLight" size:14.0]];
I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.
What i want is in following images
in above image there is one backbar button and one imageview as shown with arrow-mark. Other then that all white box is for difrent UILabels to show in navigation bar.
There is only one UIImageView in the navigation bar and one left bar button. Now problem is that i don't know how to add multiple UILabel in navigation bar at a specific position.
Till now what i have worked is to add only button and UIImageView with the right bar button array or left bar button array, but that only add items in sequnce.
So can anyone please guide me that how can anyone add UILabels or any other item at a specific position..
you can add Multiple UIlabel or Image as look like bellow image:-
this can do using bellow code you can change Label and imageView frame and put as your requirement
- (void)viewDidLoad
{
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = #"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:#"a59117c2eb511d911cbf62cf97a34d56.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
}
You can add subviews directly to the navigationBar -
UINavigationBar *navBar = navController.navigationBar;
[navBar addSubview: yourLabel];
yourLabel frame origin will be relative to the navigation bar
Try this code
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setFrame: CGRectMake(160, 7 , 40, 30)];
[button0 setTitle:#"Sort" forState:UIControlStateNormal];
button0.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
[button0 setBackgroundImage:[UIImage imageNamed:#"Btn_Sort_Btn_Sort_Places.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;
UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
lbl_title.text = str_HeaderTitle;
lbl_title.textColor = [UIColor whiteColor];
lbl_title.font = [UIFont fontWithName:#"Helvetica-Bold" size:16];
lbl_title.backgroundColor = [UIColor clearColor];
lbl_title.textAlignment = NSTextAlignmentCenter;
[buttonContainer addSubview:lbl_title];
You can set an arbitrary view instead of a view controller's title:
myViewController.navigationItem.titleView = someView;
Note that most likely the view's frame will be restricted to make room for leftBarButtonItem and rightBarButtonItem.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 2, 20, 15)];
[label setBackgroundColor:[UIColor greenColor]];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 150, 10)];
[imgView setBackgroundColor:[UIColor redColor]];
[[self.navigationController navigationBar] addSubview:label];
[self.navigationController.navigationBar addSubview:imgView];
Have a look at this piece of code. With some modifications it should solve your problem:
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(15, 0, 57, 44)];
[imageButton setBackgroundImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 250, 44)];
[titleLabel setText:#"some title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[self.navigationController.navigationBar addSubview:imageButton];
[self.navigationController.navigationBar addSubview:titleLabel];
Just modify the values in the CGRectMake() to fit your needs.
Hello in Xcode I am trying to add two labels to my image programmatically but when I add the second one it will overwrite my first one. Here is my code:
//first label
UILabel* caption = [[UILabel alloc] initWithFrame:
CGRectMake(-25, 0, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentLeft;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" #%#",[data objectForKey:#"username"]];
[self addSubview: caption];
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:
CGRectMake(-25, -200, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentCenter;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" Testing"];
[self addSubview: bottomBox];
I've tried changing the "0 to -200 to modify the Y coordinates so that the second label will shift down but it just overwrites the first one for some reason. Any help would be greatly appreciated thanks.
All the code for your 2nd label is referencing the 1st variable caption instead of bottomBox. You want:
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 320, 50)];
bottomBox.backgroundColor = [UIColor whiteColor];
bottomBox.textColor = [UIColor blackColor];
bottomBox.textAlignment = UITextAlignmentCenter;
bottomBox.font = [UIFont systemFontOfSize:16];
bottomBox.text = #"Testing";
[self addSubview: bottomBox];
I have a UITableView with a header. the problem I currently have is that the header doesn't scroll with the table. I need it to scroll off screen (above) when the user scrolls the table view up. the tableview scrolls but the header is locked at the top of the UIView.
thanks
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.backgroundColor = [UIColor whiteColor];
// add user profile image to _contentView
UIImageView *userImageView;
UIImage *userImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userProfileImageUrl]]];
userImageView=[[UIImageView alloc]initWithImage:userImage];
userImageView.frame=CGRectMake(10,10,90,100);
[sectionHeader addSubview:userImageView];
// return userImageView;
// user name lable
CGRect userNameFrame = CGRectMake(110, 60, 100, 50 );
UILabel* userNameLabel = [[UILabel alloc] initWithFrame: userNameFrame];
[userNameLabel setText: firstName];
[userNameLabel setTextColor: [UIColor blackColor]];
[userNameLabel setBackgroundColor:[UIColor clearColor]];
[userNameLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:14]];
[sectionHeader addSubview:userNameLabel];
// user last name label
CGRect userLastNameFrame = CGRectMake(110, 75, 100, 50 );
UILabel* userLastNameLabel = [[UILabel alloc] initWithFrame: userLastNameFrame];
[userLastNameLabel setText: lastName];
[userLastNameLabel setTextColor: [UIColor blackColor]];
[userLastNameLabel setBackgroundColor:[UIColor clearColor]];
[userLastNameLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:14]];
[sectionHeader addSubview:userLastNameLabel];
// user checkin view
UIView *userCheckinView = [[UIView alloc] initWithFrame:CGRectMake(10, 120, 280, 25)];
userCheckinView.backgroundColor = customColorGrey;
[sectionHeader addSubview:userCheckinView];
// check in label
UILabel* userCheckInLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 100, 20)];
[userCheckInLabel setText: #"CHECK-IN"];
userCheckInLabel.backgroundColor = customColorGrey;
userCheckInLabel.textColor = customColorIt;
[userCheckInLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userCheckinView addSubview:userCheckInLabel];
// image
UIImageView *checkinImg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"classifica_geotag_C.png"]];
checkinImg.frame = CGRectMake(5, 0, 24, 24);
[userCheckinView addSubview:checkinImg];
// check in label
UILabel* userCheckInCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 20, 20)];
[userCheckInCountLabel setText: [checkinCount stringValue]];
userCheckInCountLabel.backgroundColor = customColorGrey;
userCheckInCountLabel.textColor = customColorIt;
[userCheckInCountLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userCheckinView addSubview:userCheckInCountLabel];
// user like view
UIView *userLikeView = [[UIView alloc] initWithFrame:CGRectMake(10, 150, 280, 25)];
userLikeView.backgroundColor = customColorGrey;
[sectionHeader addSubview:userLikeView];
// like label
UILabel* userLikeLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 100, 20)];
[userLikeLabel setText: #"LIKE"];
userLikeLabel.backgroundColor = customColorGrey;
userLikeLabel.textColor = customColorIt;
[userLikeLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userLikeView addSubview:userLikeLabel];
// image
UIImageView *likeImg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"classifica_like_C.png"]];
likeImg.frame = CGRectMake(5, 0, 24, 24);
[userLikeView addSubview:likeImg];
// user like label
UILabel* userLikeCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 20, 20)];
[userLikeCountLabel setText: [likesCount stringValue]];
userLikeCountLabel.backgroundColor = customColorGrey;
userLikeCountLabel.textColor = customColorIt;
[userLikeCountLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userLikeView addSubview:userLikeCountLabel];
// la mia bacheca like view
userLaMiaView = [[UIView alloc] initWithFrame:CGRectMake(10, 180, 300, 25)];
userLaMiaView.backgroundColor = [UIColor clearColor];
[sectionHeader addSubview:userLaMiaView];
// like label
UILabel* userLaMiaLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 150, 20)];
[userLaMiaLabel setText:NSLocalizedString(#"LA MIA BACHECA", nil)];
userLaMiaLabel.backgroundColor = [UIColor clearColor];
userLaMiaLabel.textColor = customColorGrey;
[userLaMiaLabel setFont:[UIFont fontWithName:#"DIN-Bold" size:10]];
[userLaMiaView addSubview:userLaMiaLabel];
// grey line view below la mia label
userGreyLineView = [[UIView alloc] initWithFrame:CGRectMake(10, 248, 280, 1.5)];
userGreyLineView.backgroundColor = [UIColor whiteColor];
[sectionHeader addSubview:userGreyLineView];
return sectionHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 210;
}
That behavior is only common when the UITableViewStyle property of the table is set to UITableViewStylePlain. If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells.
This answer is taken from this question.
This solution works regardless of the number of headers.
create sectionHeader view in a new method and then add to the end:
self.tableView.tableHeaderView = sectionHeader;
if you have dynamic header view or single view select table style
Changing the UITableViewStyle from Plain to Grouped make the section header to scroll with the other cells.
If you write tableView.tableHeaderView = sectionHeader you will lose the actual table view header.
If you want to have table view header along with section headers you have to set UITableViewStyle property to UITableViewStyleGrouped.
Also if you want to calculate section header height automatically you can return UITableViewAutomaticDimension in heightForHeaderInSection method like this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return UITableViewAutomaticDimension;
}
If you have a single header in the table then you can use tableHeaderView as below:
tableView.tableHeaderView = Header;
Or if you have multiple header in table than you need to use Group table instead of plain table.
Thanks
Sometimes I want my view to contain 5 UILabels, sometimes 3 and sometimes n.
The number of UILabels depends on data that's fetched from a website.
You'll have to make them in code instead of interface builder
for (int i = 0; i < n; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = #"text"; //etc...
[self.view addSubview:label];
[label release];
}
A generic answer for a generic question:
while (labelsToDisplay)
{
UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
[label setText:#"someText"];
[aViewContainer addSubview:label];
[label release];
}
NSArray *dataArray;
float xCoordinate=10.0,yCoordinate=10.0,width=100,height=40;
float ver_space=20.0;
for (int i = 0; i <dataArray.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(xCoordinate,yCoordinate,width,height)];
label.text = [dataArray objectAtIndex:i];
[self.view addSubview:label];
yCoordinate=yCoordinate+height+ver_space;
}
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(125, 12,170,20)];
lbl.text=#"IOS";
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:#"AlNile" size:10.0];
lbl.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
lbl.layer.borderColor=[UIColor blackColor].CGColor;
lbl.layer.borderWidth=1.0f;
lbl.layer.cornerRadius = 6.0f;
[self.view addSubview:lbl];
UILabel *lblTitle=[[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
[lblTitle setText:#"MAK"];
[lblTitle setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lblTitle];
-Here UILable will be created dynamically.
-but property will be set differently.
Create a TextView to show the text on the labels and a NSArray to contain the data.
For more information:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html
http://developer.apple.com/library/ios/documentation/uikit/reference/UITextView_Class/Reference/UITextView.html