2016-10-03 3 views
0

클릭 한 후 NSAttributedString의 시각적 상태를 복원해야합니다.클릭 한 후 NSAttributedString의 시각적 상태를 복원하십시오.

내 NSAttributedString에 범위에 속한 링크가 포함되어 있습니다. 텍스트 필드에 "@user"를 클릭 : "//somesite.com/ HTPP"그것은 잘 작동

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"에서

는에 AA 링크가 URL을 시작합니다. I 복원 솔루션을 찾을 수 없습니다

enter image description here

:

enter image description here

하지만 일단은 기인 색이 사라지고이 블루로 대체되며, 밑줄이 추가되고, 클릭 문자열이 클릭되면 원래 색상 (또는이 자동 변경이 전혀 발생하지 않도록).

내가 thisthis을 본 적이 있지만이 실제 해결책은 없다, 나는 내 프로젝트에 뾰족한 라이브러리를 통합 할 수 있습니다 (정말 실제로, 어떤 라이브러리를 가져올 필요가 없습니다 싶습니다).

기존 코드는 Swift에 있지만 Objective-C 솔루션을 사용할 수 있습니다.

답변

1

링크를 클릭하면 텍스트가 필드 편집기에 표시됩니다. 필드 편집기의 기본 링크 텍스트 스타일은 파란색이고 밑줄이 그어져 있습니다.

해결 방법 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을 사용하여 필드 편집기를 대체하십시오. 텍스트 필드에는 자체 필드 편집기가 있고 다른 컨트롤에는 주황색 링크가 없습니다. NSTextField 또는 NSTextFieldCell의 서브 클래스가 필요 없습니다.

예 : (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 : fieldEditorForView:을 구현하고 자신의 분야 편집기를 반환 NSTextFieldCell의 서브 클래스를 생성합니다. 이것은 솔루션 2와 유사하지만 윈도우 델리게이트 대신 셀에 의해 구현됩니다.

필드 편집기의 문서는 Text Fields, Text Views, and the Field EditorUsing a Custom Field Editor입니다.

+0

첫 번째 해결 방법 : 아주 좋음. 그러나 부작용은 약간의 문제입니다. 거기에서 링크 자체를 얻을 수있는 방법이 있습니까? 그 유형을 필터링하고 올바른 색상을 다시 설정할 수 있습니다. // 설명 할 수 있다면 두 번째 해결책을 이해하지 못합니다. : p – Moritz

+0

'NSTextFieldCell'에는 'attributedStringValue'속성이 있습니다. – Willeke

+0

그동안 나는 내 대답을 편집했습니다. – Willeke

관련 문제