Adding UITextLabel to ShinobiDataGrid header cell


#1

I have a basic grid set up, and for one of the header cells I would like to apply two different sets of styles to certain parts of the text. The text of the header is “YTD (%)”, but I would like the “(%)” to be smaller and a different colour.

I am thinking the way to achieve this is to make “(%)” a separate UITextLabel and add it as a subview, but am not sure which view I need to add it to, or how I can access that view. Anybody have any ideas, or examples that I could check out?

Thanks!


#2

Greetings Program

I couldn’t find a delegate method specifically for when the header cell was preparing for display but I was able to accomplish this by adding a UILabel to the subview of the headercell in the prepareCellForDisplay:

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid prepareCellForDisplay:(SDataGridCell *)cell
{
	// If the column is the YTD and the header cell does not have the percent label, add it.
	if ([cell.coordinate.column.title isEqualToString:@"YTD"] && ![cell.coordinate.column.headerCell viewWithTag:10]) {
		// Create a label with the frame where ever you need it in the header cell.
		UILabel *percent = [[UILabel alloc] initWithFrame:CGRectMake(75, 0, 50, 50)];
		percent.tag = 10;
		percent.text = @"%";
		percent.font = [UIFont fontWithName:@"Helvetica" size:14.0];
		
		// Add it to the header cell.
		[cell.coordinate.column.headerCell addSubview:percent];
	}
}

I tried adding it when creating the column but it didn’t work there for me:

// Add a YTD column
    SDataGridColumn* ytdColumn = [[SDataGridColumn alloc] initWithTitle:@"YTD"];
    ytdColumn.width = @484;

	// Create a label with the frame where ever you need it in the header cell.
	UILabel *percent = [[UILabel alloc] initWithFrame:CGRectMake(75, 0, 50, 50)];
	percent.tag = 10;
	percent.text = @"%";
	percent.font = [UIFont fontWithName:@"Helvetica" size:14.0];
	
	// Add it to the cell header.
	[ytdColumn.headerCell addSubview:percent];
    
	[_shinobiDataGrid addColumn:ytdColumn];

Hopefully the Shinobi Squad can provide some insight and perhaps has an easier way to do it.

Wg


#3

Thanks wizgod, I’ve used your solution that occurs within prepareCellForDisplay and it’s working fine. It would be nice to do it at the same time as creating each column, but this works well too.

Thanks again,

James


#4

I found you could use a custom headerCellType when creating the column which would probably be a better solution:

SDataGridColumn* ytdColumn = [[SDataGridColumn alloc] initWithTitle:@"YTD" forProperty:@"ytdProperty" cellType:[SDataGridTextCell class] headerCellType:[YtdHeader class]];
    ytdColumn.width = @250;
    [_shinobiDataGrid addColumn:ytdColumn];

YtdHeader.h

#import <ShinobiGrids/ShinobiDataGrid.h>

@interface YtdHeader : SDataGridHeaderCell

@end

YtdHeader.m

#import "YtdHeader.h"

@implementation YtdHeader
{
    UILabel *percent;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    // When the cell frame is set, update the percent frame.
    percent.frame = CGRectMake(120, 0, 50, self.bounds.size.height);
}

- (id)initWithReuseIdentifier:(NSString *)identifier
{
    if (self = [super initWithReuseIdentifier:identifier]) {
                // Add the percent label.
		percent = [[UILabel alloc] init];
		percent.tag = 10;
		percent.text = @"(%)";
		percent.font = [UIFont fontWithName:@"Helvetica" size:14.0];

        [self addSubview:percent];
    }
    return self;
}

@end

Wg


#5

Thanks again wg, this is a nice solution and have used it in my application!  :laughing: