├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── SwiftResponsiveLabel.podspec ├── SwiftResponsiveLabel.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcuserdata │ │ └── susmitahorrow.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── susmitahorrow.xcuserdatad │ └── xcschemes │ ├── SwiftResponsiveLabel.xcscheme │ └── xcschememanagement.plist ├── SwiftResponsiveLabel ├── .DS_Store ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ ├── Contents.json │ └── check.imageset │ │ ├── 1436972523_check.png │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Demo Project │ ├── AppDelegate.swift │ ├── InteractiveTableViewCell.swift │ ├── InteractiveTableViewController.swift │ └── MainViewController.swift ├── Info.plist └── Source │ ├── NSAttributedString+Processing.swift │ ├── PatternDescriptor.swift │ ├── PatternHighlighter.swift │ ├── SwiftResponsiveLabel+TruncationHandler.swift │ ├── SwiftResponsiveLabel.swift │ ├── TextKitStack.swift │ └── TouchHandler.swift ├── SwiftResponsiveLabelTests ├── Info.plist └── SwiftResponsiveLabelTests.swift └── SwiftResponsiveLabelUITests ├── Info.plist └── SwiftResponsiveLabelUITests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata 19 | 20 | ## Other 21 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | ## Playgrounds 31 | timeline.xctimeline 32 | playground.xcworkspace 33 | 34 | # Swift Package Manager 35 | # 36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 37 | # Packages/ 38 | .build/ 39 | 40 | # CocoaPods 41 | # 42 | # We recommend against adding the Pods directory to your .gitignore. However 43 | # you should judge for yourself, the pros and cons are mentioned at: 44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 45 | # 46 | # Pods/ 47 | 48 | # Carthage 49 | # 50 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 51 | # Carthage/Checkouts 52 | 53 | Carthage/Build 54 | 55 | # fastlane 56 | # 57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 58 | # screenshots whenever they are needed. 59 | # For more information about the recommended setup visit: 60 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 61 | 62 | fastlane/report.xml 63 | fastlane/screenshots 64 | .DS_Store -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | version 2.3 2 | 3 | * Fix the issue of `truncationIndicatorImage` not visible 4 | 5 | version 2.2 6 | 7 | * Add support for Swift 4 8 | * Refactoring of RangeAttribute. Now it is a struct instead of tuple 9 | * Typealias `AttributesDictionary` added for `[String: Any]` 10 | * Fix the issue of truncation token not visible for texts with newline characters 11 | 12 | version 2.1 13 | 14 | * Fix crash at wordWrappedAttributedString calculation 15 | 16 | version 2.0 17 | 18 | * Upgrade to Swift 3 19 | * Optimize drawing method 20 | * Fix highlighting of text 21 | 22 | version 1.2 23 | 24 | * customTruncationEnabled is public 25 | * Truncation token can be an image 26 | * Allow detection of custom pattern via PatternDescriptor 27 | * Add documentation 28 | 29 | version 1.1 30 | 31 | * First version 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 hsusmita 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 | [![Version](https://img.shields.io/badge/pod-2.2-green.svg)](https://cocoapods.org/pods/SwiftResponsiveLabel) 2 | [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/hsusmita/SwiftResponsiveLabel/blob/master/LICENSE) 3 | [![Documentation](https://img.shields.io/badge/platform-iOS-orange.svg?style=flat)](http://cocoadocs.org/docsets/SwiftResponsiveLabel) 4 | 5 | # SwiftResponsiveLabel 6 | 7 | A UILabel subclass which responds to touch on specified patterns. It has the following features: 8 | 9 | 1. It can detect pattern specified by regular expression and apply style such as font, color etc. 10 | 2. It allows to replace default ellipse with tappable attributed string to mark truncation 11 | 3. Convenience methods are provided to detect hashtags, username handler and URLs 12 | 13 | #Installation 14 | 15 | Add following lines in your pod file 16 | ``` 17 | pod 'SwiftResponsiveLabel', '2.3' 18 | ``` 19 | 20 | #Usage 21 | 22 | The following snippets explain the usage of public methods. These snippets assume an instance of ResponsiveLabel named "customLabel". 23 | ``` 24 | import SwiftResponsiveLabel 25 | ``` 26 | 27 | In interface builder, set the custom class of your UILabel to SwiftResponsiveLabel. 28 | 29 | #### Username Handle Detection 30 | 31 | ``` 32 | let userHandleTapAction = PatternTapResponder{ (tappedString)-> (Void) in 33 | let messageString = "You have tapped user handle:" + tappedString 34 | self.messageLabel.text = messageString 35 | } 36 | let dict = [NSForegroundColorAttributeName: UIColor.greenColor(), 37 | NSBackgroundColorAttributeName: UIColor.blackColor()] 38 | self.customLabel.enableUserHandleDetection([NSForegroundColorAttributeName:UIColor.grayColor(), 39 | RLHighlightedAttributesDictionary: dict, RLTapResponderAttributeName:userHandleTapAction]) 40 | ``` 41 | 42 | #### URL Detection 43 | 44 | ``` 45 | let URLTapAction = PatternTapResponder{(tappedString)-> (Void) in 46 | let messageString = "You have tapped URL: " + tappedString 47 | self.messageLabel.text = messageString 48 | } 49 | self.customLabel.enableURLDetection([NSForegroundColorAttributeName:UIColor.blueColor(), RLTapResponderAttributeName:URLTapAction]) 50 | ``` 51 | 52 | #### HashTag Detection 53 | 54 | ``` 55 | let hashTagTapAction = PatternTapResponder { (tappedString)-> (Void) in 56 | let messageString = "You have tapped hashTag:" + tappedString 57 | self.messageLabel.text = messageString 58 | } 59 | let dict = [NSForegroundColorAttributeName: UIColor.redColor(), NSBackgroundColorAttributeName: UIColor.blackColor()] 60 | customLabel.enableHashTagDetection([RLHighlightedAttributesDictionary : dict, NSForegroundColorAttributeName: UIColor.cyanColor(), 61 | RLTapResponderAttributeName:hashTagTapAction]) 62 | ``` 63 | #### Custom Truncation Token 64 | ##### Set attributed string as truncation token 65 | 66 | ``` 67 | let action = PatternTapResponder {(tappedString)-> (Void) in 68 | print("You have tapped token string") 69 | } 70 | let dict = [RLHighlightedBackgroundColorAttributeName:UIColor.blackColor(), 71 | RLHighlightedForegroundColorAttributeName:UIColor.greenColor(), RLTapResponderAttributeName:action] 72 | let token = NSAttributedString(string: "...More", attributes: [NSFontAttributeName: customLabel.font, 73 | NSForegroundColorAttributeName:UIColor.brownColor(), RLHighlightedAttributesDictionary: dict]) 74 | customLabel.attributedTruncationToken = token 75 | ``` 76 | ##### Set image as truncation token 77 | 78 | The height of image size should be approximately equal to or less than the font height. Otherwise the image will not be rendered properly 79 | ``` 80 | let action = PatternTapResponder {(tappedString)-> (Void) in 81 | print("You have tapped token image") 82 | } 83 | self.customLabel.setTruncationIndicatorImage(UIImage(named: "check")!, withSize: CGSize(width: 20.0, height: 20.0), andAction: action) 84 | ``` 85 | 86 | ##### Set from interface builder 87 | 88 | 89 | # Screenshots 90 | 91 | 92 | 93 | # References 94 | 95 | The underlying implementation of SwiftResponsiveLabel is based on KILabel(https://github.com/Krelborn/KILabel). 96 | SwiftResponsiveLabel is made flexible to enable detection of any pattern specified by regular expression. 97 | 98 | The following articles were helpful in enhancing the functionalities. 99 | 100 | * http://www.cocoanetics.com/2015/03/customizing-uilabel-hyperlinks/ 101 | * http://www.cocoanetics.com/2015/03/tappable-uilabel-hyperlinks/ 102 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel.podspec: -------------------------------------------------------------------------------- 1 | Pod:: Spec.new do |spec| 2 | spec.platform = 'ios', '8.0' 3 | spec.name = 'SwiftResponsiveLabel' 4 | spec.version = '2.4' 5 | spec.summary = 'A UILabel subclass which responds to touch on specified patterns and allows to set custom truncation token' 6 | spec.author = { 7 | 'Susmita Horrow' => 'susmita.horrow@gmail.com' 8 | } 9 | spec.license = 'MIT' 10 | spec.homepage = 'https://github.com/hsusmita/SwiftResponsiveLabel' 11 | spec.source = { 12 | :git => 'https://github.com/hsusmita/SwiftResponsiveLabel.git', 13 | :tag => '2.4' 14 | } 15 | spec.ios.deployment_target = '8.0' 16 | spec.swift_version = '5.0' 17 | spec.source_files = 'SwiftResponsiveLabel/Source/*' 18 | spec.requires_arc = true 19 | end 20 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4EB857BE1C8823CE00431BA9 /* SwiftResponsiveLabel+TruncationHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB857BD1C8823CE00431BA9 /* SwiftResponsiveLabel+TruncationHandler.swift */; }; 11 | 4ECBA0111C8434EA00884FCD /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4ECBA00F1C8434EA00884FCD /* Main.storyboard */; }; 12 | 4ECBA0131C8434EA00884FCD /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4ECBA0121C8434EA00884FCD /* Assets.xcassets */; }; 13 | 4ECBA0161C8434EA00884FCD /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4ECBA0141C8434EA00884FCD /* LaunchScreen.storyboard */; }; 14 | 4ECBA0211C8434EA00884FCD /* SwiftResponsiveLabelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ECBA0201C8434EA00884FCD /* SwiftResponsiveLabelTests.swift */; }; 15 | 4ECBA02C1C8434EA00884FCD /* SwiftResponsiveLabelUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ECBA02B1C8434EA00884FCD /* SwiftResponsiveLabelUITests.swift */; }; 16 | 4ECBA03C1C8570D400884FCD /* TextKitStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ECBA03B1C8570D400884FCD /* TextKitStack.swift */; }; 17 | 4ECBA03E1C85710500884FCD /* SwiftResponsiveLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ECBA03D1C85710500884FCD /* SwiftResponsiveLabel.swift */; }; 18 | 4ECBA0401C85913A00884FCD /* NSAttributedString+Processing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ECBA03F1C85913A00884FCD /* NSAttributedString+Processing.swift */; }; 19 | 4ECBA0421C85A14700884FCD /* TouchHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ECBA0411C85A14700884FCD /* TouchHandler.swift */; }; 20 | 4EF960EB1C86E905007CB596 /* PatternDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF960E91C86E905007CB596 /* PatternDescriptor.swift */; }; 21 | 4EF960EC1C86E905007CB596 /* PatternHighlighter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EF960EA1C86E905007CB596 /* PatternHighlighter.swift */; }; 22 | 7808F2501DE2D30D00D7F9DC /* InteractiveTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7808F24F1DE2D30D00D7F9DC /* InteractiveTableViewController.swift */; }; 23 | 7808F2531DE2D37500D7F9DC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7808F2511DE2D37500D7F9DC /* AppDelegate.swift */; }; 24 | 7808F2541DE2D37500D7F9DC /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7808F2521DE2D37500D7F9DC /* MainViewController.swift */; }; 25 | 7808F2561DE2D3B100D7F9DC /* InteractiveTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7808F2551DE2D3B100D7F9DC /* InteractiveTableViewCell.swift */; }; 26 | /* End PBXBuildFile section */ 27 | 28 | /* Begin PBXContainerItemProxy section */ 29 | 4ECBA01D1C8434EA00884FCD /* PBXContainerItemProxy */ = { 30 | isa = PBXContainerItemProxy; 31 | containerPortal = 4ECBA0001C8434EA00884FCD /* Project object */; 32 | proxyType = 1; 33 | remoteGlobalIDString = 4ECBA0071C8434EA00884FCD; 34 | remoteInfo = SwiftResponsiveLabel; 35 | }; 36 | 4ECBA0281C8434EA00884FCD /* PBXContainerItemProxy */ = { 37 | isa = PBXContainerItemProxy; 38 | containerPortal = 4ECBA0001C8434EA00884FCD /* Project object */; 39 | proxyType = 1; 40 | remoteGlobalIDString = 4ECBA0071C8434EA00884FCD; 41 | remoteInfo = SwiftResponsiveLabel; 42 | }; 43 | /* End PBXContainerItemProxy section */ 44 | 45 | /* Begin PBXFileReference section */ 46 | 4EB857BD1C8823CE00431BA9 /* SwiftResponsiveLabel+TruncationHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "SwiftResponsiveLabel+TruncationHandler.swift"; path = "Source/SwiftResponsiveLabel+TruncationHandler.swift"; sourceTree = ""; }; 47 | 4ECBA0081C8434EA00884FCD /* SwiftResponsiveLabel.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftResponsiveLabel.app; sourceTree = BUILT_PRODUCTS_DIR; }; 48 | 4ECBA0101C8434EA00884FCD /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 49 | 4ECBA0121C8434EA00884FCD /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 50 | 4ECBA0151C8434EA00884FCD /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 51 | 4ECBA0171C8434EA00884FCD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 52 | 4ECBA01C1C8434EA00884FCD /* SwiftResponsiveLabelTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftResponsiveLabelTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 53 | 4ECBA0201C8434EA00884FCD /* SwiftResponsiveLabelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftResponsiveLabelTests.swift; sourceTree = ""; }; 54 | 4ECBA0221C8434EA00884FCD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 55 | 4ECBA0271C8434EA00884FCD /* SwiftResponsiveLabelUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftResponsiveLabelUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 56 | 4ECBA02B1C8434EA00884FCD /* SwiftResponsiveLabelUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftResponsiveLabelUITests.swift; sourceTree = ""; }; 57 | 4ECBA02D1C8434EA00884FCD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 58 | 4ECBA03B1C8570D400884FCD /* TextKitStack.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TextKitStack.swift; path = Source/TextKitStack.swift; sourceTree = ""; }; 59 | 4ECBA03D1C85710500884FCD /* SwiftResponsiveLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftResponsiveLabel.swift; path = Source/SwiftResponsiveLabel.swift; sourceTree = ""; }; 60 | 4ECBA03F1C85913A00884FCD /* NSAttributedString+Processing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSAttributedString+Processing.swift"; path = "Source/NSAttributedString+Processing.swift"; sourceTree = ""; }; 61 | 4ECBA0411C85A14700884FCD /* TouchHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TouchHandler.swift; path = Source/TouchHandler.swift; sourceTree = ""; }; 62 | 4EF960E91C86E905007CB596 /* PatternDescriptor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PatternDescriptor.swift; path = Source/PatternDescriptor.swift; sourceTree = ""; }; 63 | 4EF960EA1C86E905007CB596 /* PatternHighlighter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PatternHighlighter.swift; path = Source/PatternHighlighter.swift; sourceTree = ""; }; 64 | 7808F24F1DE2D30D00D7F9DC /* InteractiveTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InteractiveTableViewController.swift; path = "Demo Project/InteractiveTableViewController.swift"; sourceTree = ""; }; 65 | 7808F2511DE2D37500D7F9DC /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = "Demo Project/AppDelegate.swift"; sourceTree = ""; }; 66 | 7808F2521DE2D37500D7F9DC /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MainViewController.swift; path = "Demo Project/MainViewController.swift"; sourceTree = ""; }; 67 | 7808F2551DE2D3B100D7F9DC /* InteractiveTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InteractiveTableViewCell.swift; path = "Demo Project/InteractiveTableViewCell.swift"; sourceTree = ""; }; 68 | /* End PBXFileReference section */ 69 | 70 | /* Begin PBXFrameworksBuildPhase section */ 71 | 4ECBA0051C8434EA00884FCD /* Frameworks */ = { 72 | isa = PBXFrameworksBuildPhase; 73 | buildActionMask = 2147483647; 74 | files = ( 75 | ); 76 | runOnlyForDeploymentPostprocessing = 0; 77 | }; 78 | 4ECBA0191C8434EA00884FCD /* Frameworks */ = { 79 | isa = PBXFrameworksBuildPhase; 80 | buildActionMask = 2147483647; 81 | files = ( 82 | ); 83 | runOnlyForDeploymentPostprocessing = 0; 84 | }; 85 | 4ECBA0241C8434EA00884FCD /* Frameworks */ = { 86 | isa = PBXFrameworksBuildPhase; 87 | buildActionMask = 2147483647; 88 | files = ( 89 | ); 90 | runOnlyForDeploymentPostprocessing = 0; 91 | }; 92 | /* End PBXFrameworksBuildPhase section */ 93 | 94 | /* Begin PBXGroup section */ 95 | 4ECB9FFF1C8434EA00884FCD = { 96 | isa = PBXGroup; 97 | children = ( 98 | 4ECBA00A1C8434EA00884FCD /* SwiftResponsiveLabel */, 99 | 4ECBA01F1C8434EA00884FCD /* SwiftResponsiveLabelTests */, 100 | 4ECBA02A1C8434EA00884FCD /* SwiftResponsiveLabelUITests */, 101 | 4ECBA0091C8434EA00884FCD /* Products */, 102 | ); 103 | sourceTree = ""; 104 | }; 105 | 4ECBA0091C8434EA00884FCD /* Products */ = { 106 | isa = PBXGroup; 107 | children = ( 108 | 4ECBA0081C8434EA00884FCD /* SwiftResponsiveLabel.app */, 109 | 4ECBA01C1C8434EA00884FCD /* SwiftResponsiveLabelTests.xctest */, 110 | 4ECBA0271C8434EA00884FCD /* SwiftResponsiveLabelUITests.xctest */, 111 | ); 112 | name = Products; 113 | sourceTree = ""; 114 | }; 115 | 4ECBA00A1C8434EA00884FCD /* SwiftResponsiveLabel */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | 4ECBA03A1C84351B00884FCD /* Source */, 119 | 4ECBA0391C84351000884FCD /* Demo Project */, 120 | 4ECBA0121C8434EA00884FCD /* Assets.xcassets */, 121 | 4ECBA0141C8434EA00884FCD /* LaunchScreen.storyboard */, 122 | 4ECBA0171C8434EA00884FCD /* Info.plist */, 123 | ); 124 | path = SwiftResponsiveLabel; 125 | sourceTree = ""; 126 | }; 127 | 4ECBA01F1C8434EA00884FCD /* SwiftResponsiveLabelTests */ = { 128 | isa = PBXGroup; 129 | children = ( 130 | 4ECBA0201C8434EA00884FCD /* SwiftResponsiveLabelTests.swift */, 131 | 4ECBA0221C8434EA00884FCD /* Info.plist */, 132 | ); 133 | path = SwiftResponsiveLabelTests; 134 | sourceTree = ""; 135 | }; 136 | 4ECBA02A1C8434EA00884FCD /* SwiftResponsiveLabelUITests */ = { 137 | isa = PBXGroup; 138 | children = ( 139 | 4ECBA02B1C8434EA00884FCD /* SwiftResponsiveLabelUITests.swift */, 140 | 4ECBA02D1C8434EA00884FCD /* Info.plist */, 141 | ); 142 | path = SwiftResponsiveLabelUITests; 143 | sourceTree = ""; 144 | }; 145 | 4ECBA0391C84351000884FCD /* Demo Project */ = { 146 | isa = PBXGroup; 147 | children = ( 148 | 7808F2511DE2D37500D7F9DC /* AppDelegate.swift */, 149 | 7808F2521DE2D37500D7F9DC /* MainViewController.swift */, 150 | 4ECBA00F1C8434EA00884FCD /* Main.storyboard */, 151 | 7808F24F1DE2D30D00D7F9DC /* InteractiveTableViewController.swift */, 152 | 7808F2551DE2D3B100D7F9DC /* InteractiveTableViewCell.swift */, 153 | ); 154 | name = "Demo Project"; 155 | sourceTree = ""; 156 | }; 157 | 4ECBA03A1C84351B00884FCD /* Source */ = { 158 | isa = PBXGroup; 159 | children = ( 160 | 4ECBA03D1C85710500884FCD /* SwiftResponsiveLabel.swift */, 161 | 4EF960E91C86E905007CB596 /* PatternDescriptor.swift */, 162 | 4EF960EA1C86E905007CB596 /* PatternHighlighter.swift */, 163 | 4ECBA03B1C8570D400884FCD /* TextKitStack.swift */, 164 | 4ECBA03F1C85913A00884FCD /* NSAttributedString+Processing.swift */, 165 | 4ECBA0411C85A14700884FCD /* TouchHandler.swift */, 166 | 4EB857BD1C8823CE00431BA9 /* SwiftResponsiveLabel+TruncationHandler.swift */, 167 | ); 168 | name = Source; 169 | sourceTree = ""; 170 | }; 171 | /* End PBXGroup section */ 172 | 173 | /* Begin PBXNativeTarget section */ 174 | 4ECBA0071C8434EA00884FCD /* SwiftResponsiveLabel */ = { 175 | isa = PBXNativeTarget; 176 | buildConfigurationList = 4ECBA0301C8434EA00884FCD /* Build configuration list for PBXNativeTarget "SwiftResponsiveLabel" */; 177 | buildPhases = ( 178 | 4ECBA0041C8434EA00884FCD /* Sources */, 179 | 4ECBA0051C8434EA00884FCD /* Frameworks */, 180 | 4ECBA0061C8434EA00884FCD /* Resources */, 181 | ); 182 | buildRules = ( 183 | ); 184 | dependencies = ( 185 | ); 186 | name = SwiftResponsiveLabel; 187 | productName = SwiftResponsiveLabel; 188 | productReference = 4ECBA0081C8434EA00884FCD /* SwiftResponsiveLabel.app */; 189 | productType = "com.apple.product-type.application"; 190 | }; 191 | 4ECBA01B1C8434EA00884FCD /* SwiftResponsiveLabelTests */ = { 192 | isa = PBXNativeTarget; 193 | buildConfigurationList = 4ECBA0331C8434EA00884FCD /* Build configuration list for PBXNativeTarget "SwiftResponsiveLabelTests" */; 194 | buildPhases = ( 195 | 4ECBA0181C8434EA00884FCD /* Sources */, 196 | 4ECBA0191C8434EA00884FCD /* Frameworks */, 197 | 4ECBA01A1C8434EA00884FCD /* Resources */, 198 | ); 199 | buildRules = ( 200 | ); 201 | dependencies = ( 202 | 4ECBA01E1C8434EA00884FCD /* PBXTargetDependency */, 203 | ); 204 | name = SwiftResponsiveLabelTests; 205 | productName = SwiftResponsiveLabelTests; 206 | productReference = 4ECBA01C1C8434EA00884FCD /* SwiftResponsiveLabelTests.xctest */; 207 | productType = "com.apple.product-type.bundle.unit-test"; 208 | }; 209 | 4ECBA0261C8434EA00884FCD /* SwiftResponsiveLabelUITests */ = { 210 | isa = PBXNativeTarget; 211 | buildConfigurationList = 4ECBA0361C8434EA00884FCD /* Build configuration list for PBXNativeTarget "SwiftResponsiveLabelUITests" */; 212 | buildPhases = ( 213 | 4ECBA0231C8434EA00884FCD /* Sources */, 214 | 4ECBA0241C8434EA00884FCD /* Frameworks */, 215 | 4ECBA0251C8434EA00884FCD /* Resources */, 216 | ); 217 | buildRules = ( 218 | ); 219 | dependencies = ( 220 | 4ECBA0291C8434EA00884FCD /* PBXTargetDependency */, 221 | ); 222 | name = SwiftResponsiveLabelUITests; 223 | productName = SwiftResponsiveLabelUITests; 224 | productReference = 4ECBA0271C8434EA00884FCD /* SwiftResponsiveLabelUITests.xctest */; 225 | productType = "com.apple.product-type.bundle.ui-testing"; 226 | }; 227 | /* End PBXNativeTarget section */ 228 | 229 | /* Begin PBXProject section */ 230 | 4ECBA0001C8434EA00884FCD /* Project object */ = { 231 | isa = PBXProject; 232 | attributes = { 233 | LastSwiftUpdateCheck = 0720; 234 | LastUpgradeCheck = 1020; 235 | ORGANIZATIONNAME = hsusmita.com; 236 | TargetAttributes = { 237 | 4ECBA0071C8434EA00884FCD = { 238 | CreatedOnToolsVersion = 7.2; 239 | DevelopmentTeam = HP3Q6K3LF3; 240 | LastSwiftMigration = 1020; 241 | }; 242 | 4ECBA01B1C8434EA00884FCD = { 243 | CreatedOnToolsVersion = 7.2; 244 | LastSwiftMigration = 1020; 245 | TestTargetID = 4ECBA0071C8434EA00884FCD; 246 | }; 247 | 4ECBA0261C8434EA00884FCD = { 248 | CreatedOnToolsVersion = 7.2; 249 | LastSwiftMigration = 1020; 250 | TestTargetID = 4ECBA0071C8434EA00884FCD; 251 | }; 252 | }; 253 | }; 254 | buildConfigurationList = 4ECBA0031C8434EA00884FCD /* Build configuration list for PBXProject "SwiftResponsiveLabel" */; 255 | compatibilityVersion = "Xcode 3.2"; 256 | developmentRegion = en; 257 | hasScannedForEncodings = 0; 258 | knownRegions = ( 259 | en, 260 | Base, 261 | ); 262 | mainGroup = 4ECB9FFF1C8434EA00884FCD; 263 | productRefGroup = 4ECBA0091C8434EA00884FCD /* Products */; 264 | projectDirPath = ""; 265 | projectRoot = ""; 266 | targets = ( 267 | 4ECBA0071C8434EA00884FCD /* SwiftResponsiveLabel */, 268 | 4ECBA01B1C8434EA00884FCD /* SwiftResponsiveLabelTests */, 269 | 4ECBA0261C8434EA00884FCD /* SwiftResponsiveLabelUITests */, 270 | ); 271 | }; 272 | /* End PBXProject section */ 273 | 274 | /* Begin PBXResourcesBuildPhase section */ 275 | 4ECBA0061C8434EA00884FCD /* Resources */ = { 276 | isa = PBXResourcesBuildPhase; 277 | buildActionMask = 2147483647; 278 | files = ( 279 | 4ECBA0161C8434EA00884FCD /* LaunchScreen.storyboard in Resources */, 280 | 4ECBA0131C8434EA00884FCD /* Assets.xcassets in Resources */, 281 | 4ECBA0111C8434EA00884FCD /* Main.storyboard in Resources */, 282 | ); 283 | runOnlyForDeploymentPostprocessing = 0; 284 | }; 285 | 4ECBA01A1C8434EA00884FCD /* Resources */ = { 286 | isa = PBXResourcesBuildPhase; 287 | buildActionMask = 2147483647; 288 | files = ( 289 | ); 290 | runOnlyForDeploymentPostprocessing = 0; 291 | }; 292 | 4ECBA0251C8434EA00884FCD /* Resources */ = { 293 | isa = PBXResourcesBuildPhase; 294 | buildActionMask = 2147483647; 295 | files = ( 296 | ); 297 | runOnlyForDeploymentPostprocessing = 0; 298 | }; 299 | /* End PBXResourcesBuildPhase section */ 300 | 301 | /* Begin PBXSourcesBuildPhase section */ 302 | 4ECBA0041C8434EA00884FCD /* Sources */ = { 303 | isa = PBXSourcesBuildPhase; 304 | buildActionMask = 2147483647; 305 | files = ( 306 | 4EF960EC1C86E905007CB596 /* PatternHighlighter.swift in Sources */, 307 | 4EF960EB1C86E905007CB596 /* PatternDescriptor.swift in Sources */, 308 | 4ECBA0421C85A14700884FCD /* TouchHandler.swift in Sources */, 309 | 7808F2531DE2D37500D7F9DC /* AppDelegate.swift in Sources */, 310 | 4ECBA0401C85913A00884FCD /* NSAttributedString+Processing.swift in Sources */, 311 | 7808F2501DE2D30D00D7F9DC /* InteractiveTableViewController.swift in Sources */, 312 | 4ECBA03E1C85710500884FCD /* SwiftResponsiveLabel.swift in Sources */, 313 | 7808F2561DE2D3B100D7F9DC /* InteractiveTableViewCell.swift in Sources */, 314 | 4ECBA03C1C8570D400884FCD /* TextKitStack.swift in Sources */, 315 | 4EB857BE1C8823CE00431BA9 /* SwiftResponsiveLabel+TruncationHandler.swift in Sources */, 316 | 7808F2541DE2D37500D7F9DC /* MainViewController.swift in Sources */, 317 | ); 318 | runOnlyForDeploymentPostprocessing = 0; 319 | }; 320 | 4ECBA0181C8434EA00884FCD /* Sources */ = { 321 | isa = PBXSourcesBuildPhase; 322 | buildActionMask = 2147483647; 323 | files = ( 324 | 4ECBA0211C8434EA00884FCD /* SwiftResponsiveLabelTests.swift in Sources */, 325 | ); 326 | runOnlyForDeploymentPostprocessing = 0; 327 | }; 328 | 4ECBA0231C8434EA00884FCD /* Sources */ = { 329 | isa = PBXSourcesBuildPhase; 330 | buildActionMask = 2147483647; 331 | files = ( 332 | 4ECBA02C1C8434EA00884FCD /* SwiftResponsiveLabelUITests.swift in Sources */, 333 | ); 334 | runOnlyForDeploymentPostprocessing = 0; 335 | }; 336 | /* End PBXSourcesBuildPhase section */ 337 | 338 | /* Begin PBXTargetDependency section */ 339 | 4ECBA01E1C8434EA00884FCD /* PBXTargetDependency */ = { 340 | isa = PBXTargetDependency; 341 | target = 4ECBA0071C8434EA00884FCD /* SwiftResponsiveLabel */; 342 | targetProxy = 4ECBA01D1C8434EA00884FCD /* PBXContainerItemProxy */; 343 | }; 344 | 4ECBA0291C8434EA00884FCD /* PBXTargetDependency */ = { 345 | isa = PBXTargetDependency; 346 | target = 4ECBA0071C8434EA00884FCD /* SwiftResponsiveLabel */; 347 | targetProxy = 4ECBA0281C8434EA00884FCD /* PBXContainerItemProxy */; 348 | }; 349 | /* End PBXTargetDependency section */ 350 | 351 | /* Begin PBXVariantGroup section */ 352 | 4ECBA00F1C8434EA00884FCD /* Main.storyboard */ = { 353 | isa = PBXVariantGroup; 354 | children = ( 355 | 4ECBA0101C8434EA00884FCD /* Base */, 356 | ); 357 | name = Main.storyboard; 358 | sourceTree = ""; 359 | }; 360 | 4ECBA0141C8434EA00884FCD /* LaunchScreen.storyboard */ = { 361 | isa = PBXVariantGroup; 362 | children = ( 363 | 4ECBA0151C8434EA00884FCD /* Base */, 364 | ); 365 | name = LaunchScreen.storyboard; 366 | sourceTree = ""; 367 | }; 368 | /* End PBXVariantGroup section */ 369 | 370 | /* Begin XCBuildConfiguration section */ 371 | 4ECBA02E1C8434EA00884FCD /* Debug */ = { 372 | isa = XCBuildConfiguration; 373 | buildSettings = { 374 | ALWAYS_SEARCH_USER_PATHS = NO; 375 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 376 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 377 | CLANG_CXX_LIBRARY = "libc++"; 378 | CLANG_ENABLE_MODULES = YES; 379 | CLANG_ENABLE_OBJC_ARC = YES; 380 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 381 | CLANG_WARN_BOOL_CONVERSION = YES; 382 | CLANG_WARN_COMMA = YES; 383 | CLANG_WARN_CONSTANT_CONVERSION = YES; 384 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 385 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 386 | CLANG_WARN_EMPTY_BODY = YES; 387 | CLANG_WARN_ENUM_CONVERSION = YES; 388 | CLANG_WARN_INFINITE_RECURSION = YES; 389 | CLANG_WARN_INT_CONVERSION = YES; 390 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 391 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 392 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 393 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 394 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 395 | CLANG_WARN_STRICT_PROTOTYPES = YES; 396 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 397 | CLANG_WARN_UNREACHABLE_CODE = YES; 398 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 399 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 400 | COPY_PHASE_STRIP = NO; 401 | DEBUG_INFORMATION_FORMAT = dwarf; 402 | ENABLE_STRICT_OBJC_MSGSEND = YES; 403 | ENABLE_TESTABILITY = YES; 404 | GCC_C_LANGUAGE_STANDARD = gnu99; 405 | GCC_DYNAMIC_NO_PIC = NO; 406 | GCC_NO_COMMON_BLOCKS = YES; 407 | GCC_OPTIMIZATION_LEVEL = 0; 408 | GCC_PREPROCESSOR_DEFINITIONS = ( 409 | "DEBUG=1", 410 | "$(inherited)", 411 | ); 412 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 413 | GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = NO; 414 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 415 | GCC_WARN_UNDECLARED_SELECTOR = YES; 416 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 417 | GCC_WARN_UNUSED_FUNCTION = YES; 418 | GCC_WARN_UNUSED_VARIABLE = YES; 419 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 420 | MTL_ENABLE_DEBUG_INFO = YES; 421 | ONLY_ACTIVE_ARCH = YES; 422 | SDKROOT = iphoneos; 423 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 424 | TARGETED_DEVICE_FAMILY = "1,2"; 425 | }; 426 | name = Debug; 427 | }; 428 | 4ECBA02F1C8434EA00884FCD /* Release */ = { 429 | isa = XCBuildConfiguration; 430 | buildSettings = { 431 | ALWAYS_SEARCH_USER_PATHS = NO; 432 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 433 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 434 | CLANG_CXX_LIBRARY = "libc++"; 435 | CLANG_ENABLE_MODULES = YES; 436 | CLANG_ENABLE_OBJC_ARC = YES; 437 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 438 | CLANG_WARN_BOOL_CONVERSION = YES; 439 | CLANG_WARN_COMMA = YES; 440 | CLANG_WARN_CONSTANT_CONVERSION = YES; 441 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 442 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 443 | CLANG_WARN_EMPTY_BODY = YES; 444 | CLANG_WARN_ENUM_CONVERSION = YES; 445 | CLANG_WARN_INFINITE_RECURSION = YES; 446 | CLANG_WARN_INT_CONVERSION = YES; 447 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 448 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 449 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 450 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 451 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 452 | CLANG_WARN_STRICT_PROTOTYPES = YES; 453 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 454 | CLANG_WARN_UNREACHABLE_CODE = YES; 455 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 456 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 457 | COPY_PHASE_STRIP = NO; 458 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 459 | ENABLE_NS_ASSERTIONS = NO; 460 | ENABLE_STRICT_OBJC_MSGSEND = YES; 461 | GCC_C_LANGUAGE_STANDARD = gnu99; 462 | GCC_NO_COMMON_BLOCKS = YES; 463 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 464 | GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = NO; 465 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 466 | GCC_WARN_UNDECLARED_SELECTOR = YES; 467 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 468 | GCC_WARN_UNUSED_FUNCTION = YES; 469 | GCC_WARN_UNUSED_VARIABLE = YES; 470 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 471 | MTL_ENABLE_DEBUG_INFO = NO; 472 | SDKROOT = iphoneos; 473 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 474 | TARGETED_DEVICE_FAMILY = "1,2"; 475 | VALIDATE_PRODUCT = YES; 476 | }; 477 | name = Release; 478 | }; 479 | 4ECBA0311C8434EA00884FCD /* Debug */ = { 480 | isa = XCBuildConfiguration; 481 | buildSettings = { 482 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 483 | DEVELOPMENT_TEAM = HP3Q6K3LF3; 484 | INFOPLIST_FILE = SwiftResponsiveLabel/Info.plist; 485 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 486 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 487 | PRODUCT_BUNDLE_IDENTIFIER = com.hsusmita.SwiftResponsiveLabel; 488 | PRODUCT_NAME = "$(TARGET_NAME)"; 489 | SWIFT_VERSION = 5.0; 490 | }; 491 | name = Debug; 492 | }; 493 | 4ECBA0321C8434EA00884FCD /* Release */ = { 494 | isa = XCBuildConfiguration; 495 | buildSettings = { 496 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 497 | DEVELOPMENT_TEAM = HP3Q6K3LF3; 498 | INFOPLIST_FILE = SwiftResponsiveLabel/Info.plist; 499 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 500 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 501 | PRODUCT_BUNDLE_IDENTIFIER = com.hsusmita.SwiftResponsiveLabel; 502 | PRODUCT_NAME = "$(TARGET_NAME)"; 503 | SWIFT_VERSION = 5.0; 504 | }; 505 | name = Release; 506 | }; 507 | 4ECBA0341C8434EA00884FCD /* Debug */ = { 508 | isa = XCBuildConfiguration; 509 | buildSettings = { 510 | BUNDLE_LOADER = "$(TEST_HOST)"; 511 | INFOPLIST_FILE = SwiftResponsiveLabelTests/Info.plist; 512 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 513 | PRODUCT_BUNDLE_IDENTIFIER = com.hsusmita.SwiftResponsiveLabelTests; 514 | PRODUCT_NAME = "$(TARGET_NAME)"; 515 | SWIFT_VERSION = 5.0; 516 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SwiftResponsiveLabel.app/SwiftResponsiveLabel"; 517 | }; 518 | name = Debug; 519 | }; 520 | 4ECBA0351C8434EA00884FCD /* Release */ = { 521 | isa = XCBuildConfiguration; 522 | buildSettings = { 523 | BUNDLE_LOADER = "$(TEST_HOST)"; 524 | INFOPLIST_FILE = SwiftResponsiveLabelTests/Info.plist; 525 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 526 | PRODUCT_BUNDLE_IDENTIFIER = com.hsusmita.SwiftResponsiveLabelTests; 527 | PRODUCT_NAME = "$(TARGET_NAME)"; 528 | SWIFT_VERSION = 5.0; 529 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SwiftResponsiveLabel.app/SwiftResponsiveLabel"; 530 | }; 531 | name = Release; 532 | }; 533 | 4ECBA0371C8434EA00884FCD /* Debug */ = { 534 | isa = XCBuildConfiguration; 535 | buildSettings = { 536 | INFOPLIST_FILE = SwiftResponsiveLabelUITests/Info.plist; 537 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 538 | PRODUCT_BUNDLE_IDENTIFIER = com.hsusmita.SwiftResponsiveLabelUITests; 539 | PRODUCT_NAME = "$(TARGET_NAME)"; 540 | SWIFT_VERSION = 5.0; 541 | TEST_TARGET_NAME = SwiftResponsiveLabel; 542 | USES_XCTRUNNER = YES; 543 | }; 544 | name = Debug; 545 | }; 546 | 4ECBA0381C8434EA00884FCD /* Release */ = { 547 | isa = XCBuildConfiguration; 548 | buildSettings = { 549 | INFOPLIST_FILE = SwiftResponsiveLabelUITests/Info.plist; 550 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 551 | PRODUCT_BUNDLE_IDENTIFIER = com.hsusmita.SwiftResponsiveLabelUITests; 552 | PRODUCT_NAME = "$(TARGET_NAME)"; 553 | SWIFT_VERSION = 5.0; 554 | TEST_TARGET_NAME = SwiftResponsiveLabel; 555 | USES_XCTRUNNER = YES; 556 | }; 557 | name = Release; 558 | }; 559 | /* End XCBuildConfiguration section */ 560 | 561 | /* Begin XCConfigurationList section */ 562 | 4ECBA0031C8434EA00884FCD /* Build configuration list for PBXProject "SwiftResponsiveLabel" */ = { 563 | isa = XCConfigurationList; 564 | buildConfigurations = ( 565 | 4ECBA02E1C8434EA00884FCD /* Debug */, 566 | 4ECBA02F1C8434EA00884FCD /* Release */, 567 | ); 568 | defaultConfigurationIsVisible = 0; 569 | defaultConfigurationName = Release; 570 | }; 571 | 4ECBA0301C8434EA00884FCD /* Build configuration list for PBXNativeTarget "SwiftResponsiveLabel" */ = { 572 | isa = XCConfigurationList; 573 | buildConfigurations = ( 574 | 4ECBA0311C8434EA00884FCD /* Debug */, 575 | 4ECBA0321C8434EA00884FCD /* Release */, 576 | ); 577 | defaultConfigurationIsVisible = 0; 578 | defaultConfigurationName = Release; 579 | }; 580 | 4ECBA0331C8434EA00884FCD /* Build configuration list for PBXNativeTarget "SwiftResponsiveLabelTests" */ = { 581 | isa = XCConfigurationList; 582 | buildConfigurations = ( 583 | 4ECBA0341C8434EA00884FCD /* Debug */, 584 | 4ECBA0351C8434EA00884FCD /* Release */, 585 | ); 586 | defaultConfigurationIsVisible = 0; 587 | defaultConfigurationName = Release; 588 | }; 589 | 4ECBA0361C8434EA00884FCD /* Build configuration list for PBXNativeTarget "SwiftResponsiveLabelUITests" */ = { 590 | isa = XCConfigurationList; 591 | buildConfigurations = ( 592 | 4ECBA0371C8434EA00884FCD /* Debug */, 593 | 4ECBA0381C8434EA00884FCD /* Release */, 594 | ); 595 | defaultConfigurationIsVisible = 0; 596 | defaultConfigurationName = Release; 597 | }; 598 | /* End XCConfigurationList section */ 599 | }; 600 | rootObject = 4ECBA0001C8434EA00884FCD /* Project object */; 601 | } 602 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel.xcodeproj/project.xcworkspace/xcuserdata/susmitahorrow.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hsusmita/SwiftResponsiveLabel/85ff82738b841c4cc9de354cd252914225894a13/SwiftResponsiveLabel.xcodeproj/project.xcworkspace/xcuserdata/susmitahorrow.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SwiftResponsiveLabel.xcodeproj/xcuserdata/susmitahorrow.xcuserdatad/xcschemes/SwiftResponsiveLabel.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel.xcodeproj/xcuserdata/susmitahorrow.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SwiftResponsiveLabel.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4ECBA0071C8434EA00884FCD 16 | 17 | primary 18 | 19 | 20 | 4ECBA01B1C8434EA00884FCD 21 | 22 | primary 23 | 24 | 25 | 4ECBA0261C8434EA00884FCD 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hsusmita/SwiftResponsiveLabel/85ff82738b841c4cc9de354cd252914225894a13/SwiftResponsiveLabel/.DS_Store -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "83.5x83.5", 66 | "scale" : "2x" 67 | } 68 | ], 69 | "info" : { 70 | "version" : 1, 71 | "author" : "xcode" 72 | } 73 | } -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Assets.xcassets/check.imageset/1436972523_check.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hsusmita/SwiftResponsiveLabel/85ff82738b841c4cc9de354cd252914225894a13/SwiftResponsiveLabel/Assets.xcassets/check.imageset/1436972523_check.png -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Assets.xcassets/check.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "1436972523_check.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 34 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 100 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 144 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 186 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 236 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Demo Project/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 29/02/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Demo Project/InteractiveTableViewCell.swift: -------------------------------------------------------------------------------- 1 | // 2 | // InteractiveTableViewCell.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 21/11/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | protocol InteractiveTableViewCellDelegate { 12 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnHashTag string: String) 13 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnUrl string: String) 14 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnUserHandle string: String) 15 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, shouldExpand expand: Bool) 16 | } 17 | 18 | extension InteractiveTableViewCellDelegate { 19 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnHashTag string: String){} 20 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnUrl string: String){} 21 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnUserHandle string: String){} 22 | } 23 | 24 | final class InteractiveTableViewCell: UITableViewCell { 25 | @IBOutlet weak var responsiveLabel: SwiftResponsiveLabel! 26 | static let cellIdentifier = "InteractiveTableViewCellIdentifier" 27 | var delegate: InteractiveTableViewCellDelegate? 28 | var collapseToken = "...Read Less" 29 | var expandToken = "...Read More" 30 | private var expandAttributedToken = NSMutableAttributedString(string: "") 31 | private var collapseAttributedToken = NSMutableAttributedString(string: "") 32 | 33 | override func awakeFromNib() { 34 | super.awakeFromNib() 35 | 36 | responsiveLabel.truncationToken = expandToken 37 | responsiveLabel.isUserInteractionEnabled = true 38 | 39 | // Handle Hashtag Detection 40 | let hashTagTapAction = PatternTapResponder(currentAction: { (tappedString) -> (Void) in 41 | self.delegate?.interactiveTableViewCell(self, didTapOnHashTag: tappedString) 42 | }) 43 | responsiveLabel.enableHashTagDetection(attributes: [ 44 | NSAttributedString.Key.foregroundColor: UIColor.red, 45 | NSAttributedString.Key.RLHighlightedBackgroundColor: UIColor.orange, 46 | NSAttributedString.Key.RLTapResponder: hashTagTapAction 47 | ]) 48 | 49 | // Handle URL Detection 50 | let urlTapAction = PatternTapResponder(currentAction: { (tappedString) -> (Void) in 51 | self.delegate?.interactiveTableViewCell(self, didTapOnUrl: tappedString) 52 | }) 53 | responsiveLabel.enableURLDetection(attributes: [ 54 | NSAttributedString.Key.foregroundColor: UIColor.brown, 55 | NSAttributedString.Key.RLTapResponder: urlTapAction 56 | ]) 57 | 58 | // Handle user handle Detection 59 | let userHandleTapAction = PatternTapResponder(currentAction: { (tappedString) -> (Void) in 60 | self.delegate?.interactiveTableViewCell(self, didTapOnUserHandle: tappedString) 61 | }) 62 | responsiveLabel.enableUserHandleDetection(attributes: [ 63 | NSAttributedString.Key.foregroundColor: UIColor.green, 64 | NSAttributedString.Key.RLHighlightedForegroundColor: UIColor.green, 65 | NSAttributedString.Key.RLHighlightedBackgroundColor: UIColor.black, 66 | NSAttributedString.Key.RLTapResponder: userHandleTapAction]) 67 | 68 | let tapResponder = PatternTapResponder(currentAction: { (tappedString) -> (Void) in 69 | self.delegate?.interactiveTableViewCell(self, shouldExpand: true) 70 | }) 71 | self.expandAttributedToken = NSMutableAttributedString(string: self.expandToken) 72 | self.expandAttributedToken.addAttributes([ 73 | NSAttributedString.Key.RLTapResponder: tapResponder, 74 | NSAttributedString.Key.foregroundColor: UIColor.blue, 75 | NSAttributedString.Key.font : responsiveLabel.font ?? UIFont.systemFont(ofSize: 10) 76 | ],range: NSRange(location: 0, length: self.expandAttributedToken.length)) 77 | 78 | self.collapseAttributedToken = NSMutableAttributedString(string: self.collapseToken) 79 | let collapseTapResponder = PatternTapResponder(currentAction: { (tappedString) -> (Void) in 80 | self.delegate?.interactiveTableViewCell(self, shouldExpand: false) 81 | }) 82 | 83 | self.collapseAttributedToken.addAttributes([ 84 | NSAttributedString.Key.foregroundColor: UIColor.blue, 85 | NSAttributedString.Key.font : responsiveLabel.font ?? UIFont.systemFont(ofSize: 10), 86 | NSAttributedString.Key.RLTapResponder: collapseTapResponder 87 | ], range: NSRange(location: 0, length: self.collapseAttributedToken.length)) 88 | } 89 | 90 | func configureText(_ str: String, forExpandedState isExpanded: Bool) { 91 | if (isExpanded) { 92 | let finalString = NSMutableAttributedString(string: str) 93 | finalString.append(self.collapseAttributedToken) 94 | finalString.addAttributes([NSAttributedString.Key.font : responsiveLabel.font ?? UIFont.systemFont(ofSize: 10)], range: NSRange(location: 0, length: finalString.length)) 95 | responsiveLabel.numberOfLines = 0 96 | responsiveLabel.customTruncationEnabled = false 97 | responsiveLabel.attributedText = finalString 98 | } else { 99 | responsiveLabel.customTruncationEnabled = true 100 | responsiveLabel.attributedTruncationToken = self.expandAttributedToken 101 | responsiveLabel.numberOfLines = 5 102 | responsiveLabel.text = str 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Demo Project/InteractiveTableViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // InteractiveTableViewController.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 21/11/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class InteractiveTableViewController: UIViewController { 12 | @IBOutlet weak var tableView: UITableView! 13 | var expandedPaths: [IndexPath] = [] 14 | var arrayOfTexts: [String] = ["An example of very long text having #hashtags with @username and URL http://www.google.com. Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."] 15 | // var arrayOfTexts: [String] = ["a\nb\nc\nd\n\\e\n\\f\n"] 16 | 17 | override func viewDidLoad() { 18 | super.viewDidLoad() 19 | tableView.estimatedRowHeight = 200.0 20 | tableView.rowHeight = UITableView.automaticDimension 21 | } 22 | } 23 | 24 | extension InteractiveTableViewController: UITableViewDataSource { 25 | func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 26 | return arrayOfTexts.count 27 | } 28 | 29 | func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 30 | let cell = tableView.dequeueReusableCell(withIdentifier: InteractiveTableViewCell.cellIdentifier, for: indexPath) as! InteractiveTableViewCell 31 | cell.configureText(arrayOfTexts[indexPath.row], forExpandedState: expandedPaths.contains(indexPath)) 32 | cell.delegate = self 33 | return cell 34 | } 35 | } 36 | 37 | extension InteractiveTableViewController: UITableViewDelegate { 38 | func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 39 | return UITableView.automaticDimension 40 | } 41 | } 42 | 43 | extension InteractiveTableViewController: InteractiveTableViewCellDelegate { 44 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, shouldExpand expand: Bool) { 45 | guard let indexPath = tableView.indexPath(for: cell) else { 46 | return 47 | } 48 | if expandedPaths.contains(indexPath) { 49 | expandedPaths.remove(at: indexPath.row) 50 | } else { 51 | expandedPaths.append(indexPath) 52 | } 53 | // self.tableView.reloadRows(at: [indexPath], with: .none) 54 | self.tableView.reloadData() 55 | } 56 | 57 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnHashTag string: String) { 58 | showAlertWithMessage("You have tapped on \(string)") 59 | } 60 | 61 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnUrl string: String) { 62 | showAlertWithMessage("You have tapped on \(string)") 63 | } 64 | 65 | func interactiveTableViewCell(_ cell: InteractiveTableViewCell, didTapOnUserHandle string: String) { 66 | showAlertWithMessage("You have tapped on \(string)") 67 | } 68 | 69 | func showAlertWithMessage(_ message: String) { 70 | let alertVC = UIAlertController(title: "", message: message, preferredStyle: .alert) 71 | let action = UIAlertAction(title: "OK", style: .default) { _ in 72 | alertVC.dismiss(animated: true, completion: nil) 73 | } 74 | alertVC.addAction(action) 75 | present(alertVC, animated: true, completion: nil) 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Demo Project/MainViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MainViewController.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by hsusmita on 02/03/16. 6 | // Copyright (c) 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class MainViewController: UIViewController { 12 | @IBOutlet weak var customLabel: SwiftResponsiveLabel! 13 | @IBOutlet weak var messageLabel: UILabel! 14 | @IBOutlet weak var segmentControl: UISegmentedControl! 15 | override func viewDidLoad() { 16 | super.viewDidLoad() 17 | customLabel.text = "Hello #hashtag @username some aaa more text www.google.com some more text some more text some more text hsusmita4@gmail.com" 18 | self.customLabel.enableStringDetection("text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red]) 19 | 20 | let regexString = "([a-z\\d])\\1\\1" 21 | do { 22 | let regex = try NSRegularExpression(pattern: regexString, options: .caseInsensitive) 23 | let descriptor = PatternDescriptor(regularExpression: regex, searchType: .first, patternAttributes: 24 | [NSAttributedString.Key.foregroundColor: UIColor.green, 25 | NSAttributedString.Key.RLHighlightedForegroundColor: UIColor.green, 26 | NSAttributedString.Key.RLHighlightedBackgroundColor: UIColor.black 27 | ]) 28 | customLabel.enablePatternDetection(patternDescriptor: descriptor) 29 | } catch let error as NSError { 30 | print("NSRegularExpression Error: \(error.debugDescription)") 31 | } 32 | 33 | self.segmentControl.selectedSegmentIndex = 0 34 | self.handleSegmentChange(segmentControl) 35 | } 36 | 37 | @IBAction func enableHashTagButton(_ sender:UIButton) { 38 | sender.isSelected = !sender.isSelected 39 | if sender.isSelected { 40 | let hashTagTapAction = PatternTapResponder {(tappedString)-> (Void) in 41 | let messageString = "You have tapped hashTag:" + tappedString 42 | self.messageLabel.text = messageString 43 | } 44 | let highlightedAttributes = [NSAttributedString.Key.foregroundColor : UIColor.red, 45 | NSAttributedString.Key.backgroundColor : UIColor.black] 46 | let patternAttributes: AttributesDictionary = [ 47 | NSAttributedString.Key.RLHighlightedAttributesDictionary : highlightedAttributes, 48 | NSAttributedString.Key.foregroundColor: UIColor.cyan, 49 | NSAttributedString.Key.RLTapResponder: hashTagTapAction] 50 | customLabel.enableHashTagDetection(attributes: patternAttributes) 51 | } else { 52 | customLabel.disableHashTagDetection() 53 | } 54 | } 55 | 56 | 57 | @IBAction func enableUserhandleButton(_ sender:UIButton) { 58 | sender.isSelected = !sender.isSelected 59 | if sender.isSelected { 60 | let userHandleTapAction = PatternTapResponder{ (tappedString)-> (Void) in 61 | let messageString = "You have tapped user handle:" + tappedString 62 | self.messageLabel.text = messageString 63 | } 64 | let dict = [NSAttributedString.Key.foregroundColor : UIColor.green, 65 | NSAttributedString.Key.backgroundColor:UIColor.black] 66 | self.customLabel.enableUserHandleDetection(attributes: [ 67 | NSAttributedString.Key.foregroundColor: UIColor.gray, 68 | NSAttributedString.Key.RLHighlightedAttributesDictionary: dict, 69 | NSAttributedString.Key.RLTapResponder:userHandleTapAction 70 | ]) 71 | }else { 72 | customLabel.disableUserHandleDetection() 73 | } 74 | } 75 | 76 | @IBAction func enableURLButton(_ sender:UIButton) { 77 | sender.isSelected = !sender.isSelected 78 | if sender.isSelected { 79 | let URLTapAction = PatternTapResponder{(tappedString)-> (Void) in 80 | let messageString = "You have tapped URL: " + tappedString 81 | self.messageLabel.text = messageString 82 | } 83 | self.customLabel.enableURLDetection(attributes: [ 84 | NSAttributedString.Key.foregroundColor: UIColor.blue, 85 | NSAttributedString.Key.RLTapResponder: URLTapAction 86 | ]) 87 | } else { 88 | self.customLabel.disableURLDetection() 89 | } 90 | } 91 | 92 | @IBAction func handleSegmentChange(_ sender:UISegmentedControl) { 93 | switch(segmentControl.selectedSegmentIndex) { 94 | case 0: 95 | let action = PatternTapResponder {(tappedString)-> (Void) in 96 | let messageString = "You have tapped token string" 97 | self.messageLabel.text = messageString} 98 | let dict: AttributesDictionary = [ 99 | NSAttributedString.Key.RLHighlightedBackgroundColor:UIColor.black, 100 | NSAttributedString.Key.RLHighlightedForegroundColor:UIColor.green, 101 | NSAttributedString.Key.RLTapResponder:action 102 | ] 103 | let token = NSAttributedString(string: "...More", 104 | attributes: [NSAttributedString.Key.font:self.customLabel.font ?? UIFont.systemFont(ofSize: 10), 105 | NSAttributedString.Key.foregroundColor:UIColor.brown, 106 | NSAttributedString.Key.RLHighlightedAttributesDictionary: dict]) 107 | customLabel.attributedTruncationToken = token 108 | 109 | case 1: 110 | customLabel.truncationToken = "...Load More" 111 | case 2: 112 | customLabel.truncationIndicatorImage = UIImage(named: "check") 113 | 114 | default: 115 | break 116 | } 117 | } 118 | 119 | @IBAction func enableTruncationUIButton(_ sender: UIButton) { 120 | sender.isSelected = !sender.isSelected 121 | customLabel.customTruncationEnabled = sender.isSelected 122 | self.handleSegmentChange(self.segmentControl) 123 | } 124 | } 125 | 126 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Source/NSAttributedString+Processing.swift: -------------------------------------------------------------------------------- 1 | // 2 | // NSAttributedString+Processing.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 01/03/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | extension NSAttributedString.Key { 13 | public static let RLTapResponder = NSAttributedString.Key("TapResponder") 14 | public static let RLHighlightedForegroundColor = NSAttributedString.Key("HighlightedForegroundColor") 15 | public static let RLHighlightedBackgroundColor = NSAttributedString.Key("HighlightedBackgroundColor") 16 | public static let RLBackgroundCornerRadius = NSAttributedString.Key("HighlightedBackgroundCornerRadius") 17 | public static let RLHighlightedAttributesDictionary = NSAttributedString.Key("HighlightedAttributes") 18 | } 19 | 20 | open class PatternTapResponder { 21 | let action: (String) -> Void 22 | 23 | public init(currentAction: @escaping (_ tappedString: String) -> (Void)) { 24 | action = currentAction 25 | } 26 | 27 | open func perform(_ string: String) { 28 | action(string) 29 | } 30 | } 31 | 32 | extension NSAttributedString { 33 | 34 | func sizeOfText() -> CGSize { 35 | var range = NSMakeRange(NSNotFound, 0) 36 | let fontAttributes = self.attributes(at: 0, longestEffectiveRange: &range, 37 | in: NSRange(location: 0, length: self.length)) 38 | var size = (self.string as NSString).size(withAttributes: fontAttributes) 39 | self.enumerateAttribute(NSAttributedString.Key.attachment, 40 | in: NSRange(location: 0, length: self.length), 41 | options: []) { (value, range, stop) in 42 | if let attachment = value as? NSTextAttachment { 43 | size.width += attachment.bounds.width 44 | } 45 | } 46 | return size 47 | } 48 | 49 | func isNewLinePresent() -> Bool { 50 | let newLineRange = self.string.rangeOfCharacter(from: CharacterSet.newlines) 51 | return newLineRange?.lowerBound != newLineRange?.upperBound 52 | } 53 | 54 | /** 55 | Setup paragraph alignement properly. 56 | Interface builder applies line break style to the attributed string. This makes text container break at first line of text. So we need to set the line break to wrapping. 57 | IB only allows a single paragraph so getting the style of the first character is fine. 58 | */ 59 | func wordWrappedAttributedString() -> NSAttributedString { 60 | var processedString = self 61 | if (self.string.count > 0) { 62 | let rangePointer: NSRangePointer? = nil 63 | if let paragraphStyle: NSParagraphStyle = self.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: rangePointer) as? NSParagraphStyle, 64 | let mutableParagraphStyle = paragraphStyle.mutableCopy() as? NSMutableParagraphStyle { 65 | 66 | // Remove the line breaks 67 | mutableParagraphStyle.lineBreakMode = .byWordWrapping 68 | 69 | // Apply new style 70 | let restyled = NSMutableAttributedString(attributedString: self) 71 | restyled.addAttribute(NSAttributedString.Key.paragraphStyle, value: mutableParagraphStyle, range: NSMakeRange(0, restyled.length)) 72 | processedString = restyled 73 | } 74 | } 75 | return processedString 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Source/PatternDescriptor.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PatternDescriptor.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 02/03/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | /** 12 | Type of Pattern Search 13 | 14 | All : All matching patterns 15 | 16 | First: First matching pattern 17 | 18 | Last: Last matching pattern 19 | */ 20 | public enum PatternSearchType:Int { 21 | case all, first, last 22 | } 23 | 24 | /** 25 | PatternDescriptor Struct encapsulates following information regarding pattern to be matched 26 | 27 | Regular Expression for the pattern : NSRegularExpression 28 | 29 | Type of pattern search : PatternSearchType 30 | 31 | Attributes for the pattern : [String: NSObject] 32 | */ 33 | public typealias AttributesDictionary = [NSAttributedString.Key: Any] 34 | 35 | public struct PatternDescriptor { 36 | let searchType : PatternSearchType 37 | let patternAttributes : AttributesDictionary 38 | let patternExpression : NSRegularExpression 39 | 40 | /** 41 | - parameters: 42 | - regularExpression: An NSRegularExpression which describes the pattern 43 | - searchType: PatternSearchType 44 | - patternAttributes: AttributesDictionary 45 | - returns: 46 | An instance of pattern descriptor 47 | */ 48 | public init(regularExpression: NSRegularExpression, 49 | searchType: PatternSearchType, 50 | patternAttributes: AttributesDictionary) { 51 | self.patternExpression = regularExpression 52 | self.searchType = searchType 53 | self.patternAttributes = patternAttributes 54 | } 55 | 56 | /** 57 | Returns a pattern descriptor 58 | - parameters: 59 | - dataDetector: NSDataDetector 60 | - searchType: PatternSearchType 61 | - patternAttributes: AttributesDictionary 62 | - returns: 63 | An instance of pattern descriptor 64 | */ 65 | public init(dataDetector: NSDataDetector, 66 | searchType: PatternSearchType, 67 | patternAttributes: AttributesDictionary) { 68 | self.patternExpression = dataDetector 69 | self.searchType = searchType 70 | self.patternAttributes = patternAttributes 71 | } 72 | 73 | /** 74 | Generates array of ranges for the matches found in given string, based on current search type. 75 | 76 | If searchType = .All, all the matches are returned 77 | 78 | If searchType = .First, the array contains only the first found range 79 | 80 | if searchType = .Last, the array contains only the last found range 81 | 82 | - parameters: 83 | - string: Input String 84 | - returns: 85 | An array of NSRange 86 | */ 87 | public func patternRangesForString(_ string:String) -> [NSRange] { 88 | switch(self.searchType) { 89 | 90 | case .all: 91 | return allMatchingPattern(string) 92 | 93 | case .first: 94 | return [firstMatchingPattern(string)] 95 | 96 | case .last: 97 | return [allMatchingPattern(string)].last! 98 | } 99 | } 100 | 101 | // MARK: - Private Helpers 102 | 103 | fileprivate func allMatchingPattern(_ string:String) -> [NSRange] { 104 | var generatedRanges = [NSRange]() 105 | self.patternExpression.enumerateMatches(in: string, options: .reportCompletion, range: NSMakeRange(0, string.count)){ 106 | (result, flag, stop) -> Void in 107 | if let result = result { 108 | generatedRanges.append(result.range) 109 | } 110 | } 111 | 112 | return generatedRanges 113 | } 114 | 115 | fileprivate func firstMatchingPattern(_ string:String) -> NSRange { 116 | return self.patternExpression.rangeOfFirstMatch(in: string, options: .reportProgress, range: NSMakeRange(0, string.count)) 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Source/PatternHighlighter.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PatternHighlighter.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 02/03/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | /** This class is reponsible for finding patterns and applying attributes to those patterns 12 | */ 13 | 14 | open class PatternHighlighter { 15 | static let RegexStringForHashTag = "(? String { 86 | let key:String 87 | if patternDescriptor.patternExpression.isKind(of: NSDataDetector.self) { 88 | let types = (patternDescriptor.patternExpression as! NSDataDetector).checkingTypes 89 | key = String(types) 90 | }else { 91 | key = patternDescriptor.patternExpression.pattern; 92 | } 93 | return key 94 | } 95 | 96 | fileprivate func removePatternAttributes(_ patternDescriptor: PatternDescriptor) { 97 | guard let attributedText = self.attributedText else { 98 | return 99 | } 100 | //Generate ranges for current text of textStorage 101 | let patternRanges = patternDescriptor.patternRangesForString(attributedText.string) 102 | for range in patternRanges { //Remove attributes from the ranges conditionally 103 | for (name, _) in patternDescriptor.patternAttributes { 104 | attributedText.removeAttribute(name, range: range) 105 | } 106 | } 107 | } 108 | 109 | fileprivate func addPatternAttributes(_ patternDescriptor: PatternDescriptor) { 110 | guard let attributedText = self.patternHighlightedText else { 111 | return 112 | } 113 | //Generate ranges for attributed text of the label 114 | let patternRanges = patternDescriptor.patternRangesForString(attributedText.string) 115 | for range in patternRanges { //Apply attributes to the ranges conditionally 116 | attributedText.addAttributes(patternDescriptor.patternAttributes, range: range) 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Source/SwiftResponsiveLabel+TruncationHandler.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TruncationHandler.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 03/03/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | class InlineTextAttachment : NSTextAttachment { 13 | var fontDescender: CGFloat? 14 | 15 | override func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect { 16 | var superRect = super.attachmentBounds(for: textContainer, proposedLineFragment: lineFrag, glyphPosition: position, characterIndex: charIndex) 17 | superRect.origin.y = self.fontDescender ?? 0 18 | return superRect 19 | } 20 | } 21 | 22 | extension SwiftResponsiveLabel { 23 | func stringWithTruncationToken() -> NSAttributedString? { 24 | let currentText = self.attributedTextToDisplay 25 | guard let truncationToken = self.attributedTruncationToken, currentText.string.isEmpty == false else { 26 | return nil 27 | } 28 | let range = self.textKitStack.rangeForTokenInsertion(truncationToken) 29 | guard range.location != NSNotFound && range.location > 0 && (range.location + range.length) <= currentText.length else { 30 | return nil 31 | } 32 | var attributedString = NSMutableAttributedString() 33 | attributedString = NSMutableAttributedString(attributedString: currentText) 34 | attributedString.replaceCharacters(in: range, with: truncationToken) 35 | return attributedString 36 | } 37 | 38 | func truncationTokenAppended() -> Bool { 39 | guard let token = self.attributedTruncationToken else { 40 | return false 41 | } 42 | return self.textKitStack.rangeOfString(token.string).location != NSNotFound 43 | } 44 | 45 | func attributedStringWithImage(_ image: UIImage, withSize size: CGSize, andAction action: PatternTapResponder?) -> NSAttributedString { 46 | let textAttachment = InlineTextAttachment() 47 | textAttachment.image = image 48 | textAttachment.fontDescender = self.font.descender; 49 | textAttachment.bounds = CGRect(x: 0, y: -self.font.descender - self.font.lineHeight/2, width: size.width, height: size.height) 50 | let imageAttributedString = NSAttributedString(attachment: textAttachment) 51 | let paddingString = NSAttributedString(string: " ") 52 | let finalString = NSMutableAttributedString(attributedString: paddingString) 53 | finalString.append(imageAttributedString) 54 | finalString.append(paddingString) 55 | if let action = action { 56 | finalString.addAttribute(NSAttributedString.Key.RLTapResponder, value: action, range: NSMakeRange(0, finalString.length)) 57 | } 58 | return finalString 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Source/SwiftResponsiveLabel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SwiftResponsiveLabel.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 01/03/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | @IBDesignable 13 | open class SwiftResponsiveLabel: UILabel { 14 | var textKitStack = TextKitStack() 15 | var touchHandler: TouchHandler? 16 | var patternHighlighter = PatternHighlighter() 17 | 18 | override init(frame: CGRect) { 19 | super.init(frame: frame) 20 | self.attributedTruncationToken = NSAttributedString(string: truncationToken, attributes: self.attributesFromProperties) 21 | } 22 | 23 | required public init?(coder aDecoder: NSCoder) { 24 | super.init(coder: aDecoder) 25 | self.attributedTruncationToken = NSAttributedString(string: truncationToken, attributes: self.attributesFromProperties) 26 | } 27 | 28 | override open var frame: CGRect { 29 | didSet { 30 | self.textKitStack.resizeTextContainer(frame.size) 31 | } 32 | } 33 | 34 | override open var bounds: CGRect { 35 | didSet { 36 | self.textKitStack.resizeTextContainer(bounds.size) 37 | } 38 | } 39 | 40 | override open var preferredMaxLayoutWidth: CGFloat { 41 | didSet { 42 | self.textKitStack.resizeTextContainer(frame.size) 43 | } 44 | } 45 | 46 | override open var text: String? { 47 | didSet { 48 | self.textKitStack.updateTextStorage(self.attributedTextToDisplay) 49 | setNeedsDisplay() 50 | } 51 | } 52 | 53 | override open var attributedText: NSAttributedString? { 54 | didSet { 55 | self.textKitStack.updateTextStorage(self.attributedTextToDisplay) 56 | setNeedsDisplay() 57 | } 58 | } 59 | 60 | override open var numberOfLines: Int { 61 | didSet { 62 | let rect = self.textKitStack.rectFittingTextForContainerSize(self.bounds.size, numberOfLines: self.numberOfLines, font: self.font) 63 | self.textKitStack.resizeTextContainer(rect.size) 64 | } 65 | } 66 | 67 | 68 | /** This boolean determines if custom truncation token should be added 69 | */ 70 | 71 | @IBInspectable open var customTruncationEnabled: Bool = true { 72 | didSet { 73 | self.textKitStack.updateTextStorage(self.attributedTextToDisplay) 74 | self.setNeedsDisplay() 75 | } 76 | } 77 | 78 | /** Custom truncation token string. The default value is "..." 79 | 80 | If customTruncationEnabled is true, then this text will be seen while truncation in place of default ellipse 81 | */ 82 | @IBInspectable open var truncationToken: String = "..." { 83 | didSet { 84 | self.attributedTruncationToken = NSAttributedString(string: truncationToken, attributes: self.attributesFromProperties) 85 | } 86 | } 87 | 88 | /** Custom truncation token atributed string. The default value is "..." 89 | 90 | If customTruncationEnabled is true, then this text will be seen while truncation in place of default ellipse 91 | */ 92 | @IBInspectable open var attributedTruncationToken: NSAttributedString? { 93 | didSet { 94 | if let _ = self.attributedTruncationToken { 95 | self.textKitStack.updateTextStorage(self.attributedTextToDisplay) 96 | self.setNeedsDisplay() 97 | } 98 | } 99 | } 100 | 101 | @IBInspectable open var truncationIndicatorImage: UIImage? { 102 | didSet { 103 | if let image = truncationIndicatorImage { 104 | self.attributedTruncationToken = self.attributedStringWithImage(image, withSize: CGSize(width: 20.0, height: 20.0), andAction: nil) 105 | } 106 | } 107 | } 108 | 109 | fileprivate var attributesFromProperties: AttributesDictionary { 110 | let shadow = NSShadow() 111 | if let shadowColor = self.shadowColor { 112 | shadow.shadowColor = shadowColor 113 | shadow.shadowOffset = self.shadowOffset 114 | } else { 115 | shadow.shadowOffset = CGSize(width: 0, height: -1) 116 | shadow.shadowColor = nil 117 | } 118 | 119 | var color = self.textColor 120 | if !self.isEnabled { 121 | color = UIColor.lightGray 122 | } else if let _ = self.highlightedTextColor, self.isHighlighted == true { 123 | color = self.highlightedTextColor; 124 | } 125 | 126 | let paragraph = NSMutableParagraphStyle() 127 | paragraph.alignment = self.textAlignment 128 | 129 | return [NSAttributedString.Key.font : self.font ?? UIFont.systemFont(ofSize: 10), 130 | NSAttributedString.Key.foregroundColor : color!, 131 | NSAttributedString.Key.shadow: shadow, 132 | NSAttributedString.Key.paragraphStyle: paragraph] 133 | } 134 | 135 | 136 | open var attributedTextToDisplay: NSAttributedString { 137 | var finalAttributedString = NSAttributedString() 138 | if let attributedText = attributedText?.wordWrappedAttributedString() { 139 | finalAttributedString = NSAttributedString(attributedString: attributedText) 140 | } else { 141 | finalAttributedString = NSAttributedString(string: text ?? "", attributes: self.attributesFromProperties) 142 | } 143 | return finalAttributedString 144 | } 145 | 146 | // MARK: Override methods from Superclass 147 | 148 | override open func awakeFromNib() { 149 | super.awakeFromNib() 150 | self.initialTextConfiguration() 151 | if isUserInteractionEnabled { 152 | self.touchHandler = TouchHandler(responsiveLabel: self) 153 | } 154 | } 155 | 156 | override open func drawText(in rect: CGRect) { 157 | // Add truncation token if necessary 158 | var finalString: NSAttributedString = textKitStack.currentAttributedText 159 | if let _ = self.attributedTruncationToken, self.shouldTruncate() && self.customTruncationEnabled { 160 | if let string = self.stringWithTruncationToken(), self.truncationTokenAppended() == false { 161 | finalString = string 162 | } 163 | } 164 | // Apply pattern 165 | self.patternHighlighter.updateAttributedText(finalString) 166 | if let highlightedString = self.patternHighlighter.patternHighlightedText { 167 | finalString = highlightedString 168 | } 169 | textKitStack.updateTextStorage(finalString) 170 | self.textKitStack.drawText(self.textOffSet(rect)) 171 | } 172 | 173 | override open func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect { 174 | let rect = self.textKitStack.rectFittingTextForContainerSize(bounds.size, numberOfLines: self.numberOfLines, font: self.font) 175 | self.textKitStack.resizeTextContainer(rect.size) 176 | return rect 177 | } 178 | 179 | // MARK: Public methods 180 | 181 | /** Method to set an image as truncation indicator 182 | - parameters: 183 | - image: UIImage 184 | - size: CGSize : The height of image size should be approximately equal to or less than the font height. Otherwise the image will not be rendered properly 185 | - action: PatternTapResponder action to be performed on tap on the image 186 | */ 187 | func setTruncationIndicatorImage(_ image: UIImage, withSize size: CGSize, andAction action: PatternTapResponder?) { 188 | let attributedString = self.attributedStringWithImage(image, withSize: size, andAction: action) 189 | self.attributedTruncationToken = attributedString 190 | } 191 | 192 | /** Add attributes to all the occurences of pattern dictated by pattern descriptor 193 | - parameters: 194 | - patternDescriptor: The descriptor for the pattern to be detected 195 | */ 196 | open func enablePatternDetection(patternDescriptor: PatternDescriptor) { 197 | self.patternHighlighter.enablePatternDetection(patternDescriptor) 198 | self.textKitStack.updateTextStorage(self.attributedTextToDisplay) 199 | self.setNeedsDisplay() 200 | } 201 | 202 | /** Add given attributes to urls 203 | - parameters: 204 | - attributes: AttributesDictionary 205 | */ 206 | open func enableURLDetection(attributes: AttributesDictionary) { 207 | do { 208 | let regex = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) 209 | let descriptor = PatternDescriptor(regularExpression: regex, searchType: .all, patternAttributes: attributes) 210 | self.enablePatternDetection(patternDescriptor: descriptor) 211 | } catch let error as NSError { 212 | print("NSDataDetector Error: \(error.debugDescription)") 213 | } 214 | } 215 | 216 | /** Add given attributes to user handles 217 | - parameters: 218 | - attributes: AttributesDictionary 219 | */ 220 | open func enableUserHandleDetection(attributes: AttributesDictionary) { 221 | self.highlightPattern(PatternHighlighter.RegexStringForUserHandle, attributes: attributes) 222 | } 223 | 224 | /** Add given attributes to hastags 225 | - parameters: 226 | - attributes: AttributesDictionary 227 | */ 228 | open func enableHashTagDetection(attributes: AttributesDictionary) { 229 | self.highlightPattern(PatternHighlighter.RegexStringForHashTag, attributes: attributes) 230 | } 231 | 232 | /** Add given attributes to the occurrences of given string 233 | - parameters: 234 | - string: String 235 | - attributes: AttributesDictionary 236 | */ 237 | open func enableStringDetection(_ string: String, attributes: AttributesDictionary) { 238 | let pattern = String(format: PatternHighlighter.RegexFormatForSearchWord, string) 239 | self.highlightPattern(pattern, attributes: attributes) 240 | } 241 | 242 | /** Add given attributes to the occurrences of all the strings of given array 243 | - parameters: 244 | - stringsArray: [String] 245 | - attributes: AttributesDictionary 246 | */ 247 | open func enableDetectionForStrings(_ stringsArray: [String], attributes: AttributesDictionary) { 248 | for string in stringsArray { 249 | enableStringDetection(string, attributes: attributes) 250 | } 251 | } 252 | 253 | /** Removes previously applied attributes from all the occurences of pattern dictated by pattern descriptor 254 | - parameters: 255 | - patternDescriptor: The descriptor for the pattern to be detected 256 | */ 257 | open func disablePatternDetection(_ patternDescriptor: PatternDescriptor) { 258 | self.patternHighlighter.disablePatternDetection(patternDescriptor) 259 | self.textKitStack.updateTextStorage(self.attributedTextToDisplay) 260 | self.setNeedsLayout() 261 | } 262 | 263 | /** remove attributes form url 264 | */ 265 | open func disableURLDetection() { 266 | let key = String(NSTextCheckingResult.CheckingType.link.rawValue) 267 | self.unhighlightPattern(key) 268 | } 269 | 270 | /** remove attributes form user handle 271 | */ 272 | open func disableUserHandleDetection() { 273 | self.unhighlightPattern(PatternHighlighter.RegexStringForUserHandle) 274 | } 275 | 276 | /** remove attributes form hash tags 277 | */ 278 | open func disableHashTagDetection() { 279 | self.unhighlightPattern(PatternHighlighter.RegexStringForHashTag) 280 | } 281 | 282 | /** Remove attributes from all the occurrences of given string 283 | - parameters: 284 | - string: String 285 | */ 286 | open func disableStringDetection(_ string: String) { 287 | let pattern = String(format: PatternHighlighter.RegexFormatForSearchWord, string) 288 | self.unhighlightPattern(pattern) 289 | } 290 | 291 | /** Remove attributes from all the occurrences of all the strings in the array 292 | - parameters: 293 | - string: [String] 294 | */ 295 | open func disableDetectionForStrings(_ stringsArray:[String]) { 296 | for string in stringsArray { 297 | disableStringDetection(string) 298 | } 299 | } 300 | 301 | // MARK: Private Helpers 302 | 303 | fileprivate func highlightPattern(_ pattern: String, attributes: AttributesDictionary) { 304 | patternHighlighter.highlightPattern(pattern, dictionary: attributes) 305 | self.textKitStack.updateTextStorage(self.attributedTextToDisplay) 306 | self.setNeedsDisplay() 307 | } 308 | 309 | fileprivate func unhighlightPattern(_ pattern: String) { 310 | self.patternHighlighter.unhighlightPattern(regexString: pattern) 311 | self.textKitStack.updateTextStorage(self.attributedTextToDisplay) 312 | self.setNeedsDisplay() 313 | } 314 | 315 | internal func shouldTruncate() -> Bool { 316 | guard numberOfLines > 0 else { 317 | return false 318 | } 319 | let range = self.textKitStack.rangeForTokenInsertion(self.attributedTextToDisplay) 320 | return (range.location + range.length <= self.attributedTextToDisplay.length) 321 | } 322 | 323 | fileprivate func textOffSet(_ rect: CGRect) -> CGPoint { 324 | var textOffset = CGPoint.zero 325 | let textBounds = self.textKitStack.boundingRectForCompleteText() 326 | let paddingHeight = (rect.size.height - textBounds.size.height) / 2.0 327 | if paddingHeight > 0 { 328 | textOffset.y = paddingHeight 329 | } else { 330 | textOffset.y = 0 331 | } 332 | return textOffset 333 | } 334 | 335 | fileprivate func initialTextConfiguration() { 336 | var currentText = NSAttributedString() 337 | if let attributedText = self.attributedText { 338 | currentText = attributedText.wordWrappedAttributedString() 339 | } else if let text = self.text { 340 | currentText = NSAttributedString(string: text, attributes: self.attributesFromProperties) 341 | } 342 | self.textKitStack.updateTextStorage(currentText) 343 | } 344 | } 345 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Source/TextKitStack.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TextKitStack.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 01/03/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | public struct RangeAttribute { 13 | let key: NSAttributedString.Key 14 | let attribute: Any? 15 | let range: NSRange 16 | } 17 | 18 | open class TextKitStack { 19 | fileprivate var textContainer = NSTextContainer() 20 | fileprivate var layoutManager = NSLayoutManager() 21 | fileprivate var textStorage = NSTextStorage() 22 | fileprivate var currentTextOffset = CGPoint.zero 23 | 24 | open var textStorageLength: Int { 25 | return self.textStorage.length 26 | } 27 | 28 | open var currentAttributedText: NSAttributedString { 29 | return textStorage 30 | } 31 | 32 | open var numberOflines: Int = 0 { 33 | didSet { 34 | self.textContainer.maximumNumberOfLines = self.numberOflines 35 | } 36 | } 37 | 38 | init() { 39 | self.textContainer.widthTracksTextView = true 40 | self.textContainer.heightTracksTextView = true 41 | self.layoutManager.addTextContainer(self.textContainer) 42 | self.textStorage.addLayoutManager(self.layoutManager) 43 | } 44 | 45 | /** Draws text in textStorage starting at point specified by textOffset 46 | - parameters: 47 | - textOffset: CGPoint 48 | */ 49 | open func drawText(_ textOffset: CGPoint) { 50 | self.currentTextOffset = textOffset 51 | let glyphRange = self.layoutManager.glyphRange(for: self.textContainer) 52 | self.layoutManager.drawBackground(forGlyphRange: glyphRange, at: textOffset) 53 | self.layoutManager.drawGlyphs(forGlyphRange: glyphRange, at: textOffset) 54 | } 55 | 56 | /** Resizes textContainer 57 | - parameters: 58 | - size: CGSize 59 | */ 60 | open func resizeTextContainer(_ size: CGSize) { 61 | self.textContainer.size = size 62 | } 63 | 64 | /** Updates textStorage 65 | - parameters: 66 | - attributedText: NSAttributedString 67 | */ 68 | open func updateTextStorage(_ attributedText: NSAttributedString) { 69 | self.textStorage.setAttributedString(attributedText) 70 | } 71 | 72 | /** Returns character index at a particular location 73 | - parameters: 74 | - location: CGPoint 75 | */ 76 | open func characterIndexAtLocation(_ location: CGPoint) -> Int { 77 | var characterIndex: Int = NSNotFound 78 | if self.textStorage.string.count > 0 { 79 | let glyphIndex = self.glyphIndexForLocation(location) 80 | // If the location is in white space after the last glyph on the line we don't 81 | // count it as a hit on the text 82 | let rangePointer: NSRangePointer? = nil 83 | var lineRect = self.layoutManager.lineFragmentUsedRect(forGlyphAt: glyphIndex, effectiveRange: rangePointer) 84 | lineRect.size.height = 60.0 //Adjustment to increase tap area 85 | if lineRect.contains(location) { 86 | characterIndex = self.layoutManager.characterIndexForGlyph(at: glyphIndex) 87 | } 88 | } 89 | return characterIndex 90 | } 91 | 92 | /** Returns the range which contains the index 93 | - parameters: 94 | - index: Int 95 | */ 96 | open func rangeContainingIndex(_ index: Int) -> NSRange { 97 | return self.layoutManager.range(ofNominallySpacedGlyphsContaining: index) 98 | } 99 | 100 | /** Returns bounding rectangle which encloses all the glyphs corresponding to textStorage 101 | */ 102 | open func boundingRectForCompleteText() -> CGRect { 103 | let initialSize = self.textContainer.size 104 | self.textContainer.size = CGSize(width: self.textContainer.size.width, height: CGFloat.greatestFiniteMagnitude) 105 | let glyphRange = self.layoutManager.glyphRange(for: textContainer) 106 | self.layoutManager.invalidateDisplay(forCharacterRange: NSMakeRange(0, self.textStorage.length - 1)) 107 | let rect = self.layoutManager.boundingRect(forGlyphRange: glyphRange, in:self.textContainer) 108 | self.textContainer.size = initialSize 109 | return rect 110 | } 111 | 112 | /** Returns bounding rectangle based on containerSize, number of lines and font of text 113 | - parameters: 114 | - size: CGSize 115 | - numberOfLines: Int 116 | - font: UIFont 117 | */ 118 | open func rectFittingTextForContainerSize(_ size: CGSize, numberOfLines: Int, font: UIFont) -> CGRect { 119 | let initialSize = self.textContainer.size 120 | self.textContainer.size = size 121 | self.textContainer.maximumNumberOfLines = numberOfLines 122 | var textBounds = self.layoutManager.boundingRect(forGlyphRange: NSMakeRange(0, self.layoutManager.numberOfGlyphs), in: self.textContainer) 123 | let totalLines = Int(textBounds.size.height / font.lineHeight) 124 | if numberOfLines > 0 { 125 | if numberOfLines < totalLines { 126 | textBounds.size.height -= CGFloat(totalLines - numberOfLines) * font.lineHeight 127 | } else if numberOfLines > totalLines { 128 | textBounds.size.height += CGFloat(numberOfLines - totalLines) * font.lineHeight 129 | } 130 | } 131 | textBounds.size.width = ceil(textBounds.size.width) 132 | textBounds.size.height = ceil(textBounds.size.height) 133 | self.textContainer.size = initialSize 134 | return textBounds; 135 | } 136 | 137 | /** Returns range at which given attributedTruncationToken can be appended 138 | - parameters: 139 | - attributedTruncationToken: NSAttributedString 140 | */ 141 | open func rangeForTokenInsertion(_ attributedTruncationToken: NSAttributedString) -> NSRange { 142 | guard self.textStorage.length > 0 else { 143 | return NSMakeRange(NSNotFound, 0) 144 | } 145 | var rangeOfText = NSMakeRange(NSNotFound, 0) 146 | if textStorage.isNewLinePresent() { 147 | rangeOfText = self.truncatedRangeForStringWithNewLine() 148 | } else { 149 | let glyphIndex = self.layoutManager.glyphIndexForCharacter(at: self.textStorage.length - 1) 150 | rangeOfText = self.layoutManager.truncatedGlyphRange(inLineFragmentForGlyphAt: glyphIndex) 151 | var lineRange = NSMakeRange(NSNotFound, 0) 152 | self.layoutManager.lineFragmentRect(forGlyphAt: glyphIndex, effectiveRange: &lineRange) 153 | rangeOfText = lineRange 154 | } 155 | let completeRect = boundingRectForCompleteText() 156 | let sizeOfToken = attributedTruncationToken.sizeOfText() 157 | var rectOfToken = CGRect.zero 158 | rectOfToken.size = sizeOfToken 159 | rectOfToken.origin.x = completeRect.maxX - sizeOfToken.width 160 | rectOfToken.origin.y = completeRect.maxY - sizeOfToken.height 161 | let index = self.characterIndexAtLocation(rectOfToken.origin) 162 | if rangeOfText.location != NSNotFound && index != NSNotFound { 163 | rangeOfText.length += (rangeOfText.location - index) 164 | rangeOfText.location = index 165 | } 166 | return rangeOfText; 167 | } 168 | 169 | /** Returns range at which new line appears 170 | */ 171 | open func truncatedRangeForStringWithNewLine() -> NSRange { 172 | let numberOfGlyphs = self.layoutManager.numberOfGlyphs 173 | var lineRange = NSMakeRange(NSNotFound, 0) 174 | let font = self.textStorage.attribute(NSAttributedString.Key.font, at: 0, effectiveRange: nil) as! UIFont 175 | let approximateNumberOfLines = Int(self.layoutManager.usedRect(for: self.textContainer).height / font.lineHeight) 176 | var index = 0 177 | var numberOfLines = 0 178 | while index < numberOfGlyphs { 179 | self.layoutManager.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange) 180 | if numberOfLines == approximateNumberOfLines - 1 { 181 | break 182 | } 183 | index = NSMaxRange(lineRange) 184 | numberOfLines += 1 185 | } 186 | let rangeOfText = NSMakeRange(lineRange.location + lineRange.length - 1, self.textStorage.length - lineRange.location - lineRange.length + 1) 187 | return rangeOfText 188 | } 189 | 190 | /** Returns the array of RangeAttribute instances for a given index 191 | - parameters: 192 | - attributeKey: NSAttributedStringKey 193 | - index: Int 194 | */ 195 | open func rangeAttributeForKey(_ attributeKey: NSAttributedString.Key, atIndex index: Int) -> RangeAttribute { 196 | var rangeOfTappedText = NSRange() 197 | let attribute = self.textStorage.attribute(attributeKey, at: index, effectiveRange: &rangeOfTappedText) 198 | return RangeAttribute(key: attributeKey, attribute: attribute, range: rangeOfTappedText) 199 | } 200 | 201 | /** Returns the array of RangeAttribute instances for a given index 202 | - parameters: 203 | - index: Int 204 | */ 205 | open func rangeAttributesAtIndex( _ index: Int) -> [RangeAttribute] { 206 | var rangeOfTappedText = NSRange() 207 | var rangeAttributes: [RangeAttribute] = [] 208 | self.textStorage.attributes(at: index, effectiveRange: &rangeOfTappedText).forEach { (arg) in 209 | 210 | let (key, value) = arg 211 | rangeAttributes.append(RangeAttribute(key: key, attribute: value, range: rangeOfTappedText)) 212 | } 213 | return rangeAttributes 214 | } 215 | 216 | /** Adds given attribute to the textStorage for the given key at the given range 217 | - parameters: 218 | - attribute: Any 219 | - key: NSAttributedStringKey 220 | - range: NSRange 221 | */ 222 | open func addAttribute(_ attribute: Any, forkey key: NSAttributedString.Key, atRange range: NSRange) { 223 | self.textStorage.addAttribute(key, value: attribute, range: range) 224 | } 225 | 226 | /** Removes attribute from the textStorage for the given key at the given range 227 | - parameters: 228 | - key: NSAttributedStringKey 229 | - range: NSRange 230 | */ 231 | open func removeAttribute(forkey key: NSAttributedString.Key, atRange range: NSRange) { 232 | self.textStorage.removeAttribute(key, range: range) 233 | } 234 | 235 | /** Returns the substring present in given range of the current textStorage 236 | - parameters: 237 | - range: NSRange 238 | */ 239 | open func substringForRange(_ range: NSRange) -> String { 240 | return (self.textStorage.string as NSString).substring(with: range) 241 | } 242 | 243 | /** Returns the range of string in the current textStorage 244 | - parameters: 245 | - string: String 246 | */ 247 | open func rangeOfString(_ string: String) -> NSRange { 248 | return (self.textStorage.string as NSString).range(of: string) 249 | } 250 | 251 | // MARK: Private Helpers 252 | 253 | fileprivate func glyphIndexForLocation(_ location: CGPoint) -> Int { 254 | // Use text offset to convert to text cotainer coordinates 255 | var convertedLocation = location 256 | convertedLocation.x -= self.currentTextOffset.x 257 | convertedLocation.y -= self.currentTextOffset.y 258 | return self.layoutManager.glyphIndex(for: location, in: self.textContainer, fractionOfDistanceThroughGlyph: nil) 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /SwiftResponsiveLabel/Source/TouchHandler.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TouchHandler.swift 3 | // SwiftResponsiveLabel 4 | // 5 | // Created by Susmita Horrow on 01/03/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit.UIGestureRecognizerSubclass 11 | 12 | class TouchGestureRecognizer: UIGestureRecognizer { 13 | 14 | override func touchesBegan(_ touches: Set, with event: UIEvent) { 15 | super.touchesBegan(touches, with: event) 16 | self.state = .began 17 | } 18 | 19 | override func touchesCancelled(_ touches: Set, with event: UIEvent) { 20 | super.touchesCancelled(touches, with: event) 21 | self.state = .cancelled 22 | } 23 | 24 | override func touchesEnded(_ touches: Set, with event: UIEvent) { 25 | super.touchesEnded(touches, with: event) 26 | self.state = .ended 27 | } 28 | } 29 | 30 | class TouchHandler: NSObject { 31 | fileprivate var responsiveLabel: SwiftResponsiveLabel? 32 | 33 | var touchIndex: Int? 34 | var selectedRange: NSRange? 35 | fileprivate var defaultAttributes: AttributesDictionary? 36 | fileprivate var highlightAttributes: AttributesDictionary? 37 | 38 | init(responsiveLabel: SwiftResponsiveLabel) { 39 | super.init() 40 | self.responsiveLabel = responsiveLabel 41 | let gestureRecognizer = TouchGestureRecognizer(target: self, action: #selector(TouchHandler.handleTouch(_:))) 42 | self.responsiveLabel?.addGestureRecognizer(gestureRecognizer) 43 | gestureRecognizer.delegate = self 44 | } 45 | 46 | @objc fileprivate func handleTouch(_ gesture: UIGestureRecognizer) { 47 | let touchLocation = gesture.location(in: self.responsiveLabel) 48 | let index = self.responsiveLabel?.textKitStack.characterIndexAtLocation(touchLocation) 49 | self.touchIndex = index 50 | 51 | switch gesture.state { 52 | case .began: 53 | self.beginSession() 54 | case .cancelled: 55 | self.cancelSession() 56 | case .ended: 57 | self.endSession() 58 | default: 59 | return 60 | } 61 | } 62 | 63 | fileprivate func beginSession() { 64 | guard let textkitStack = self.responsiveLabel?.textKitStack, 65 | let touchIndex = self.touchIndex, self.touchIndex! < textkitStack.textStorageLength else { return } 66 | var rangeOfTappedText = NSRange() 67 | let highlightAttributeInfo = textkitStack.rangeAttributeForKey(NSAttributedString.Key.RLHighlightedAttributesDictionary, atIndex: touchIndex) 68 | rangeOfTappedText = highlightAttributeInfo.range 69 | self.highlightAttributes = highlightAttributeInfo.attribute as? AttributesDictionary 70 | if let attributes = self.highlightAttributes { 71 | self.selectedRange = rangeOfTappedText 72 | self.defaultAttributes = [NSAttributedString.Key : Any]() 73 | for (key, value) in attributes { 74 | self.defaultAttributes![key] = textkitStack.rangeAttributeForKey(key, atIndex: touchIndex).attribute 75 | textkitStack.addAttribute(value, forkey: key, atRange: rangeOfTappedText) 76 | } 77 | self.responsiveLabel?.setNeedsDisplay() 78 | } 79 | if self.selectedRange == nil { 80 | if let _ = textkitStack.rangeAttributeForKey(NSAttributedString.Key.RLTapResponder, atIndex: touchIndex).attribute as? PatternTapResponder { 81 | self.selectedRange = rangeOfTappedText 82 | } 83 | } 84 | } 85 | 86 | fileprivate func cancelSession() { 87 | self.removeHighlight() 88 | } 89 | 90 | fileprivate func endSession() { 91 | self.performActionOnSelection() 92 | self.removeHighlight() 93 | } 94 | 95 | fileprivate func removeHighlight() { 96 | guard let textkitStack = self.responsiveLabel?.textKitStack, 97 | let selectedRange = self.selectedRange, 98 | let highlightAttributes = self.highlightAttributes, 99 | let defaultAttributes = self.defaultAttributes else { 100 | self.resetGlobalVariables() 101 | return 102 | } 103 | for (key, _) in highlightAttributes { 104 | textkitStack.removeAttribute(forkey: key, atRange: selectedRange) 105 | if let defaultValue = defaultAttributes[key] { 106 | textkitStack.addAttribute(defaultValue, forkey: key, atRange: selectedRange) 107 | } 108 | } 109 | 110 | self.responsiveLabel?.setNeedsDisplay() 111 | self.resetGlobalVariables() 112 | } 113 | 114 | private func resetGlobalVariables() { 115 | self.selectedRange = nil 116 | self.defaultAttributes = nil 117 | self.highlightAttributes = nil 118 | } 119 | 120 | fileprivate func performActionOnSelection() { 121 | guard let textkitStack = self.responsiveLabel?.textKitStack, let selectedRange = self.selectedRange else { return } 122 | if let tapResponder = textkitStack.rangeAttributeForKey(NSAttributedString.Key.RLTapResponder, atIndex: selectedRange.location).attribute as? PatternTapResponder { 123 | let tappedString = textkitStack.substringForRange(selectedRange) 124 | tapResponder.perform(tappedString) 125 | } 126 | } 127 | } 128 | 129 | extension TouchHandler : UIGestureRecognizerDelegate { 130 | func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 131 | return true 132 | } 133 | 134 | func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { 135 | let touchLocation = touch.location(in: self.responsiveLabel) 136 | guard let textkitStack = self.responsiveLabel?.textKitStack, 137 | let index = self.responsiveLabel?.textKitStack.characterIndexAtLocation(touchLocation), index < textkitStack.textStorageLength 138 | else { 139 | return false 140 | } 141 | let keys = textkitStack.rangeAttributesAtIndex(index).map { $0.key } 142 | return keys.contains(NSAttributedString.Key.RLHighlightedAttributesDictionary) || keys.contains(NSAttributedString.Key.RLTapResponder) 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /SwiftResponsiveLabelTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /SwiftResponsiveLabelTests/SwiftResponsiveLabelTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SwiftResponsiveLabelTests.swift 3 | // SwiftResponsiveLabelTests 4 | // 5 | // Created by Susmita Horrow on 29/02/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import SwiftResponsiveLabel 11 | 12 | class SwiftResponsiveLabelTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | // Use XCTAssert and related functions to verify your tests produce the correct results. 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measure { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /SwiftResponsiveLabelUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /SwiftResponsiveLabelUITests/SwiftResponsiveLabelUITests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SwiftResponsiveLabelUITests.swift 3 | // SwiftResponsiveLabelUITests 4 | // 5 | // Created by Susmita Horrow on 29/02/16. 6 | // Copyright © 2016 hsusmita.com. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | 11 | class SwiftResponsiveLabelUITests: XCTestCase { 12 | 13 | override func setUp() { 14 | super.setUp() 15 | 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | 18 | // In UI tests it is usually best to stop immediately when a failure occurs. 19 | continueAfterFailure = false 20 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 21 | XCUIApplication().launch() 22 | 23 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 24 | } 25 | 26 | override func tearDown() { 27 | // Put teardown code here. This method is called after the invocation of each test method in the class. 28 | super.tearDown() 29 | } 30 | 31 | func testExample() { 32 | // Use recording to get started writing UI tests. 33 | // Use XCTAssert and related functions to verify your tests produce the correct results. 34 | } 35 | 36 | } 37 | --------------------------------------------------------------------------------