悬停Cocoa下划线 - Objective-C

时间:2012-08-29 00:05:58

标签: objective-c cocoa

我一直试图强调悬停NSTextField。我在第一次尝试时发生的事情是文本有下划线,但它在前一篇文章的基础上。通过另一次尝试,它变得越来越大胆。

-(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];
}

1 个答案:

答案 0 :(得分:0)

我认为我找到了答案 - 它不在于代码本身,而在于代码中我正在构建的演示代码。作者在drawRect中实现了这些视图,这意味着每次绘制矩形时,都会在同一位置创建一个新的文本字段。