Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f14288f
Added some convenience functions to NSTextField subclass to enable ad…
Mar 27, 2014
a630f5a
Added some NSString and NSAttributedString convenience methods.
Thesaurus Aug 14, 2015
4af46dd
Support multiple hyperlinks within a single textfield.
Thesaurus Aug 31, 2015
a739f2a
Return copy of receiver rather than nil if link key not found.
Thesaurus Jan 22, 2016
69b59a5
Added convenience stringValue setter method.
Thesaurus Mar 2, 2016
7cb980c
Asset the URLS as it is all to easy to pass in string values in when …
Thesaurus Apr 19, 2016
a2f57f5
Default to native hyperlink support. The custom hyperlink implementat…
Nov 17, 2016
31387fa
Comment updates.
Nov 17, 2016
bf2af2c
Apply control font if none found when setting attributed string. If t…
Nov 17, 2016
9af3120
Added convenience method.
Dec 28, 2016
93cf21a
Fix text redraw issue when using bindings.
Dec 28, 2016
5d39f5b
Try and turn hyperlink underlining off.
Jan 5, 2017
2f1c2c8
Comment update
Feb 19, 2017
0ca0d45
Added convenience method for text with multiple hyperlinks.
Mar 27, 2017
914dd73
Added convenience method.
Apr 7, 2017
41ef4c6
Improve cursor handling
Oct 3, 2017
6bbc17d
Allow empty attributed string value.
Feb 22, 2018
4303f0c
Try and dispel link tool tips.
Apr 16, 2018
a5cc136
Check for empty link colour.
Apr 29, 2018
c6c1728
Additional convenience methods
Aug 28, 2018
c63787b
Fix missing return value
Aug 30, 2018
14a45a7
On macOS 10.14 we seem to need to enforce the text field init in orde…
Oct 28, 2018
5e33cf6
Add explicit enabling of hyperlinks.
Nov 20, 2018
1bf3261
Add convenience method and ensure that font is preserved when adding …
Apr 5, 2020
cf35100
Additional support for adding multiple hyperlinks.
Nov 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NSTextFieldHyperlinks/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet HyperlinkTextField *hyperlinkTextField;

@property (weak) IBOutlet HyperlinkTextField *hyperlinkTextField2;
@property (weak) IBOutlet HyperlinkTextField *hyperlinkTextField3;
@end
14 changes: 9 additions & 5 deletions NSTextFieldHyperlinks/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// Create hyperlink
NSString *linkName = @"blog";
NSURL *url = [NSURL URLWithString:@"http://toomasvahter.wordpress.com"];
NSMutableAttributedString *hyperlinkString = [[NSMutableAttributedString alloc] initWithString:linkName];
[hyperlinkString beginEditing];
[hyperlinkString addAttribute:NSLinkAttributeName value:url range:NSMakeRange(0, [hyperlinkString length])];
[hyperlinkString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:NSMakeRange(0, [hyperlinkString length])];
[hyperlinkString endEditing];
NSAttributedString *hyperlinkString = [self.hyperlinkTextField hyperlink:linkName toURL:url];
[resultString appendAttributedString:hyperlinkString];

NSString *plainString = @". Some pretty interesting posts.";
[resultString appendAttributedString:[[NSAttributedString alloc] initWithString:plainString]];

[self.hyperlinkTextField setAttributedStringValue:resultString];

// Update existing textfield substring with hyperlink
self.hyperlinkTextField2.linkColor = [NSColor redColor];
[self.hyperlinkTextField2 updateSubstring:@"blog" withHyperLinkToURL:url];

// Replace existing textfield content key with hyperlink
self.hyperlinkTextField3.linkColor = [NSColor greenColor];
[self.hyperlinkTextField3 replaceSubstring:@"$key" withHyperLink:@"blog" toURL:url];
}

@end
86 changes: 86 additions & 0 deletions NSTextFieldHyperlinks/HyperlinkTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,91 @@

#import <Cocoa/Cocoa.h>

// link option keys
extern NSString * HTLinkOption;
extern NSString * HTUrlOption;
extern NSString * HTColorOption;

@interface HyperlinkTextField : NSTextField

@property (strong) NSCursor *cursor;

/*!

Explicity enable and disable hyperlinks.

*/
@property (assign) BOOL hyperlinksEnabled;

/*!

Link color

*/
@property (strong) NSColor *linkColor;

/*!

Update control attributed string with link options to define hyperlink.

*/
- (void)updateAttributedStringValueWithLinkOptions:(NSArray <NSDictionary <NSString *, NSObject *> *>*)options;

/*!

Set control string with link options to define hyperlink.

*/
- (void)setStringValue:(NSString *)stringValue linkOptions:(NSArray <NSDictionary <NSString *, NSObject *> *>*)options;

/*!

Set control string with attributes and link options to define hyperlink.

*/
- (void)setStringValue:(NSString *)stringValue attributes:(NSDictionary<NSString *, id> *)attributes linkOptions:(NSArray <NSDictionary <NSString *, NSObject *> *>*)options;

/*!

Update control substring with hyperlink to given URL

*/
- (void)updateSubstring:(NSString *)linktext withHyperLinkToURL:(NSURL *)linkURL;

/*!

Update control substring with hyperlink to given URL

*/
- (void)replaceSubstring:(NSString *)linkKey withHyperLink:(NSString *)linktext toURL:(NSURL *)linkURL;

/*!

Make hyperlink attributed string to given URL

*/
- (NSAttributedString *)hyperlink:(NSString *)linktext toURL:(NSURL *)linkURL;

@end

@interface NSString (HyperTextField)
- (NSAttributedString *)htf_hyperlinkToURL:(NSURL *)linkURL;
- (NSAttributedString *)htf_hyperlinkToURL:(NSURL *)linkURL linkColor:(NSColor *)linkColor;
- (NSAttributedString *)htf_hyperlinkToURL:(NSURL *)linkURL linkColor:(NSColor *)linkColor font:(NSFont *)font;
- (NSAttributedString *)htf_hyperlinkWithAttributes:(NSDictionary<NSString *, id> *)attributes linkOptions:(NSArray <NSDictionary <NSString *, NSObject *> *>*)options;
@end

@interface NSAttributedString (HyperTextField)

- (NSAttributedString *)htf_replaceSubstringWithHyperLink:(NSString *)linktext toURL:(NSURL *)linkURL;

- (NSAttributedString *)htf_replaceSubstringWithHyperLink:(NSString *)linktext
toURL:(NSURL *)linkURL
linkColor:(NSColor *)linkColor;

- (NSAttributedString *)htf_replaceSubstring:(NSString *)linkKey
withHyperLink:(NSString *)linktext
toURL:(NSURL *)linkURL
linkColor:(NSColor *)linkColor;
@end

Loading