点击后恢复NSAttributedString的可视状态

问题描述:

我需要在点击它之后恢复NSAttributedString的可视状态。点击后恢复NSAttributedString的可视状态

我的NSAttributedString包含归因于范围的链接。

在这个例子中文字 “@user” 有AA到 “HTPP://somesite.com/”:

let text = "Hey @user!" 

let attr = NSMutableAttributedString(string: text) 
let range = NSRange(location: 4, length: 5) 
attr.addAttribute(NSForegroundColorAttributeName, value: NSColor.orange, range: range) 
attr.addAttribute(NSLinkAttributeName, value: "htpp://somesite.com/", range: range) 

let tf = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50)) 
tf.allowsEditingTextAttributes = true 
tf.isSelectable = true 
tf.stringValue = text 
tf.attributedStringValue = attr 

它运作良好:在文本字段的 “@user” 点击,它启动了URL。

enter image description here

但是,一旦点击,归因颜色消失,并通过这个蓝色的更换和添加​​下划线:

enter image description here

我无法找到一个解决方案,以恢复单击字符串时的原始颜色(或者避免共有此自动更改)。

我见过thisthis但没有实际的解决方案,我不能将指向的库集成到我的项目中(我真的很想不必导入任何库,实际上)。

请注意,我现有的代码是在Swift中,但我可以使用Objective-C解决方案。

单击链接时,文本由字段编辑器显示。字段编辑器中的默认链接文本样式为蓝色并带下划线。

解决方案1:在NSTextFieldCell的子类中更改替代setUpFieldEditorAttributes:中的链接的文本样式。

- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj { 
    NSText *fieldEditor = [super setUpFieldEditorAttributes:textObj]; 
    if ([fieldEditor isKindOfClass:[NSTextView class]]) { 
     NSMutableDictionary *linkAttributes = [((NSTextView *)fieldEditor).linkTextAttributes mutableCopy]; 
     linkAttributes[NSForegroundColorAttributeName] = [NSColor orangeColor]; 
     [linkAttributes removeObjectForKey:NSUnderlineStyleAttributeName]; 
     ((NSTextView *)fieldEditor).linkTextAttributes = linkAttributes; 
    } 
    return fieldEditor; 
} 

副作用:现场编辑器由窗口中的所有控件共享,所有控件现在将显示橙色链接。

解决方案2:使用fieldEditor:forObject:方法或windowWillReturnFieldEditor:toObject:委托方法NSWindow替换您自己的字段编辑器。文本字段有其自己的字段编辑器,其他控件不会有橙色链接。不需要NSTextFieldNSTextFieldCell的子类。

例子:(AppDelegate中是窗口的代表)

@interface AppDelegate() 

@property (weak) IBOutlet NSWindow *window; 
@property (weak) IBOutlet NSTextField *textField; 
@property (nonatomic, strong) NSTextView *linkFieldEditor; 

@end 

@implementation AppDelegate 

- (NSTextView *)linkFieldEditor { 
    if (!_linkFieldEditor) { 
     _linkFieldEditor = [[NSTextView alloc] initWithFrame:NSZeroRect]; 
     _linkFieldEditor.fieldEditor = YES; 
     NSMutableDictionary *linkAttributes = [_linkFieldEditor.linkTextAttributes mutableCopy]; 
     linkAttributes[NSForegroundColorAttributeName] = [NSColor orangeColor]; 
     [linkAttributes removeObjectForKey:NSUnderlineStyleAttributeName]; 
     _linkFieldEditor.linkTextAttributes = linkAttributes; 
    } 
    return _linkFieldEditor; 
} 

- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)client { 
    if (client == self.textField) 
     return self.linkFieldEditor; 
    else 
     return nil; 
} 

解决方案3:创建NSTextFieldCell一个子类,实现fieldEditorForView:并返回自己的字段编辑器。这与解决方案2类似,但由单元而不是窗口委托实现。

现场编辑器文档:Text Fields, Text Views, and the Field EditorUsing a Custom Field Editor

+0

第一个解决方案:非常好。但副作用有点问题。有没有办法从那里获取链接本身?我可以过滤它的类型并设置正确的颜色。 //我不明白你的第二个解决方案,对不起,如果你可以解释...:p – Moritz

+0

'NSTextFieldCell'也有一个propertyStringValue属性。 – Willeke

+0

与此同时,我编辑了我的答案。 – Willeke