【Mac OS X開發】NSTextFieldCell選中和正常狀態的字型段落樣式設定
阿新 • • 發佈:2019-02-18
在設定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]; } }