admin管理员组文章数量:1122846
I'm following this code here where I have an NSTableView that displays the folder name as a group header and then the contents of the folder below, for this example these are all images.
So like, here I have a folder named flags and a folder named states, what I want to do is to change the font size of this header, I have successfully changed the text color but for some reason I can't change the font size neither through storyboard nor programmatically. Here's my tableView:viewForTableColumn:row: method
-(NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
{
DesktopEntity *entity = _tableContents[row];
if ([entity isKindOfClass:[DesktopFolderEntity class]])
{
NSTextField *groupCell = [tableView makeViewWithIdentifier:@"GroupCell" owner:self];
[groupCell setStringValue: entity.name];
[groupCell setFont:[NSFont fontWithName:@"Arial" size:40]];
[groupCell setTextColor:[NSColor magentaColor]];
return groupCell;
}
else if ([entity isKindOfClass:[DesktopImageEntity class]])
{
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"ImageCell" owner:self];
[cellView.textField setStringValue: entity.name];
[cellView.textField setFont:[NSFont fontWithName:@"Impact" size:20]];
[cellView.imageView setImage: [(DesktopImageEntity *)entity image]];
return cellView;
}
return nil;
}
what am I doing wrong here, and how can I change the font size programmatically?
Edit
I tried changing the font size in the second if statement for the "ImageCell" and it works, but I still can't get it to work for the "GroupCell" in the first if statement.
Looking a bit further, there's this method tableView:isGroupRow: that I'm using:
-(BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row
{
DesktopEntity *entity = _tableContents[row];
if ([entity isKindOfClass:[DesktopFolderEntity class]])
{
return YES;
}
return NO;
}
I found out that if I comment out this function those changes work and the font size gets updated, but after removing this function, the cell won't act like a group cell but rather as a normal cell which is not what I wanted. Any way around this?
Here's the table after trying to change the font size and color(while still maintaining the isGroupRow method), you can see that the font color changed but its size remains the same, what gives?
I'm following this code here where I have an NSTableView that displays the folder name as a group header and then the contents of the folder below, for this example these are all images.
So like, here I have a folder named flags and a folder named states, what I want to do is to change the font size of this header, I have successfully changed the text color but for some reason I can't change the font size neither through storyboard nor programmatically. Here's my tableView:viewForTableColumn:row: method
-(NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
{
DesktopEntity *entity = _tableContents[row];
if ([entity isKindOfClass:[DesktopFolderEntity class]])
{
NSTextField *groupCell = [tableView makeViewWithIdentifier:@"GroupCell" owner:self];
[groupCell setStringValue: entity.name];
[groupCell setFont:[NSFont fontWithName:@"Arial" size:40]];
[groupCell setTextColor:[NSColor magentaColor]];
return groupCell;
}
else if ([entity isKindOfClass:[DesktopImageEntity class]])
{
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"ImageCell" owner:self];
[cellView.textField setStringValue: entity.name];
[cellView.textField setFont:[NSFont fontWithName:@"Impact" size:20]];
[cellView.imageView setImage: [(DesktopImageEntity *)entity image]];
return cellView;
}
return nil;
}
what am I doing wrong here, and how can I change the font size programmatically?
Edit
I tried changing the font size in the second if statement for the "ImageCell" and it works, but I still can't get it to work for the "GroupCell" in the first if statement.
Looking a bit further, there's this method tableView:isGroupRow: that I'm using:
-(BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row
{
DesktopEntity *entity = _tableContents[row];
if ([entity isKindOfClass:[DesktopFolderEntity class]])
{
return YES;
}
return NO;
}
I found out that if I comment out this function those changes work and the font size gets updated, but after removing this function, the cell won't act like a group cell but rather as a normal cell which is not what I wanted. Any way around this?
Here's the table after trying to change the font size and color(while still maintaining the isGroupRow method), you can see that the font color changed but its size remains the same, what gives?
Share Improve this question edited Nov 22, 2024 at 20:20 IRP_HANDLER asked Nov 22, 2024 at 1:15 IRP_HANDLERIRP_HANDLER 3081 silver badge9 bronze badges 5 |1 Answer
Reset to default 0As Willeke explained, the solution here is to subclass NSTableRowView and override the isGroupRowStyle method
@interface CustomTableRowView : NSTableRowView
@end
@implementation CustomTableRowView
-(BOOL)isGroupRowStyle
{
return NO;
}
//custom background for a table row view
- (void)drawBackgroundInRect:(NSRect)dirtyRect
{
NSColor *groupBackgroundColor = [NSColor windowBackgroundColor];
[groupBackgroundColor setFill];
NSRectFill(dirtyRect);
}
@end
I also had to override rowViewForRow for the NSTableViewDelegate:
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
DesktopEntity *entity = _tableContents[row];
if ([entity isKindOfClass:[DesktopFolderEntity class]])
{
return [[CustomTableRowView alloc] init];
}
// use default row for non-group rows
return nil;
}
本文标签: objective cCan39t change the font size of an NSTableViewStack Overflow
版权声明:本文标题:objective c - Can't change the font size of an NSTableView - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306250a1932890.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
NSTableRowView
, overrideisGroupRowStyle
and returnNO
. – Willeke Commented Nov 22, 2024 at 20:40