【Mac OS X开发】NSTextFieldCell选中和正常状态的字体段落样式设置

本文详细介绍了如何通过调整NSTextFieldCell的attributedStringValue属性,实现选中状态和未选中状态时的两种不同字体段落样式,并通过避免直接复制初始化属性字段来解决运行时出现的EXC_BAD_ACCESS错误问题。

在设置NSTextFieldCell在选中状态和未选中状态时的两种不同的字体段落样式时,即更改字体颜色与字体类型、大小和段落的首行缩进、行未显示完全的分割样式等,查找网上的资料,发现有很多是复制attributedStringValue进行属性字典的构建,先前也觉得这样很便利,可以直接针对某属性样式进行修改,如下所示:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
    if([self isHighlighted]) { // 该单元格处于选中状态时
        color = [NSColor cyanColor];
        [color set];
        NSRectFill(cellFrame);
        
        NSDictionary *attribs = [[[[self attributedStringValue] attributesAtIndex:0 effectiveRange:nil] mutableCopy] autorelease];
        [attribs setValue:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
        NSMutableParagraphStyle *paraStyle = [attribs objectForKey:NSParagraphStyleAttributeName];
        [paraStyle setFirstLineHeadIndent:12.0];
        [paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
        [[self title] drawInRect:cellFrame withAttributes:attribs];
        
    }
    else { // 该单元格处于未选中状态时
        NSDictionary *attribs = [[[[self attributedStringValue] attributesAtIndex:0 effectiveRange:nil] mutableCopy] autorelease];
        [attribs setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
        NSMutableParagraphStyle *paraStyle = [attribs objectForKey:NSParagraphStyleAttributeName];
        [paraStyle setFirstLineHeadIndent:12.0];
        [paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
        [[self title] drawInRect:cellFrame withAttributes:attribs];
    }
}

但是,这样直接使用NSTextFieldCell的attributedStringValue进行拷贝初始化属性字段,有时候运行程序的时候会出现提示为“Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)”的bug,经过Profile的Zombie测试后才发现问题出现在这里。

后来试着修改成如下所示,顺利通过测试!即先构造一个空的NSDictionary,再向里面添加自定义的键值,NSMutableParagraphStyle初始化时使用defaultParagraphStyle进行拷贝而来。

现将运行顺利的代码附上:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
    if([self isHighlighted]) { // 该单元格处于选中状态时
        color = [NSColor cyanColor];
        [color set];
        NSRectFill(cellFrame);
        
        NSDictionary *attribs = [[[NSMutableDictionary alloc] init] autorelease];
        [attribs setValue:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
        NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
        [paraStyle setFirstLineHeadIndent:12.0];
        [paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
        [attribs setValue:paraStyle forKey:NSParagraphStyleAttributeName];
        NSFont *font = [NSFont fontWithName:@"Helvetica" size:14];
        [attribs setValue:font forKey:NSFontAttributeName];
        [[self title] drawInRect:cellFrame withAttributes:attribs];
        
    }
    else { // 该单元格处于未选中状态时
        NSDictionary *attribs = [[[NSMutableDictionary alloc] init] autorelease];
        [attribs setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
        NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
        [paraStyle setFirstLineHeadIndent:12.0];
        [paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
        [attribs setValue:paraStyle forKey:NSParagraphStyleAttributeName];
        NSFont *font = [NSFont fontWithName:@"Helvetica" size:14];
        [attribs setValue:font forKey:NSFontAttributeName];
        [[self title] drawInRect:cellFrame withAttributes:attribs];
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值