Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-14-2008, 04:22 AM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default Font size/color of TableView Header

Hi,

I cant seem to figure out a way to change the font size of the header text on UITableView (titleForHeaderInSection)

Anyone care to elaborate ?

Thanks for any help.
bergetun is offline   Reply With Quote
Old 10-14-2008, 06:26 AM   #2 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

I don't think you can.
PhoneyDeveloper is offline   Reply With Quote
Old 10-14-2008, 09:17 AM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
I don't think you can.
ohh, thats really at bummer.

Not even the font color ?

Last edited by bergetun; 10-14-2008 at 09:25 AM.
bergetun is offline   Reply With Quote
Old 10-14-2008, 09:53 AM   #4 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Use viewForHeaderInSection and use a UILabel. You may also need to implement heightForHeaderInSection.
PhoneyDeveloper is offline   Reply With Quote
Old 10-14-2008, 03:27 PM   #5 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default

Thanks for trying to help.

The HeaderFooter example on Apple.com does exactly what you are saying, but it still shows the titleForHeaderInSection text + what they added to viewForHeaderInSection

I have several sections.
bergetun is offline   Reply With Quote
Old 10-14-2008, 04:50 PM   #6 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

No it doesn't. There are two kinds of headers. There's a table header and there are section headers. That example sets the table header to a view and one section header to text. You need to set the section headers to views.
PhoneyDeveloper is offline   Reply With Quote
Old 10-16-2008, 11:27 AM   #7 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default

aha. My fault.

Thank you very much.


For future reference to anyone else having the same problem.
If you want to change font or font color of the section title you to replace the default view with your own.

- (UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger) section
{
return self.myHeaderView2;
}
bergetun is offline   Reply With Quote
Old 02-09-2009, 04:14 PM   #8 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 1
Default Have an example of this?

bergetun: would you have an example of what the code looks like where you build the new header view?

Thanks.

Quote:
Originally Posted by bergetun View Post
aha. My fault.

Thank you very much.


For future reference to anyone else having the same problem.
If you want to change font or font color of the section title you to replace the default view with your own.

- (UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger) section
{
return self.myHeaderView2;
}
derfberg is offline   Reply With Quote
Old 04-23-2009, 09:34 PM   #9 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 4
Default

For those still looking for a solution:

Code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	// create the parent view that will hold header Label
	UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
	
	// create the button object
	UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
	headerLabel.backgroundColor = [UIColor clearColor];
	headerLabel.opaque = NO;
	headerLabel.textColor = [UIColor blackColor];
	headerLabel.highlightedTextColor = [UIColor whiteColor];
	headerLabel.font = [UIFont boldSystemFontOfSize:20];
	headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);

	// If you want to align the header text as centered
	// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);

	headerLabel.text = <Put here whatever you want to display> // i.e. array element
	[customView addSubview:headerLabel];

	return customView;
}
Furthermore, it is advisable to add heightForHeaderInSection function

Code:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 44.0;
}
salboy is offline   Reply With Quote
Old 08-13-2009, 08:15 PM   #10 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 1
Default

Thanks for the solution. It works beautifully. Couple of questions:

1. Why do we need a customview. Why can't we use just UILabel only?
2. Is there a memory leak here for customview and the label?

Thanks

Quote:
Originally Posted by salboy View Post
For those still looking for a solution:

Code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	// create the parent view that will hold header Label
	UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
	
	// create the button object
	UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
	headerLabel.backgroundColor = [UIColor clearColor];
	headerLabel.opaque = NO;
	headerLabel.textColor = [UIColor blackColor];
	headerLabel.highlightedTextColor = [UIColor whiteColor];
	headerLabel.font = [UIFont boldSystemFontOfSize:20];
	headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);

	// If you want to align the header text as centered
	// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);

	headerLabel.text = <Put here whatever you want to display> // i.e. array element
	[customView addSubview:headerLabel];

	return customView;
}
Furthermore, it is advisable to add heightForHeaderInSection function

Code:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 44.0;
}
kooljava2 is offline   Reply With Quote
Old 08-22-2009, 04:45 PM   #11 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 9
Default

Quote:
Originally Posted by kooljava2 View Post
Thanks for the solution. It works beautifully. Couple of questions:

1. Why do we need a customview. Why can't we use just UILabel only?
2. Is there a memory leak here for customview and the label?

Thanks
  1. You could use a UILabel; it's a UIView subclass after all. As a matter of fact, Apple's docs suggest returning a UILabel as an example. If you read salboy's code, though, you'll see that the label bounds X coordinate is set to 10.0 so that it won't bump against the left side of the table. And it's set inside a view because...

    I'm not sure specifically how UITableView handles section titles, but since they bump up against each other as they slide out of view I imagine they're simply views being moved directly over the contents of the tableView as it's tracking. In the default case, there's already a view with an embedded label offset inside the view. When you return the section header text via tableView:titleForHeaderInSection:, it's simply setting the default label's text property.

    If you'd just passed a UILabel, it would be offset to the bounds of the section header frame, and draw the text at the extreme left of the table. Try it.

    Just as a point of style, I probably would have init'ed customView a bit differently:
    Code:
    UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 44.0)] autorelease];
    The view's X coordinate doesn't make much of a difference. It's the label's offset you're worried about.

    Oh, and to mimic the standard UITableView behavior, I'd set the header view's backgroundColor to a color with a 0.9 or so alpha component. If you wanted to use gray, it'd look like:
    Code:
    customView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.9];
  2. Yes, both the view and label are leaking. Personally, I'd alloc the view as autorelease (because you have to return it) and release the label after you've added it to the view. The view will be released when its superview is released. (Well, actually when its autoreleasePool is drained...)
rwetmore is offline   Reply With Quote
Old 10-24-2009, 12:39 PM   #12 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 91
Arrow

Has anybody got this working with an xib file?

I tried this but it crashes on me:

Code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {	
	
	id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] 
													objectAtIndex:section];
	
	SpecialOffersTableViewSectionHeader *tableViewSectionHeader = 
		(SpecialOffersTableViewSectionHeader*)[[[NSBundle mainBundle] loadNibNamed:@"SpecialOffersTableViewSectionHeader" 
																			 owner:self 
																		   options:nil] lastObject];
	tableViewSectionHeader.sectionHeader.text = [sectionInfo name];
	self.tableView.tableHeaderView = tableViewSectionHeader;

	return tableViewSectionHeader;
}
Code:
@interface SpecialOffersTableViewSectionHeader : UIView {
    IBOutlet UILabel *sectionHeader;
}
@property (nonatomic, retain) IBOutlet UILabel *sectionHeader;
@end
Code:
#import "SpecialOffersTableViewSectionHeader.h"
@implementation SpecialOffersTableViewSectionHeader
@synthesize sectionHeader;
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
- (void)dealloc {
    [super dealloc];
}
@end
The error I get in the console is:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SpecialOffersTableViewController 0x3c0c8f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key sectionHeader.'
tychop is offline   Reply With Quote
Old 12-25-2009, 02:56 AM   #13 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 3
Default

This worked beautifully. Thank you.

Quote:
Originally Posted by salboy View Post
For those still looking for a solution:

Code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	// create the parent view that will hold header Label
	UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
	
	// create the button object
	UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
	headerLabel.backgroundColor = [UIColor clearColor];
	headerLabel.opaque = NO;
	headerLabel.textColor = [UIColor blackColor];
	headerLabel.highlightedTextColor = [UIColor whiteColor];
	headerLabel.font = [UIFont boldSystemFontOfSize:20];
	headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);

	// If you want to align the header text as centered
	// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);

	headerLabel.text = <Put here whatever you want to display> // i.e. array element
	[customView addSubview:headerLabel];

	return customView;
}
Furthermore, it is advisable to add heightForHeaderInSection function

Code:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 44.0;
}
treblig is offline   Reply With Quote
Old 01-17-2010, 09:53 AM   #14 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 1
Default Multiline header text

If the text of your header is multiline, add the instruction :
headerLabel.numberOfLines = 0;
Futtersack is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 41,862
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
Welcome to our newest member, futurevilla216
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:04 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0