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
  • Have you tried an attributed string? – red_menace Commented Nov 22, 2024 at 2:10
  • I tried your code and it works for me. Post a minimal reproducible example please. – Willeke Commented Nov 22, 2024 at 10:05
  • @Willeke here's the whole thing github.com/lucasderraugh/AppleProg-Cocoa-Tutorials/tree/master/… – IRP_HANDLER Commented Nov 22, 2024 at 16:48
  • @Willeke for some reason it only works for the second if statement, but not for the first when trying to change the font size for the groupcell – IRP_HANDLER Commented Nov 22, 2024 at 17:48
  • 1 Group rows are drawn in group row style. Workaround: Subclass NSTableRowView, override isGroupRowStyle and return NO. – Willeke Commented Nov 22, 2024 at 20:40
Add a comment  | 

1 Answer 1

Reset to default 0

As 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