Center vertically UILabel text in table view section header - iphone

How to change position of the text in tableview section? I use viewForHeaderInSection:Section and i create there somwthing like this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
label.backgroundColor = [UIColor blackColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Gotham-Bold" size:17.0f];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.comlpetedFetchedResultsController sections] objectAtIndex:section];
label.text = [#" " stringByAppendingString:[sectionInfo name]];
return label;
}
It doesnt matter what size is the parameters in UILabel frame, i always get the same result:
How can i center text in UILabel vertically?
Thanks

Try
[yourLabel setNumberOfLines:0];
[yourLabel sizeToFit];
[yourLabel setContentMode:UIViewContentModeCenter];

Put the label in a UIView then return the view:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
[view addSubview:label];
return view;
Make sure you implement the following method:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
otherwise your view will simply be on top of the cells.

Go through this link: NSTextAlignment
Also code snippet given below, may help you
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setText:#"Abc"];
[myLabel setTextAlignment:NSTextAlignmentCenter];

Related

Activate View for header delegate for specific section in single table

I am developing an application in which I need header to customize and add my own button just for single section. I googled and done some code where I am able to add button, but I am facing two issue.
Titles of other's section is not showing.
Button not show properly because of tableview scroll size same after adding button.
Here is what I am doing.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * headerView = [[[UIView alloc] initWithFrame:CGRectMake(1, 0, tableView.bounds.size.width, 40)] autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
if(section==2){
float width = tableView.bounds.size.width;
int fontSize = 18;
int padding = 10;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(padding, 2, width - padding, fontSize)];
label.text = #"Texto";
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0,1);
label.font = [UIFont boldSystemFontOfSize:fontSize];
[headerView addSubview:label];
UIButton * registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[registerButton setImage:[UIImage imageNamed:#"P_register_btn.png"] forState:UIControlStateNormal];
[registerButton setFrame:CGRectMake(0, 0, 320, 150)];
[headerView addSubview:registerButton];
return headerView;
}
return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section==0)
return #"Registration";
else if(section==1)
return #"Player Detail";
return nil;
}
Here is Image of my out put in which Texto text show but button is under that area where the end limit of table view scroll height and also section 0 and 1 title is not showing I also block code for first and second section in viewforheaderinsection. Thanks in advance.
The other header names don't appear because the viewForHeader method only answers for section 2. Once implemented, the datasource expects that method to be the authority on all headers. Just add some else logic for the other sections....
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * headerView;
UILabel *label;
if (section==2) {
headerView = [[[UIView alloc] initWithFrame:CGRectMake(1, 0, tableView.bounds.size.width, 40)] autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
// and so on
return headerView;
} else if (section == 0) {
label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,tableView.bounds.size.width, 44)] autorelease];
label.text = #"Section 0 Title";
return label;
} else .. and so on
The header answered by this method looks to be 40px high (see initWithFrame), but the button being added is 150px high (see setFrame: for the button). That's the likely the root cause of the button issue. Try implementing:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return (section == 2)? 150.0 : UITableViewAutomaticDimension;
}

iPhone When increasing the height of UITableView section header the height of the cell below it decreases. How to avoid?

I created a UITableView of which I adjusted the height and background of the section header. This all works as expected but the cell (row) below it becomes smaller. It almost seems that the section header goes over the first cell.
Is there a way to solve this? Or should I ask, how to solve this?
My code of the section header:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 30)];
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
[headerLabel setTextColor:[UIColor whiteColor]];
headerLabel.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"menuSectionBg.png"]];
[headerView addSubview:headerLabel];
return headerView;
}
You should not adjust the height of the cells' views, but instead implement the
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
method in your UITableViewDelegate implementation.
The heights for the cells are calculated before the cells are even created.

UITableView Section Header Font Issue

I'm trying to customize the section header for a UITableView. The UITableView was created in IB. I'm having 2 issues that I cannot figure out.
The font size will not increase past about 18 or something regardless of how large the UILabel is.
The section headers are obscuring the tables.
I've colored the labels blue so you can see their size.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// Name of section
NSString *header = [[purchases allKeys] objectAtIndex:section];
// Label for section header
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(10, 0, 230, 45);
label.textColor = _orange;
label.font = [UIFont fontWithName:#"Heiti TC Medium" size:52];
label.text = header;
label.backgroundColor = [UIColor blueColor];
// View to contain label
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 230, 45)];
[view autorelease];
[view addSubview:label];
return view;
}
You should add table view delegate method for header height
You should use all cell width for header view.
Your font named as STHeitiTC-Medium

How to set width of UITableView header view?

Is there any way to set the width of a UITableView section header view to something less than the full width of the UITableView? Now matter how wide I set the view returned by tableView:viewForHeaderInSection:, the header view is stretched to the width of the UITableView.
Here is my code:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *label = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 50, 50)];
label.backgroundColor = [UIColor yellowColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
return label;
}
Try this ., if i have understood your question this will surely work for you
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 60)];
headerView.backgroundColor = [UIColor clearColor];
UIView *label = [[UIView alloc] initWithFrame: CGRectMake(0,0, 50, 50)];
label.backgroundColor = [UIColor yellowColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
[headerView addSubview:label];
return headerView;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
if([[self.groupHeadings objectAtIndex:aSection] isEqualToString:#""])
return nil;
return [self.groupHeadings objectAtIndex:aSection];
}
Simple solution: return a UIView which contains nothing but your desired header view, while giving your headerView the UIViewAutoresizing property flexible right margin, so it won't scale with it's superview (or maybe flexible left margin, or both, depending on what loomk you want ;]).

How to change text color for Section Headers in a Grouped TableView in iPhone SDK?

I am making an iPhone app where in I have a grouped TableView with headers for the sections.
Problem is that I want to change the Section Header's text color.
How can I change the text color of Section Header?
What should I do?
Add the following code to your AppDelegate class in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
If you don't want to do it app wide like in Vahan's solution, here is a solution using one of UITableViewDelegate's method :
func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textColor = UIColor.redColor()
}
}
This is SURELY gonna work for you.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
tempView.backgroundColor=[UIColor clearColor];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
tempLabel.backgroundColor=[UIColor clearColor];
tempLabel.shadowColor = [UIColor blackColor];
tempLabel.shadowOffset = CGSizeMake(0,2);
tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:#"Helvetica" size:fontSizeForHeaders];
tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
tempLabel.text=#"Header Text";
[tempView addSubview:tempLabel];
[tempLabel release];
return tempView;
}
just copy and paste this function in your code.
You can implement this table view data source method:
- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
//create your custom label here & anything else you may want to add
return YourCustomView;
}
I built off of the answer from #Harsh.
This is the closest I could get, indistinguishable from what I can tell.
It goes in the <UITableViewDataSource> obviously.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *hView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
hView.backgroundColor=[UIColor clearColor];
UILabel *hLabel=[[[UILabel alloc] initWithFrame:CGRectMake(19,17,301,21)] autorelease];
hLabel.backgroundColor=[UIColor clearColor];
hLabel.shadowColor = [UIColor whiteColor];
hLabel.shadowOffset = CGSizeMake(0.5,1); // closest as far as I could tell
hLabel.textColor = [UIColor blackColor]; // or whatever you want
hLabel.font = [UIFont boldSystemFontOfSize:17];
hLabel.text = #"Your title here"; // probably from array
[hView addSubview:hLabel];
return hView;
}
#Harsh 's answer worked great for me, and by changing the coordinations of UILabel you can move it around. Also, I, personally thought to change the shadow offset a bit to make it more readable, but that could be a personal choice. Here's my version in case:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, -5, 300, 30)] autorelease];
//If you add a bit to x and decrease y, it will be more in line with the tableView cell (that is in iPad and landscape)
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor yellowColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.5., 0.5.);
label.font = [UIFont boldSystemFontOfSize:18];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]autorelease];
[view addSubview:label];
return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(20, 8, 220, 20);
myLabel.font = [UIFont boldSystemFontOfSize:16];
myLabel.text = [self tableView:tableView
titleForHeaderInSection:section];
myLabel.backgroundColor=[UIColor grayColor];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}