├── .gitignore
├── Image
└── screenshot.png
├── LICENSE
├── README.md
├── TweetTextView.xcodeproj
└── project.pbxproj
└── TweetTextView
├── AppDelegate.swift
├── Assets.xcassets
└── AppIcon.appiconset
│ └── Contents.json
├── Base.lproj
└── Main.storyboard
├── Info.plist
├── TweetTextView.entitlements
├── TweetTextView.swift
└── ViewController.swift
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.gitignore.io/api/xcode
3 |
4 | ### Xcode ###
5 | # Xcode
6 | #
7 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
8 |
9 | ## Build generated
10 | build/
11 | DerivedData/
12 |
13 | ## Various settings
14 | *.pbxuser
15 | !default.pbxuser
16 | *.mode1v3
17 | !default.mode1v3
18 | *.mode2v3
19 | !default.mode2v3
20 | *.perspectivev3
21 | !default.perspectivev3
22 | xcuserdata/
23 |
24 | ## Other
25 | *.moved-aside
26 | *.xccheckout
27 | *.xcscmblueprint
28 |
29 | ### Xcode Patch ###
30 | *.xcodeproj/*
31 | !*.xcodeproj/project.pbxproj
32 | !*.xcodeproj/xcshareddata/
33 | !*.xcworkspace/contents.xcworkspacedata
34 | /*.gcno
35 |
36 |
37 | # End of https://www.gitignore.io/api/xcode
38 |
39 |
40 |
41 |
42 |
43 |
44 | 山东移动404
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/Image/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/KarlZeo/TweetTextView/dcc464c204c23a6b279920264f1ae6ebb9f47b38/Image/screenshot.png
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 ZEO
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TweetTextView
2 |
3 | This is a custom NSTextView to heighlight username hashtag and link.
4 |
5 | ## Support
6 |
7 | Swift 4.2
8 |
9 | ## How to Use
10 |
11 | ```Swift
12 | //Set example String.
13 | let statusString: String = "Example tweet text here. Don't you just love AppKit? Here's a reference to @Twitter. Check out this cool new site: https://www.apple.com #hashtag #tutorial #中文测试"
14 |
15 | let insetRect = CGRect(x: 0, y: 0, width: 480, height: 270)
16 | let statusView = TweetTextView(frame: insetRect)
17 |
18 | //You must set usernameTextColor's value before statusString.
19 | statusView.usernameTextColor = NSColor.red
20 | //You must set linkTextColor's value before statusString.
21 | statusView.linkTextColor = NSColor.purple
22 | //You must set hashtagTextColor's value before statusString.
23 | statusView.hashtagTextColor = NSColor.lightGray
24 |
25 | //You must set usernameTextFont's value before statusString.
26 | statusView.usernameTextFont = NSFont.systemFont(ofSize: 18.0)
27 | //You must set linkTextFont's value before statusString.
28 | statusView.linkTextFont = NSFont.boldSystemFont(ofSize: 15.0)
29 | //You must set hashtagTextFont's value before statusString.
30 | statusView.hashtagTextFont = NSFont.boldSystemFont(ofSize: 20.0)
31 |
32 | statusView.statusString = statusString
33 |
34 | self.view.addSubview(statusView)
35 |
36 | //Set statusView's link string click action
37 | statusView.linkTarget = self
38 | statusView.linkAction = #selector(link)
39 |
40 | //Set statusView's username string click action
41 | statusView.usernameTarget = self
42 | statusView.usernameAction = #selector(username)
43 |
44 | //Set statusView's hashtag string click action
45 | statusView.hashtagTarget = self
46 | statusView.hashtagAction = #selector(hashtag)
47 | ```
48 |
49 | ## Screenshot
50 |
51 | 
52 |
53 | ## Objective-C version
54 |
55 | if you use Objective-C,you can use [TweetView-OS-X](https://github.com/JanX2/TweetView-OS-X).
56 |
57 | ## Thanks for
58 |
59 | Some code and idea from [TweetView-OS-X](https://github.com/JanX2/TweetView-OS-X).
60 | I add some code for this.Now can add custom action everywhere when you initialize finish.
61 |
62 | Thinks for [@JanX2](https://github.com/JanX2)'s oc code.
63 |
64 |
--------------------------------------------------------------------------------
/TweetTextView.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 48;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | A1BF8A6F201CD5B500180399 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1BF8A6E201CD5B500180399 /* AppDelegate.swift */; };
11 | A1BF8A71201CD5B500180399 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1BF8A70201CD5B500180399 /* ViewController.swift */; };
12 | A1BF8A73201CD5B500180399 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A1BF8A72201CD5B500180399 /* Assets.xcassets */; };
13 | A1BF8A76201CD5B500180399 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A1BF8A74201CD5B500180399 /* Main.storyboard */; };
14 | A1BF8A7F201CD5C600180399 /* TweetTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1BF8A7E201CD5C600180399 /* TweetTextView.swift */; };
15 | /* End PBXBuildFile section */
16 |
17 | /* Begin PBXCopyFilesBuildPhase section */
18 | A1B381622028770E00091837 /* Embed Frameworks */ = {
19 | isa = PBXCopyFilesBuildPhase;
20 | buildActionMask = 2147483647;
21 | dstPath = "";
22 | dstSubfolderSpec = 10;
23 | files = (
24 | );
25 | name = "Embed Frameworks";
26 | runOnlyForDeploymentPostprocessing = 0;
27 | };
28 | /* End PBXCopyFilesBuildPhase section */
29 |
30 | /* Begin PBXFileReference section */
31 | A1BF8A6B201CD5B500180399 /* TweetTextView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TweetTextView.app; sourceTree = BUILT_PRODUCTS_DIR; };
32 | A1BF8A6E201CD5B500180399 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
33 | A1BF8A70201CD5B500180399 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
34 | A1BF8A72201CD5B500180399 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
35 | A1BF8A75201CD5B500180399 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
36 | A1BF8A77201CD5B500180399 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
37 | A1BF8A78201CD5B500180399 /* TweetTextView.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = TweetTextView.entitlements; sourceTree = ""; };
38 | A1BF8A7E201CD5C600180399 /* TweetTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TweetTextView.swift; sourceTree = ""; };
39 | /* End PBXFileReference section */
40 |
41 | /* Begin PBXFrameworksBuildPhase section */
42 | A1BF8A68201CD5B500180399 /* Frameworks */ = {
43 | isa = PBXFrameworksBuildPhase;
44 | buildActionMask = 2147483647;
45 | files = (
46 | );
47 | runOnlyForDeploymentPostprocessing = 0;
48 | };
49 | /* End PBXFrameworksBuildPhase section */
50 |
51 | /* Begin PBXGroup section */
52 | A1BF8A62201CD5B500180399 = {
53 | isa = PBXGroup;
54 | children = (
55 | A1BF8A6D201CD5B500180399 /* TweetTextView */,
56 | A1BF8A6C201CD5B500180399 /* Products */,
57 | );
58 | sourceTree = "";
59 | };
60 | A1BF8A6C201CD5B500180399 /* Products */ = {
61 | isa = PBXGroup;
62 | children = (
63 | A1BF8A6B201CD5B500180399 /* TweetTextView.app */,
64 | );
65 | name = Products;
66 | sourceTree = "";
67 | };
68 | A1BF8A6D201CD5B500180399 /* TweetTextView */ = {
69 | isa = PBXGroup;
70 | children = (
71 | A1BF8A6E201CD5B500180399 /* AppDelegate.swift */,
72 | A1BF8A70201CD5B500180399 /* ViewController.swift */,
73 | A1BF8A7E201CD5C600180399 /* TweetTextView.swift */,
74 | A1BF8A72201CD5B500180399 /* Assets.xcassets */,
75 | A1BF8A74201CD5B500180399 /* Main.storyboard */,
76 | A1BF8A77201CD5B500180399 /* Info.plist */,
77 | A1BF8A78201CD5B500180399 /* TweetTextView.entitlements */,
78 | );
79 | path = TweetTextView;
80 | sourceTree = "";
81 | };
82 | /* End PBXGroup section */
83 |
84 | /* Begin PBXNativeTarget section */
85 | A1BF8A6A201CD5B500180399 /* TweetTextView */ = {
86 | isa = PBXNativeTarget;
87 | buildConfigurationList = A1BF8A7B201CD5B500180399 /* Build configuration list for PBXNativeTarget "TweetTextView" */;
88 | buildPhases = (
89 | A1BF8A67201CD5B500180399 /* Sources */,
90 | A1BF8A68201CD5B500180399 /* Frameworks */,
91 | A1BF8A69201CD5B500180399 /* Resources */,
92 | A1B381622028770E00091837 /* Embed Frameworks */,
93 | );
94 | buildRules = (
95 | );
96 | dependencies = (
97 | );
98 | name = TweetTextView;
99 | productName = TweetTextView;
100 | productReference = A1BF8A6B201CD5B500180399 /* TweetTextView.app */;
101 | productType = "com.apple.product-type.application";
102 | };
103 | /* End PBXNativeTarget section */
104 |
105 | /* Begin PBXProject section */
106 | A1BF8A63201CD5B500180399 /* Project object */ = {
107 | isa = PBXProject;
108 | attributes = {
109 | LastSwiftUpdateCheck = 0920;
110 | LastUpgradeCheck = 0940;
111 | ORGANIZATIONNAME = Zeo;
112 | TargetAttributes = {
113 | A1BF8A6A201CD5B500180399 = {
114 | CreatedOnToolsVersion = 9.2;
115 | ProvisioningStyle = Automatic;
116 | };
117 | };
118 | };
119 | buildConfigurationList = A1BF8A66201CD5B500180399 /* Build configuration list for PBXProject "TweetTextView" */;
120 | compatibilityVersion = "Xcode 8.0";
121 | developmentRegion = en;
122 | hasScannedForEncodings = 0;
123 | knownRegions = (
124 | en,
125 | Base,
126 | );
127 | mainGroup = A1BF8A62201CD5B500180399;
128 | productRefGroup = A1BF8A6C201CD5B500180399 /* Products */;
129 | projectDirPath = "";
130 | projectRoot = "";
131 | targets = (
132 | A1BF8A6A201CD5B500180399 /* TweetTextView */,
133 | );
134 | };
135 | /* End PBXProject section */
136 |
137 | /* Begin PBXResourcesBuildPhase section */
138 | A1BF8A69201CD5B500180399 /* Resources */ = {
139 | isa = PBXResourcesBuildPhase;
140 | buildActionMask = 2147483647;
141 | files = (
142 | A1BF8A73201CD5B500180399 /* Assets.xcassets in Resources */,
143 | A1BF8A76201CD5B500180399 /* Main.storyboard in Resources */,
144 | );
145 | runOnlyForDeploymentPostprocessing = 0;
146 | };
147 | /* End PBXResourcesBuildPhase section */
148 |
149 | /* Begin PBXSourcesBuildPhase section */
150 | A1BF8A67201CD5B500180399 /* Sources */ = {
151 | isa = PBXSourcesBuildPhase;
152 | buildActionMask = 2147483647;
153 | files = (
154 | A1BF8A71201CD5B500180399 /* ViewController.swift in Sources */,
155 | A1BF8A7F201CD5C600180399 /* TweetTextView.swift in Sources */,
156 | A1BF8A6F201CD5B500180399 /* AppDelegate.swift in Sources */,
157 | );
158 | runOnlyForDeploymentPostprocessing = 0;
159 | };
160 | /* End PBXSourcesBuildPhase section */
161 |
162 | /* Begin PBXVariantGroup section */
163 | A1BF8A74201CD5B500180399 /* Main.storyboard */ = {
164 | isa = PBXVariantGroup;
165 | children = (
166 | A1BF8A75201CD5B500180399 /* Base */,
167 | );
168 | name = Main.storyboard;
169 | sourceTree = "";
170 | };
171 | /* End PBXVariantGroup section */
172 |
173 | /* Begin XCBuildConfiguration section */
174 | A1BF8A79201CD5B500180399 /* Debug */ = {
175 | isa = XCBuildConfiguration;
176 | buildSettings = {
177 | ALWAYS_SEARCH_USER_PATHS = NO;
178 | CLANG_ANALYZER_NONNULL = YES;
179 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
180 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
181 | CLANG_CXX_LIBRARY = "libc++";
182 | CLANG_ENABLE_MODULES = YES;
183 | CLANG_ENABLE_OBJC_ARC = YES;
184 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
185 | CLANG_WARN_BOOL_CONVERSION = YES;
186 | CLANG_WARN_COMMA = YES;
187 | CLANG_WARN_CONSTANT_CONVERSION = YES;
188 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
189 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
190 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
191 | CLANG_WARN_EMPTY_BODY = YES;
192 | CLANG_WARN_ENUM_CONVERSION = YES;
193 | CLANG_WARN_INFINITE_RECURSION = YES;
194 | CLANG_WARN_INT_CONVERSION = YES;
195 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
196 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
197 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
198 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
199 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
200 | CLANG_WARN_STRICT_PROTOTYPES = YES;
201 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
202 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
203 | CLANG_WARN_UNREACHABLE_CODE = YES;
204 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
205 | CODE_SIGN_IDENTITY = "Mac Developer";
206 | COPY_PHASE_STRIP = NO;
207 | DEBUG_INFORMATION_FORMAT = dwarf;
208 | ENABLE_STRICT_OBJC_MSGSEND = YES;
209 | ENABLE_TESTABILITY = YES;
210 | GCC_C_LANGUAGE_STANDARD = gnu11;
211 | GCC_DYNAMIC_NO_PIC = NO;
212 | GCC_NO_COMMON_BLOCKS = YES;
213 | GCC_OPTIMIZATION_LEVEL = 0;
214 | GCC_PREPROCESSOR_DEFINITIONS = (
215 | "DEBUG=1",
216 | "$(inherited)",
217 | );
218 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
219 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
220 | GCC_WARN_UNDECLARED_SELECTOR = YES;
221 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
222 | GCC_WARN_UNUSED_FUNCTION = YES;
223 | GCC_WARN_UNUSED_VARIABLE = YES;
224 | MACOSX_DEPLOYMENT_TARGET = 10.13;
225 | MTL_ENABLE_DEBUG_INFO = YES;
226 | ONLY_ACTIVE_ARCH = YES;
227 | SDKROOT = macosx;
228 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
229 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
230 | };
231 | name = Debug;
232 | };
233 | A1BF8A7A201CD5B500180399 /* Release */ = {
234 | isa = XCBuildConfiguration;
235 | buildSettings = {
236 | ALWAYS_SEARCH_USER_PATHS = NO;
237 | CLANG_ANALYZER_NONNULL = YES;
238 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
239 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
240 | CLANG_CXX_LIBRARY = "libc++";
241 | CLANG_ENABLE_MODULES = YES;
242 | CLANG_ENABLE_OBJC_ARC = YES;
243 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
244 | CLANG_WARN_BOOL_CONVERSION = YES;
245 | CLANG_WARN_COMMA = YES;
246 | CLANG_WARN_CONSTANT_CONVERSION = YES;
247 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
248 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
249 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
250 | CLANG_WARN_EMPTY_BODY = YES;
251 | CLANG_WARN_ENUM_CONVERSION = YES;
252 | CLANG_WARN_INFINITE_RECURSION = YES;
253 | CLANG_WARN_INT_CONVERSION = YES;
254 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
255 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
256 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
257 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
258 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
259 | CLANG_WARN_STRICT_PROTOTYPES = YES;
260 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
261 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
262 | CLANG_WARN_UNREACHABLE_CODE = YES;
263 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
264 | CODE_SIGN_IDENTITY = "Mac Developer";
265 | COPY_PHASE_STRIP = NO;
266 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
267 | ENABLE_NS_ASSERTIONS = NO;
268 | ENABLE_STRICT_OBJC_MSGSEND = YES;
269 | GCC_C_LANGUAGE_STANDARD = gnu11;
270 | GCC_NO_COMMON_BLOCKS = YES;
271 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
272 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
273 | GCC_WARN_UNDECLARED_SELECTOR = YES;
274 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
275 | GCC_WARN_UNUSED_FUNCTION = YES;
276 | GCC_WARN_UNUSED_VARIABLE = YES;
277 | MACOSX_DEPLOYMENT_TARGET = 10.13;
278 | MTL_ENABLE_DEBUG_INFO = NO;
279 | SDKROOT = macosx;
280 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
281 | };
282 | name = Release;
283 | };
284 | A1BF8A7C201CD5B500180399 /* Debug */ = {
285 | isa = XCBuildConfiguration;
286 | buildSettings = {
287 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
288 | CODE_SIGN_ENTITLEMENTS = TweetTextView/TweetTextView.entitlements;
289 | CODE_SIGN_STYLE = Automatic;
290 | COMBINE_HIDPI_IMAGES = YES;
291 | DEVELOPMENT_TEAM = 8T3342R9BX;
292 | INFOPLIST_FILE = TweetTextView/Info.plist;
293 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
294 | PRODUCT_BUNDLE_IDENTIFIER = zeo.karl.mikulove.TweetTextView;
295 | PRODUCT_NAME = "$(TARGET_NAME)";
296 | SWIFT_VERSION = 4.2;
297 | };
298 | name = Debug;
299 | };
300 | A1BF8A7D201CD5B500180399 /* Release */ = {
301 | isa = XCBuildConfiguration;
302 | buildSettings = {
303 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
304 | CODE_SIGN_ENTITLEMENTS = TweetTextView/TweetTextView.entitlements;
305 | CODE_SIGN_STYLE = Automatic;
306 | COMBINE_HIDPI_IMAGES = YES;
307 | DEVELOPMENT_TEAM = 8T3342R9BX;
308 | INFOPLIST_FILE = TweetTextView/Info.plist;
309 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
310 | PRODUCT_BUNDLE_IDENTIFIER = zeo.karl.mikulove.TweetTextView;
311 | PRODUCT_NAME = "$(TARGET_NAME)";
312 | SWIFT_VERSION = 4.2;
313 | };
314 | name = Release;
315 | };
316 | /* End XCBuildConfiguration section */
317 |
318 | /* Begin XCConfigurationList section */
319 | A1BF8A66201CD5B500180399 /* Build configuration list for PBXProject "TweetTextView" */ = {
320 | isa = XCConfigurationList;
321 | buildConfigurations = (
322 | A1BF8A79201CD5B500180399 /* Debug */,
323 | A1BF8A7A201CD5B500180399 /* Release */,
324 | );
325 | defaultConfigurationIsVisible = 0;
326 | defaultConfigurationName = Release;
327 | };
328 | A1BF8A7B201CD5B500180399 /* Build configuration list for PBXNativeTarget "TweetTextView" */ = {
329 | isa = XCConfigurationList;
330 | buildConfigurations = (
331 | A1BF8A7C201CD5B500180399 /* Debug */,
332 | A1BF8A7D201CD5B500180399 /* Release */,
333 | );
334 | defaultConfigurationIsVisible = 0;
335 | defaultConfigurationName = Release;
336 | };
337 | /* End XCConfigurationList section */
338 | };
339 | rootObject = A1BF8A63201CD5B500180399 /* Project object */;
340 | }
341 |
--------------------------------------------------------------------------------
/TweetTextView/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // TweetTextView
4 | //
5 | // Created by Zeo on 27/01/2018.
6 | // Copyright © 2018 Zeo. All rights reserved.
7 | //
8 |
9 | import Cocoa
10 |
11 | @NSApplicationMain
12 | class AppDelegate: NSObject, NSApplicationDelegate {
13 |
14 |
15 |
16 | func applicationDidFinishLaunching(_ aNotification: Notification) {
17 | // Insert code here to initialize your application
18 | }
19 |
20 | func applicationWillTerminate(_ aNotification: Notification) {
21 | // Insert code here to tear down your application
22 | }
23 |
24 |
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/TweetTextView/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/TweetTextView/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
--------------------------------------------------------------------------------
/TweetTextView/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
11 | CFBundleIdentifier
12 | $(PRODUCT_BUNDLE_IDENTIFIER)
13 | CFBundleInfoDictionaryVersion
14 | 6.0
15 | CFBundleName
16 | $(PRODUCT_NAME)
17 | CFBundlePackageType
18 | APPL
19 | CFBundleShortVersionString
20 | 1.0
21 | CFBundleVersion
22 | 1
23 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSHumanReadableCopyright
26 | Copyright © 2018 Zeo. All rights reserved.
27 | NSMainStoryboardFile
28 | Main
29 | NSPrincipalClass
30 | NSApplication
31 |
32 |
33 |
--------------------------------------------------------------------------------
/TweetTextView/TweetTextView.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/TweetTextView/TweetTextView.swift:
--------------------------------------------------------------------------------
1 | // TVTextView.swift
2 | // ActiveLabelMac
3 | //
4 | // Created by Zeo on 27/01/2018.
5 | // Copyright © 2018 Zeo. All rights reserved.
6 | //
7 |
8 | import Cocoa
9 |
10 | let TVLinkMatchAttributeName = "TVLinkMatch"
11 | let TVUsernameMatchAttributeName = "TVUsernameMatch"
12 | let TVHashtagMatchAttributeName = "TVHashtagMatch"
13 |
14 | class TweetTextView: NSTextView {
15 |
16 | public var linkAction: Selector?
17 |
18 | public var linkTarget: AnyObject?
19 |
20 | public var usernameAction: Selector?
21 |
22 | public var usernameTarget: AnyObject?
23 |
24 | public var hashtagAction: Selector?
25 |
26 | public var hashtagTarget: AnyObject?
27 |
28 | public var usernameTextColor: NSColor = {
29 | let color = NSColor(cgColor: NSColor.blue.cgColor)
30 | return color!
31 | }()
32 |
33 | public var linkTextColor: NSColor = {
34 | let color = NSColor(cgColor: NSColor.blue.cgColor)
35 | return color!
36 | }()
37 |
38 | public var hashtagTextColor: NSColor = {
39 | let color = NSColor(cgColor: NSColor.lightGray.cgColor)
40 | return color!
41 | }()
42 |
43 | public var usernameTextFont: NSFont = {
44 | let font = NSFont.boldSystemFont(ofSize: 14.0)
45 | return font
46 | }()
47 |
48 | public var linkTextFont: NSFont = {
49 | let font = NSFont.boldSystemFont(ofSize: 14.0)
50 | return font
51 | }()
52 |
53 | public var hashtagTextFont: NSFont = {
54 | let font = NSFont.systemFont(ofSize: 14.0)
55 | return font
56 | }()
57 |
58 | public var textShadow: NSShadow = {
59 | let shadow = NSShadow()
60 | shadow.shadowColor = NSColor(deviceWhite: 1, alpha: 0.8)
61 | shadow.shadowBlurRadius = 0
62 | shadow.shadowOffset = NSMakeSize(0, -1)
63 | return shadow
64 | }()
65 |
66 | public var paragraphStyle: NSMutableParagraphStyle = {
67 | let style = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
68 | style?.minimumLineHeight = 22
69 | style?.maximumLineHeight = 22
70 | style?.paragraphSpacing = 0
71 | style?.paragraphSpacingBefore = 0
72 | style?.tighteningFactorForTruncation = 4
73 | style?.alignment = .natural
74 | style?.lineBreakMode = .byWordWrapping
75 | return style!
76 | }()
77 |
78 | var statusString: String {
79 | set {
80 | let attStr = self.setAttributedString(newValue)
81 | self.textStorage?.setAttributedString(attStr)
82 | }
83 | get {
84 | return ""
85 | }
86 | }
87 |
88 | override init(frame frameRect: NSRect) {
89 | super.init(frame: frameRect)
90 |
91 | }
92 |
93 | required init?(coder: NSCoder) {
94 | fatalError("init(coder:) has not been implemented")
95 | }
96 |
97 | override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
98 | super.init(frame: frameRect, textContainer: container)
99 | self.originSetting()
100 | }
101 |
102 | override func awakeFromNib() {
103 | self.originSetting()
104 | }
105 |
106 | func originSetting() {
107 | self.autoresizingMask = [.width, .height]
108 | self.backgroundColor = NSColor.clear
109 | self.textContainerInset = NSZeroSize
110 | self.isEditable = false
111 | self.isSelectable = true
112 | }
113 |
114 |
115 | private func setAttributedString(_ originString: String) -> NSMutableAttributedString {
116 | let statusString: String = originString
117 |
118 | let attributedStatusString = NSMutableAttributedString(string: statusString)
119 |
120 | let fullAttributes = [
121 | NSAttributedString.Key.foregroundColor: NSColor(deviceHue: 0.53, saturation: 0.13, brightness: 0.26, alpha: 1),
122 | NSAttributedString.Key.shadow: self.textShadow,
123 | NSAttributedString.Key.cursor: NSCursor.arrow,
124 | NSAttributedString.Key.kern: 0.0,
125 | NSAttributedString.Key.ligature: 0,
126 | NSAttributedString.Key.paragraphStyle: self.paragraphStyle,
127 | NSAttributedString.Key.font: NSFont.systemFont(ofSize: 14.0)
128 | ] as [NSAttributedString.Key : Any]
129 | attributedStatusString.addAttributes(fullAttributes, range: NSRange(location: 0, length: statusString.count))
130 |
131 | let linkMatches = scanString(forLinks: statusString)
132 | let usernameMatches = scanString(forUsernames: statusString)
133 | let hashtagMatches = scanString(forHashtags: statusString)
134 |
135 | for match: NSTextCheckingResult in linkMatches {
136 | let range: NSRange = match.range
137 | if range.location != NSNotFound {
138 | let string: NSString = NSString(string: statusString)
139 | let linkMatchedString = string.substring(with: range)
140 | let linkAttr: NSDictionary = [
141 | NSAttributedString.Key.cursor: NSCursor.pointingHand,
142 | NSAttributedString.Key.foregroundColor: self.linkTextColor,
143 | NSAttributedString.Key.font: self.linkTextFont,
144 | TVLinkMatchAttributeName: linkMatchedString
145 | ]
146 | attributedStatusString.addAttributes(linkAttr as! [NSAttributedString.Key : Any], range: range)
147 | }
148 | }
149 |
150 | for match: NSTextCheckingResult in usernameMatches {
151 | let range: NSRange = match.range
152 | if range.location != NSNotFound {
153 | let string: NSString = NSString(string: statusString)
154 | let usernameMatchedString = string.substring(with: range)
155 | // Add custom attribute of UsernameMatch to indicate where our usernames are found
156 | let linkAttr2: NSDictionary = [
157 | NSAttributedString.Key.foregroundColor: self.usernameTextColor,
158 | NSAttributedString.Key.cursor: NSCursor.pointingHand,
159 | NSAttributedString.Key.font: self.usernameTextFont,
160 | TVUsernameMatchAttributeName: usernameMatchedString
161 | ]
162 | attributedStatusString.addAttributes(linkAttr2 as! [NSAttributedString.Key : Any], range: range)
163 | }
164 | }
165 |
166 | for match: NSTextCheckingResult in hashtagMatches {
167 | let range: NSRange = match.range
168 | if range.location != NSNotFound {
169 | let string: NSString = NSString(string: statusString)
170 | let hashtagMatchedString = string.substring(with: range)
171 | // Add custom attribute of HashtagMatch to indicate where our hashtags are found
172 | let linkAttr3: NSDictionary = [
173 | NSAttributedString.Key.foregroundColor: self.hashtagTextColor,
174 | NSAttributedString.Key.foregroundColor: NSCursor.pointingHand,
175 | NSAttributedString.Key.font: self.hashtagTextFont,
176 | TVHashtagMatchAttributeName: hashtagMatchedString
177 | ]
178 | attributedStatusString.addAttributes(linkAttr3 as! [NSAttributedString.Key : Any], range: range)
179 | }
180 | }
181 | return attributedStatusString
182 | }
183 |
184 | override func mouseDown(with event: NSEvent) {
185 | let point = self.convert(event.locationInWindow, from: nil)
186 | let charIndex = self.characterIndexForInsertion(at: point)
187 | if (NSLocationInRange(charIndex, NSRange(location: 0, length: self.string.count)) == true) {
188 | let attributes: NSDictionary = attributedString().attributes(at: charIndex, effectiveRange: nil) as NSDictionary
189 | if attributes[TVLinkMatchAttributeName] != nil {
190 | self.linkTarget?.perform(self.linkAction, with: self)
191 | }
192 | if attributes[TVUsernameMatchAttributeName] != nil {
193 | self.usernameTarget?.perform(self.usernameAction, with: self)
194 | }
195 | if attributes[TVHashtagMatchAttributeName] != nil {
196 | self.hashtagTarget?.perform(hashtagAction, with: self)
197 | }
198 | }
199 | }
200 |
201 | private func scanString(forLinks string: String) -> [NSTextCheckingResult] {
202 | var regEx: NSRegularExpression? = nil
203 | var onceToken = NSInteger()
204 | if (onceToken == 0) {
205 | /* TODO: move below code to a static variable initializer (dispatch_once is deprecated) */
206 | let pattern = "\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/)))"
207 | var error: Error?
208 | regEx = try? NSRegularExpression(pattern: pattern, options: [])
209 | if regEx == nil {
210 | print("\(error)")
211 | }
212 | }
213 | onceToken = 1
214 | let fullRange = NSRange(location: 0, length: (string.count ?? 0))
215 | let matches = regEx?.matches(in: string, options: [], range: fullRange)
216 | return matches ?? [NSTextCheckingResult]()
217 | }
218 |
219 | private func scanString(forUsernames string: String) -> [NSTextCheckingResult] {
220 | var regEx: NSRegularExpression? = nil
221 | var onceToken = NSInteger()
222 | if (onceToken == 0) {
223 | /* TODO: move below code to a static variable initializer (dispatch_once is deprecated) */
224 | let pattern = "@{1}([-A-Za-z0-9_]{2,})"
225 | var error: Error?
226 | regEx = try? NSRegularExpression(pattern: pattern, options: [])
227 | if regEx == nil {
228 | print("\(error)")
229 | }
230 | }
231 | onceToken = 1
232 | let fullRange = NSRange(location: 0, length: (string.count ?? 0))
233 | let matches = regEx?.matches(in: string, options: [], range: fullRange)
234 | return matches ?? [NSTextCheckingResult]()
235 | }
236 |
237 | private func scanString(forHashtags string: String) -> [NSTextCheckingResult] {
238 | var regEx: NSRegularExpression? = nil
239 | var onceToken = NSInteger()
240 | if (onceToken == 0) {
241 | /* TODO: move below code to a static variable initializer (dispatch_once is deprecated) */
242 | let pattern = "[\\s]{1,}#{1}([^\\s]{2,})"
243 | var error: Error?
244 | regEx = try? NSRegularExpression(pattern: pattern, options: [])
245 | if regEx == nil {
246 | print("\(error)")
247 | }
248 | }
249 | onceToken = 1
250 | let fullRange = NSRange(location: 0, length: (string.count ?? 0))
251 | let matches = regEx?.matches(in: string, options: [], range: fullRange)
252 | return matches ?? [NSTextCheckingResult]()
253 | }
254 | }
255 |
256 |
--------------------------------------------------------------------------------
/TweetTextView/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // TweetTextView
4 | //
5 | // Created by Zeo on 27/01/2018.
6 | // Copyright © 2018 Zeo. All rights reserved.
7 | //
8 |
9 | import Cocoa
10 |
11 | class ViewController: NSViewController {
12 |
13 | override func viewDidLoad() {
14 | super.viewDidLoad()
15 |
16 | let statusString: String = "Example tweet text here. Don't you just love AppKit? Here's a reference to @Twitter. Check out this cool new site: https://www.apple.com #hashtag #tutorial #中文测试"
17 |
18 | let insetRect = CGRect(x: 0, y: 0, width: 480, height: 270)
19 | let statusView = TweetTextView(frame: insetRect)
20 | statusView.usernameTextColor = NSColor.red
21 | statusView.linkTextColor = NSColor.purple
22 | statusView.hashtagTextColor = NSColor.lightGray
23 | statusView.usernameTextFont = NSFont.systemFont(ofSize: 18.0)
24 | statusView.linkTextFont = NSFont.boldSystemFont(ofSize: 15.0)
25 | statusView.hashtagTextFont = NSFont.boldSystemFont(ofSize: 20.0)
26 | statusView.statusString = statusString
27 |
28 | self.view.addSubview(statusView)
29 |
30 | statusView.linkTarget = self
31 | statusView.linkAction = #selector(link)
32 | statusView.usernameTarget = self
33 | statusView.usernameAction = #selector(username)
34 | statusView.hashtagTarget = self
35 | statusView.hashtagAction = #selector(hashtag)
36 | }
37 |
38 | @objc func link() {
39 | print("link")
40 | }
41 |
42 | @objc func username() {
43 | print("username")
44 | }
45 |
46 | @objc func hashtag() {
47 | print("hashtag")
48 | }
49 |
50 | override var representedObject: Any? {
51 | didSet {
52 | // Update the view, if already loaded.
53 | }
54 | }
55 | }
56 |
57 |
--------------------------------------------------------------------------------