 |
 |
|
 |
10-14-2008, 04:22 AM
|
#1 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 31
|
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.
|
|
|
10-14-2008, 06:26 AM
|
#2 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 1,431
|
I don't think you can.
|
|
|
10-14-2008, 09:17 AM
|
#3 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 31
|
Quote:
Originally Posted by PhoneyDeveloper
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.
|
|
|
10-14-2008, 09:53 AM
|
#4 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 1,431
|
Use viewForHeaderInSection and use a UILabel. You may also need to implement heightForHeaderInSection.
|
|
|
10-14-2008, 03:27 PM
|
#5 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 31
|
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.
|
|
|
10-14-2008, 04:50 PM
|
#6 (permalink)
|
|
New Member
Join Date: Sep 2008
Posts: 1,431
|
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.
|
|
|
10-16-2008, 11:27 AM
|
#7 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 31
|
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 *)tableView  UITableView *)tableView viewForHeaderInSection  NSInteger) section
{
return self.myHeaderView2;
}
|
|
|
02-09-2009, 04:14 PM
|
#8 (permalink)
|
|
New Member
Join Date: Jan 2009
Posts: 1
|
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
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 *)tableView  UITableView *)tableView viewForHeaderInSection  NSInteger) section
{
return self.myHeaderView2;
}
|
|
|
|
04-23-2009, 09:34 PM
|
#9 (permalink)
|
|
Registered Member
Join Date: Mar 2009
Posts: 4
|
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;
}
|
|
|
08-13-2009, 08:15 PM
|
#10 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 1
|
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
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;
}
|
|
|
|
08-22-2009, 04:45 PM
|
#11 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 9
|
Quote:
Originally Posted by kooljava2
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
|
- 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];
- 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...)
|
|
|
10-24-2009, 12:39 PM
|
#12 (permalink)
|
|
Registered Member
Join Date: Oct 2009
Posts: 91
|
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.'
|
|
|
12-25-2009, 02:56 AM
|
#13 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 3
|
This worked beautifully. Thank you.
Quote:
Originally Posted by salboy
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;
}
|
|
|
|
01-17-2010, 09:53 AM
|
#14 (permalink)
|
|
Registered Member
Join Date: Jan 2010
Posts: 1
|
Multiline header text
If the text of your header is multiline, add the instruction :
headerLabel.numberOfLines = 0;
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 414 |
| 38 members and 376 guests |
| AdamSubach, aderrington, benoitr007, BrianSlick, caseysackett, Danneman, dev123, ErichGS, futurevilla216, Gambit, GreatWizard, gustavo7sexton, gw1921, HemiMG, HowEver, iSDK, Jeremy1026, joelhull, lifeCoder45, mattiahalter, melodizzzy, mriphoneman, newchucky, Ovidius, Piequanna, qilin, Racker, rendezvouscp, riq, Sega dude, socals, themathminister, timle8n1, tinrocket, Whitehk, ZunePod |
| Most users ever online was 965, 06-30-2010 at 04:26 AM. |
» Stats |
Members: 41,862
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
|
| Welcome to our newest member, futurevilla216 |
|