2012-08-29 40 views
0

我一直試圖強調懸停NSTextField。我的第一次嘗試發生的事情是,文本被加下劃線,但是它在前面的文本之上。隨着另一次嘗試,它變得越來越大膽。下劃線懸停可可 - Objective-C

-(void)mouseEntered:(NSEvent *)theEvent { 
if (is_underlined) 
    return; 
is_underlined = YES; 

//NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName]; 
//NSMutableAttributedString * as = [[NSMutableAttributedString alloc] initWithString:self.event.label 
                 //   attributes:attrsDictionary]; 
//[as autorelease]; 
NSMutableAttributedString * as = [[[self attributedStringValue] mutableCopy] autorelease]; 
[as beginEditing]; 
[as addAttribute:NSUnderlineStyleAttributeName 
      value:[NSNumber numberWithInt:NSUnderlineStyleSingle] 
      range:[[[self window] fieldEditor:YES forObject:self] selectedRange]]; 

[as endEditing]; 
[self setAttributedStringValue:as]; 
} 

-(void)mouseExited:(NSEvent *)theEvent { 
    if (!is_underlined) 
     return; 
    is_underlined = NO; 
    // self.stringValue = self.event.label; 
    //[self setAttributedStringValue:nil]; 
    NSMutableAttributedString * as = [[[self attributedStringValue] mutableCopy] autorelease]; 
    [as beginEditing]; 
    [as removeAttribute:NSUnderlineStyleAttributeName range:[[[self window] fieldEditor:YES forObject:self] selectedRange]]; 
    // [as addAttribute:NSUnderlineStyleAttributeName 
     //   value:[NSNumber numberWithInt:0] 
    //   range:[[[self window] fieldEditor:YES forObject:self] selectedRange]]; 

    [as endEditing]; 
    [self setAttributedStringValue:as]; 
} 

回答

0

我認爲我找到了答案 - 它不在代碼本身,而是代碼中的代碼,我構建的代碼。作者在drawRect中實現了這些視圖,這意味着每次繪製矩形時,都會在同一位置創建一個新的文本框。