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

1392

被折叠的 条评论
为什么被折叠?



