├── .gitignore ├── HYLabel.podspec ├── HYLabel.xcworkspace └── contents.xcworkspacedata ├── HYLabel ├── HYLabel.xcodeproj │ └── project.pbxproj ├── HYLabel │ ├── HYLabel.h │ └── Info.plist └── Source │ └── HYLabel.swift ├── HYLabelDemo-Swift ├── HYLabelDemo-Swift.xcodeproj │ └── project.pbxproj └── HYLabelDemo-Swift │ ├── AppDelegate.swift │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ └── ViewController.swift ├── HYLabelDemoOC ├── HYLabelDemoOC.xcodeproj │ └── project.pbxproj └── HYLabelDemoOC │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ ├── Source │ └── HYLabel.swift │ ├── ViewController.h │ ├── ViewController.m │ └── main.m ├── LICENSE ├── README.md └── ReadmeImage └── image1.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | # Xcode 3 | # 4 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 5 | 6 | ## Build generated 7 | build/ 8 | DerivedData 9 | 10 | ## Various settings 11 | *.pbxuser 12 | !default.pbxuser 13 | *.mode1v3 14 | !default.mode1v3 15 | *.mode2v3 16 | !default.mode2v3 17 | *.perspectivev3 18 | !default.perspectivev3 19 | xcuserdata 20 | 21 | ## Other 22 | *.xccheckout 23 | *.moved-aside 24 | *.xcuserstate 25 | *.xcscmblueprint 26 | 27 | ## Obj-C/Swift specific 28 | *.hmap 29 | *.ipa 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | .build/ 40 | 41 | # CocoaPods 42 | # 43 | # We recommend against adding the Pods directory to your .gitignore. However 44 | # you should judge for yourself, the pros and cons are mentioned at: 45 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 46 | # 47 | # Pods/ 48 | 49 | # Carthage 50 | # 51 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 52 | # Carthage/Checkouts 53 | 54 | Carthage/Build 55 | 56 | # fastlane 57 | # 58 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 59 | # screenshots whenever they are needed. 60 | # For more information about the recommended setup visit: 61 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 62 | 63 | fastlane/report.xml 64 | fastlane/screenshots 65 | -------------------------------------------------------------------------------- /HYLabel.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'HYLabel' 3 | s.version = '1.0.1' 4 | s.summary = '用于识别Label中的@用户-话题##-链接' 5 | s.homepage = 'https://github.com/coderwhy/HYLabel' 6 | s.license = 'MIT' 7 | s.authors = {'coderwhy' => '372623326@qq.com'} 8 | s.platform = :ios, '8.0' 9 | s.source = {:git => 'https://github.com/coderwhy/HYLabel.git', :tag => s.version} 10 | s.source_files = 'HYLabel/Source/*.swift' 11 | s.framework = 'UIKit' 12 | s.requires_arc = true 13 | end -------------------------------------------------------------------------------- /HYLabel.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /HYLabel/HYLabel.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 187A8A111C8FC0EB00D8DA51 /* HYLabel.h in Headers */ = {isa = PBXBuildFile; fileRef = 187A8A101C8FC0EB00D8DA51 /* HYLabel.h */; settings = {ATTRIBUTES = (Public, ); }; }; 11 | 187A8A1A1C8FC15300D8DA51 /* HYLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 187A8A191C8FC15300D8DA51 /* HYLabel.swift */; }; 12 | /* End PBXBuildFile section */ 13 | 14 | /* Begin PBXFileReference section */ 15 | 187A8A0D1C8FC0EB00D8DA51 /* HYLabel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = HYLabel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 16 | 187A8A101C8FC0EB00D8DA51 /* HYLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HYLabel.h; sourceTree = ""; }; 17 | 187A8A121C8FC0EB00D8DA51 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 18 | 187A8A191C8FC15300D8DA51 /* HYLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HYLabel.swift; sourceTree = ""; }; 19 | /* End PBXFileReference section */ 20 | 21 | /* Begin PBXFrameworksBuildPhase section */ 22 | 187A8A091C8FC0EB00D8DA51 /* Frameworks */ = { 23 | isa = PBXFrameworksBuildPhase; 24 | buildActionMask = 2147483647; 25 | files = ( 26 | ); 27 | runOnlyForDeploymentPostprocessing = 0; 28 | }; 29 | /* End PBXFrameworksBuildPhase section */ 30 | 31 | /* Begin PBXGroup section */ 32 | 187A8A031C8FC0EB00D8DA51 = { 33 | isa = PBXGroup; 34 | children = ( 35 | 187A8A0F1C8FC0EB00D8DA51 /* HYLabel */, 36 | 187A8A0E1C8FC0EB00D8DA51 /* Products */, 37 | ); 38 | sourceTree = ""; 39 | }; 40 | 187A8A0E1C8FC0EB00D8DA51 /* Products */ = { 41 | isa = PBXGroup; 42 | children = ( 43 | 187A8A0D1C8FC0EB00D8DA51 /* HYLabel.framework */, 44 | ); 45 | name = Products; 46 | sourceTree = ""; 47 | }; 48 | 187A8A0F1C8FC0EB00D8DA51 /* HYLabel */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | 187A8A181C8FC13E00D8DA51 /* Source */, 52 | 187A8A101C8FC0EB00D8DA51 /* HYLabel.h */, 53 | 187A8A121C8FC0EB00D8DA51 /* Info.plist */, 54 | ); 55 | path = HYLabel; 56 | sourceTree = ""; 57 | }; 58 | 187A8A181C8FC13E00D8DA51 /* Source */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 187A8A191C8FC15300D8DA51 /* HYLabel.swift */, 62 | ); 63 | path = Source; 64 | sourceTree = SOURCE_ROOT; 65 | }; 66 | /* End PBXGroup section */ 67 | 68 | /* Begin PBXHeadersBuildPhase section */ 69 | 187A8A0A1C8FC0EB00D8DA51 /* Headers */ = { 70 | isa = PBXHeadersBuildPhase; 71 | buildActionMask = 2147483647; 72 | files = ( 73 | 187A8A111C8FC0EB00D8DA51 /* HYLabel.h in Headers */, 74 | ); 75 | runOnlyForDeploymentPostprocessing = 0; 76 | }; 77 | /* End PBXHeadersBuildPhase section */ 78 | 79 | /* Begin PBXNativeTarget section */ 80 | 187A8A0C1C8FC0EB00D8DA51 /* HYLabel */ = { 81 | isa = PBXNativeTarget; 82 | buildConfigurationList = 187A8A151C8FC0EB00D8DA51 /* Build configuration list for PBXNativeTarget "HYLabel" */; 83 | buildPhases = ( 84 | 187A8A081C8FC0EB00D8DA51 /* Sources */, 85 | 187A8A091C8FC0EB00D8DA51 /* Frameworks */, 86 | 187A8A0A1C8FC0EB00D8DA51 /* Headers */, 87 | 187A8A0B1C8FC0EB00D8DA51 /* Resources */, 88 | ); 89 | buildRules = ( 90 | ); 91 | dependencies = ( 92 | ); 93 | name = HYLabel; 94 | productName = HYLabel; 95 | productReference = 187A8A0D1C8FC0EB00D8DA51 /* HYLabel.framework */; 96 | productType = "com.apple.product-type.framework"; 97 | }; 98 | /* End PBXNativeTarget section */ 99 | 100 | /* Begin PBXProject section */ 101 | 187A8A041C8FC0EB00D8DA51 /* Project object */ = { 102 | isa = PBXProject; 103 | attributes = { 104 | LastUpgradeCheck = 0720; 105 | ORGANIZATIONNAME = xiaomage; 106 | TargetAttributes = { 107 | 187A8A0C1C8FC0EB00D8DA51 = { 108 | CreatedOnToolsVersion = 7.2; 109 | }; 110 | }; 111 | }; 112 | buildConfigurationList = 187A8A071C8FC0EB00D8DA51 /* Build configuration list for PBXProject "HYLabel" */; 113 | compatibilityVersion = "Xcode 3.2"; 114 | developmentRegion = English; 115 | hasScannedForEncodings = 0; 116 | knownRegions = ( 117 | en, 118 | ); 119 | mainGroup = 187A8A031C8FC0EB00D8DA51; 120 | productRefGroup = 187A8A0E1C8FC0EB00D8DA51 /* Products */; 121 | projectDirPath = ""; 122 | projectRoot = ""; 123 | targets = ( 124 | 187A8A0C1C8FC0EB00D8DA51 /* HYLabel */, 125 | ); 126 | }; 127 | /* End PBXProject section */ 128 | 129 | /* Begin PBXResourcesBuildPhase section */ 130 | 187A8A0B1C8FC0EB00D8DA51 /* Resources */ = { 131 | isa = PBXResourcesBuildPhase; 132 | buildActionMask = 2147483647; 133 | files = ( 134 | ); 135 | runOnlyForDeploymentPostprocessing = 0; 136 | }; 137 | /* End PBXResourcesBuildPhase section */ 138 | 139 | /* Begin PBXSourcesBuildPhase section */ 140 | 187A8A081C8FC0EB00D8DA51 /* Sources */ = { 141 | isa = PBXSourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | 187A8A1A1C8FC15300D8DA51 /* HYLabel.swift in Sources */, 145 | ); 146 | runOnlyForDeploymentPostprocessing = 0; 147 | }; 148 | /* End PBXSourcesBuildPhase section */ 149 | 150 | /* Begin XCBuildConfiguration section */ 151 | 187A8A131C8FC0EB00D8DA51 /* Debug */ = { 152 | isa = XCBuildConfiguration; 153 | buildSettings = { 154 | ALWAYS_SEARCH_USER_PATHS = NO; 155 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 156 | CLANG_CXX_LIBRARY = "libc++"; 157 | CLANG_ENABLE_MODULES = YES; 158 | CLANG_ENABLE_OBJC_ARC = YES; 159 | CLANG_WARN_BOOL_CONVERSION = YES; 160 | CLANG_WARN_CONSTANT_CONVERSION = YES; 161 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 162 | CLANG_WARN_EMPTY_BODY = YES; 163 | CLANG_WARN_ENUM_CONVERSION = YES; 164 | CLANG_WARN_INT_CONVERSION = YES; 165 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 166 | CLANG_WARN_UNREACHABLE_CODE = YES; 167 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 168 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 169 | COPY_PHASE_STRIP = NO; 170 | CURRENT_PROJECT_VERSION = 1; 171 | DEBUG_INFORMATION_FORMAT = dwarf; 172 | ENABLE_STRICT_OBJC_MSGSEND = YES; 173 | ENABLE_TESTABILITY = YES; 174 | GCC_C_LANGUAGE_STANDARD = gnu99; 175 | GCC_DYNAMIC_NO_PIC = NO; 176 | GCC_NO_COMMON_BLOCKS = YES; 177 | GCC_OPTIMIZATION_LEVEL = 0; 178 | GCC_PREPROCESSOR_DEFINITIONS = ( 179 | "DEBUG=1", 180 | "$(inherited)", 181 | ); 182 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 183 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 184 | GCC_WARN_UNDECLARED_SELECTOR = YES; 185 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 186 | GCC_WARN_UNUSED_FUNCTION = YES; 187 | GCC_WARN_UNUSED_VARIABLE = YES; 188 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 189 | MTL_ENABLE_DEBUG_INFO = YES; 190 | ONLY_ACTIVE_ARCH = YES; 191 | SDKROOT = iphoneos; 192 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 193 | TARGETED_DEVICE_FAMILY = "1,2"; 194 | VERSIONING_SYSTEM = "apple-generic"; 195 | VERSION_INFO_PREFIX = ""; 196 | }; 197 | name = Debug; 198 | }; 199 | 187A8A141C8FC0EB00D8DA51 /* Release */ = { 200 | isa = XCBuildConfiguration; 201 | buildSettings = { 202 | ALWAYS_SEARCH_USER_PATHS = NO; 203 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 204 | CLANG_CXX_LIBRARY = "libc++"; 205 | CLANG_ENABLE_MODULES = YES; 206 | CLANG_ENABLE_OBJC_ARC = YES; 207 | CLANG_WARN_BOOL_CONVERSION = YES; 208 | CLANG_WARN_CONSTANT_CONVERSION = YES; 209 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 210 | CLANG_WARN_EMPTY_BODY = YES; 211 | CLANG_WARN_ENUM_CONVERSION = YES; 212 | CLANG_WARN_INT_CONVERSION = YES; 213 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 214 | CLANG_WARN_UNREACHABLE_CODE = YES; 215 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 216 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 217 | COPY_PHASE_STRIP = NO; 218 | CURRENT_PROJECT_VERSION = 1; 219 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 220 | ENABLE_NS_ASSERTIONS = NO; 221 | ENABLE_STRICT_OBJC_MSGSEND = YES; 222 | GCC_C_LANGUAGE_STANDARD = gnu99; 223 | GCC_NO_COMMON_BLOCKS = YES; 224 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 225 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 226 | GCC_WARN_UNDECLARED_SELECTOR = YES; 227 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 228 | GCC_WARN_UNUSED_FUNCTION = YES; 229 | GCC_WARN_UNUSED_VARIABLE = YES; 230 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 231 | MTL_ENABLE_DEBUG_INFO = NO; 232 | SDKROOT = iphoneos; 233 | TARGETED_DEVICE_FAMILY = "1,2"; 234 | VALIDATE_PRODUCT = YES; 235 | VERSIONING_SYSTEM = "apple-generic"; 236 | VERSION_INFO_PREFIX = ""; 237 | }; 238 | name = Release; 239 | }; 240 | 187A8A161C8FC0EB00D8DA51 /* Debug */ = { 241 | isa = XCBuildConfiguration; 242 | buildSettings = { 243 | CLANG_ENABLE_MODULES = YES; 244 | DEFINES_MODULE = YES; 245 | DYLIB_COMPATIBILITY_VERSION = 1; 246 | DYLIB_CURRENT_VERSION = 1; 247 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 248 | INFOPLIST_FILE = HYLabel/Info.plist; 249 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 250 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 251 | PRODUCT_BUNDLE_IDENTIFIER = com.520it.HYLabel; 252 | PRODUCT_NAME = "$(TARGET_NAME)"; 253 | SKIP_INSTALL = YES; 254 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 255 | }; 256 | name = Debug; 257 | }; 258 | 187A8A171C8FC0EB00D8DA51 /* Release */ = { 259 | isa = XCBuildConfiguration; 260 | buildSettings = { 261 | CLANG_ENABLE_MODULES = YES; 262 | DEFINES_MODULE = YES; 263 | DYLIB_COMPATIBILITY_VERSION = 1; 264 | DYLIB_CURRENT_VERSION = 1; 265 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 266 | INFOPLIST_FILE = HYLabel/Info.plist; 267 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 268 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 269 | PRODUCT_BUNDLE_IDENTIFIER = com.520it.HYLabel; 270 | PRODUCT_NAME = "$(TARGET_NAME)"; 271 | SKIP_INSTALL = YES; 272 | }; 273 | name = Release; 274 | }; 275 | /* End XCBuildConfiguration section */ 276 | 277 | /* Begin XCConfigurationList section */ 278 | 187A8A071C8FC0EB00D8DA51 /* Build configuration list for PBXProject "HYLabel" */ = { 279 | isa = XCConfigurationList; 280 | buildConfigurations = ( 281 | 187A8A131C8FC0EB00D8DA51 /* Debug */, 282 | 187A8A141C8FC0EB00D8DA51 /* Release */, 283 | ); 284 | defaultConfigurationIsVisible = 0; 285 | defaultConfigurationName = Release; 286 | }; 287 | 187A8A151C8FC0EB00D8DA51 /* Build configuration list for PBXNativeTarget "HYLabel" */ = { 288 | isa = XCConfigurationList; 289 | buildConfigurations = ( 290 | 187A8A161C8FC0EB00D8DA51 /* Debug */, 291 | 187A8A171C8FC0EB00D8DA51 /* Release */, 292 | ); 293 | defaultConfigurationIsVisible = 0; 294 | }; 295 | /* End XCConfigurationList section */ 296 | }; 297 | rootObject = 187A8A041C8FC0EB00D8DA51 /* Project object */; 298 | } 299 | -------------------------------------------------------------------------------- /HYLabel/HYLabel/HYLabel.h: -------------------------------------------------------------------------------- 1 | // 2 | // HYLabel.h 3 | // HYLabel 4 | // 5 | // Created by apple on 16/3/9. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for HYLabel. 12 | FOUNDATION_EXPORT double HYLabelVersionNumber; 13 | 14 | //! Project version string for HYLabel. 15 | FOUNDATION_EXPORT const unsigned char HYLabelVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /HYLabel/HYLabel/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 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /HYLabel/Source/HYLabel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HYLabel.swift 3 | // HYLabel 4 | // 5 | // Created by apple on 16/3/8. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | enum TapHandlerType : Int { 12 | case noneHandle = 0 13 | case userHandle = 1 14 | case topicHandle = 2 15 | case linkHandle = 3 16 | } 17 | 18 | public class HYLabel: UILabel { 19 | 20 | // MARK:- 属性 21 | // 重写系统的属性 22 | override public var text : String? { 23 | didSet { 24 | prepareText() 25 | } 26 | } 27 | 28 | override public var attributedText: NSAttributedString? { 29 | didSet { 30 | prepareText() 31 | } 32 | } 33 | 34 | override public var font: UIFont! { 35 | didSet { 36 | prepareText() 37 | } 38 | } 39 | 40 | override public var textColor: UIColor! { 41 | didSet { 42 | prepareText() 43 | } 44 | } 45 | 46 | public var matchTextColor : UIColor = UIColor(red: 87 / 255.0, green: 196 / 255.0, blue: 251 / 255.0, alpha: 1.0) { 47 | didSet { 48 | prepareText() 49 | } 50 | } 51 | 52 | // 懒加载属性 53 | private lazy var textStorage : NSTextStorage = NSTextStorage() // NSMutableAttributeString的子类 54 | private lazy var layoutManager : NSLayoutManager = NSLayoutManager() // 布局管理者 55 | private lazy var textContainer : NSTextContainer = NSTextContainer() // 容器,需要设置容器的大小 56 | 57 | // 用于记录下标值 58 | private lazy var linkRanges : [NSRange] = [NSRange]() 59 | private lazy var userRanges : [NSRange] = [NSRange]() 60 | private lazy var topicRanges : [NSRange] = [NSRange]() 61 | 62 | // 用于记录用户选中的range 63 | private var selectedRange : NSRange? 64 | 65 | // 用户记录点击还是松开 66 | private var isSelected : Bool = false 67 | 68 | // 闭包属性,用于回调 69 | private var tapHandlerType : TapHandlerType = TapHandlerType.noneHandle 70 | 71 | public typealias HYTapHandler = (HYLabel, String, NSRange) -> Void 72 | public var linkTapHandler : HYTapHandler? 73 | public var topicTapHandler : HYTapHandler? 74 | public var userTapHandler : HYTapHandler? 75 | 76 | // MARK:- 构造函数 77 | override init(frame: CGRect) { 78 | super.init(frame: frame) 79 | 80 | prepareTextSystem() 81 | } 82 | 83 | required public init?(coder aDecoder: NSCoder) { 84 | super.init(coder: aDecoder) 85 | 86 | prepareTextSystem() 87 | } 88 | 89 | // MARK:- 布局子控件 90 | override public func layoutSubviews() { 91 | super.layoutSubviews() 92 | 93 | // 设置容器的大小为Label的尺寸 94 | textContainer.size = frame.size 95 | } 96 | 97 | // MARK:- 重写drawTextInRect方法 98 | override public func drawTextInRect(rect: CGRect) { 99 | // 1.绘制背景 100 | if selectedRange != nil { 101 | // 2.0.确定颜色 102 | let selectedColor = isSelected ? UIColor(white: 0.7, alpha: 0.2) : UIColor.clearColor() 103 | 104 | // 2.1.设置颜色 105 | textStorage.addAttribute(NSBackgroundColorAttributeName, value: selectedColor, range: selectedRange!) 106 | 107 | // 2.2.绘制背景 108 | layoutManager.drawBackgroundForGlyphRange(selectedRange!, atPoint: CGPoint(x: 0, y: 0)) 109 | } 110 | 111 | // 2.绘制字形 112 | // 需要绘制的范围 113 | let range = NSRange(location: 0, length: textStorage.length) 114 | layoutManager.drawGlyphsForGlyphRange(range, atPoint: CGPointZero) 115 | } 116 | } 117 | 118 | extension HYLabel { 119 | /// 准备文本系统 120 | private func prepareTextSystem() { 121 | // 0.准备文本 122 | prepareText() 123 | 124 | // 1.将布局添加到storeage中 125 | textStorage.addLayoutManager(layoutManager) 126 | 127 | // 2.将容器添加到布局中 128 | layoutManager.addTextContainer(textContainer) 129 | 130 | // 3.让label可以和用户交互 131 | userInteractionEnabled = true 132 | 133 | // 4.设置间距为0 134 | textContainer.lineFragmentPadding = 0 135 | } 136 | 137 | /// 准备文本 138 | private func prepareText() { 139 | // 1.准备字符串 140 | var attrString : NSAttributedString? 141 | if attributedText != nil { 142 | attrString = attributedText 143 | } else if text != nil { 144 | attrString = NSAttributedString(string: text!) 145 | } else { 146 | attrString = NSAttributedString(string: "") 147 | } 148 | 149 | selectedRange = nil 150 | 151 | // 2.设置换行模型 152 | let attrStringM = addLineBreak(attrString!) 153 | 154 | attrStringM.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: attrStringM.length)) 155 | 156 | // 3.设置textStorage的内容 157 | textStorage.setAttributedString(attrStringM) 158 | 159 | // 4.匹配URL 160 | if let linkRanges = getLinkRanges() { 161 | self.linkRanges = linkRanges 162 | for range in linkRanges { 163 | textStorage.addAttribute(NSForegroundColorAttributeName, value: matchTextColor, range: range) 164 | } 165 | } 166 | 167 | // 5.匹配@用户 168 | if let userRanges = getRanges("@[\\u4e00-\\u9fa5a-zA-Z0-9_-]*") { 169 | self.userRanges = userRanges 170 | for range in userRanges { 171 | textStorage.addAttribute(NSForegroundColorAttributeName, value: matchTextColor, range: range) 172 | } 173 | } 174 | 175 | 176 | // 6.匹配话题## 177 | if let topicRanges = getRanges("#.*?#") { 178 | self.topicRanges = topicRanges 179 | for range in topicRanges { 180 | textStorage.addAttribute(NSForegroundColorAttributeName, value: matchTextColor, range: range) 181 | } 182 | } 183 | 184 | setNeedsDisplay() 185 | } 186 | } 187 | 188 | 189 | // MARK:- 字符串匹配封装 190 | extension HYLabel { 191 | private func getRanges(pattern : String) -> [NSRange]? { 192 | // 创建正则表达式对象 193 | guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { 194 | return nil 195 | } 196 | 197 | return getRangesFromResult(regex) 198 | } 199 | 200 | private func getLinkRanges() -> [NSRange]? { 201 | // 创建正则表达式 202 | guard let detector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue) else { 203 | return nil 204 | } 205 | 206 | return getRangesFromResult(detector) 207 | } 208 | 209 | private func getRangesFromResult(regex : NSRegularExpression) -> [NSRange] { 210 | // 1.匹配结果 211 | let results = regex.matchesInString(textStorage.string, options: [], range: NSRange(location: 0, length: textStorage.length)) 212 | 213 | // 2.遍历结果 214 | var ranges = [NSRange]() 215 | for res in results { 216 | ranges.append(res.range) 217 | } 218 | 219 | return ranges 220 | } 221 | } 222 | 223 | 224 | // MARK:- 点击交互的封装 225 | extension HYLabel { 226 | override public func touchesBegan(touches: Set, withEvent event: UIEvent?) { 227 | // 0.记录点击 228 | isSelected = true 229 | 230 | // 1.获取用户点击的点 231 | let selectedPoint = touches.first!.locationInView(self) 232 | 233 | // 2.获取该点所在的字符串的range 234 | selectedRange = getSelectRange(selectedPoint) 235 | 236 | // 3.是否处理了事件 237 | if selectedRange == nil { 238 | super.touchesBegan(touches, withEvent: event) 239 | } 240 | } 241 | 242 | override public func touchesEnded(touches: Set, withEvent event: UIEvent?) { 243 | if selectedRange == nil { 244 | super.touchesBegan(touches, withEvent: event) 245 | return 246 | } 247 | 248 | // 0.记录松开 249 | isSelected = false 250 | 251 | // 2.重新绘制 252 | setNeedsDisplay() 253 | 254 | // 3.取出内容 255 | let contentText = (textStorage.string as NSString).substringWithRange(selectedRange!) 256 | 257 | // 3.回调 258 | switch tapHandlerType { 259 | case .linkHandle: 260 | if linkTapHandler != nil { 261 | linkTapHandler!(self, contentText, selectedRange!) 262 | } 263 | case .topicHandle: 264 | if topicTapHandler != nil { 265 | topicTapHandler!(self, contentText, selectedRange!) 266 | } 267 | case .userHandle: 268 | if userTapHandler != nil { 269 | userTapHandler!(self, contentText, selectedRange!) 270 | } 271 | default: 272 | break 273 | } 274 | } 275 | 276 | private func getSelectRange(selectedPoint : CGPoint) -> NSRange? { 277 | // 0.如果属性字符串为nil,则不需要判断 278 | if textStorage.length == 0 { 279 | return nil 280 | } 281 | 282 | // 1.获取选中点所在的下标值(index) 283 | let index = layoutManager.glyphIndexForPoint(selectedPoint, inTextContainer: textContainer) 284 | 285 | // 2.判断下标在什么内 286 | // 2.1.判断是否是一个链接 287 | for linkRange in linkRanges { 288 | if index > linkRange.location && index < linkRange.location + linkRange.length { 289 | setNeedsDisplay() 290 | tapHandlerType = .linkHandle 291 | return linkRange 292 | } 293 | } 294 | 295 | // 2.2.判断是否是一个@用户 296 | for userRange in userRanges { 297 | if index > userRange.location && index < userRange.location + userRange.length { 298 | setNeedsDisplay() 299 | tapHandlerType = .userHandle 300 | return userRange 301 | } 302 | } 303 | 304 | // 2.3.判断是否是一个#话题# 305 | for topicRange in topicRanges { 306 | if index > topicRange.location && index < topicRange.location + topicRange.length { 307 | setNeedsDisplay() 308 | tapHandlerType = .topicHandle 309 | return topicRange 310 | } 311 | } 312 | 313 | return nil 314 | } 315 | } 316 | 317 | // MARK:- 补充 318 | extension HYLabel { 319 | /// 如果用户没有设置lineBreak,则所有内容会绘制到同一行中,因此需要主动设置 320 | private func addLineBreak(attrString: NSAttributedString) -> NSMutableAttributedString { 321 | let attrStringM = NSMutableAttributedString(attributedString: attrString) 322 | 323 | if attrStringM.length == 0 { 324 | return attrStringM 325 | } 326 | 327 | var range = NSRange(location: 0, length: 0) 328 | var attributes = attrStringM.attributesAtIndex(0, effectiveRange: &range) 329 | var paragraphStyle = attributes[NSParagraphStyleAttributeName] as? NSMutableParagraphStyle 330 | 331 | if paragraphStyle != nil { 332 | paragraphStyle!.lineBreakMode = NSLineBreakMode.ByWordWrapping 333 | } else { 334 | paragraphStyle = NSMutableParagraphStyle() 335 | paragraphStyle!.lineBreakMode = NSLineBreakMode.ByWordWrapping 336 | attributes[NSParagraphStyleAttributeName] = paragraphStyle 337 | 338 | attrStringM.setAttributes(attributes, range: range) 339 | } 340 | 341 | return attrStringM 342 | } 343 | } 344 | -------------------------------------------------------------------------------- /HYLabelDemo-Swift/HYLabelDemo-Swift.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 187A89F21C8FC0A300D8DA51 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 187A89F11C8FC0A300D8DA51 /* AppDelegate.swift */; }; 11 | 187A89F41C8FC0A300D8DA51 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 187A89F31C8FC0A300D8DA51 /* ViewController.swift */; }; 12 | 187A89F71C8FC0A300D8DA51 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 187A89F51C8FC0A300D8DA51 /* Main.storyboard */; }; 13 | 187A89F91C8FC0A300D8DA51 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 187A89F81C8FC0A300D8DA51 /* Assets.xcassets */; }; 14 | 187A89FC1C8FC0A300D8DA51 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 187A89FA1C8FC0A300D8DA51 /* LaunchScreen.storyboard */; }; 15 | 187A8A1D1C8FC2E200D8DA51 /* HYLabel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 187A8A1C1C8FC2E200D8DA51 /* HYLabel.framework */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 187A89EE1C8FC0A300D8DA51 /* HYLabelDemo-Swift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "HYLabelDemo-Swift.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 187A89F11C8FC0A300D8DA51 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | 187A89F31C8FC0A300D8DA51 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | 187A89F61C8FC0A300D8DA51 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | 187A89F81C8FC0A300D8DA51 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | 187A89FB1C8FC0A300D8DA51 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | 187A89FD1C8FC0A300D8DA51 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | 187A8A1C1C8FC2E200D8DA51 /* HYLabel.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = HYLabel.framework; path = "../HYLabel/build/Debug-iphoneos/HYLabel.framework"; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 187A89EB1C8FC0A300D8DA51 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | 187A8A1D1C8FC2E200D8DA51 /* HYLabel.framework in Frameworks */, 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | 187A89E51C8FC0A300D8DA51 = { 42 | isa = PBXGroup; 43 | children = ( 44 | 187A8A1C1C8FC2E200D8DA51 /* HYLabel.framework */, 45 | 187A89F01C8FC0A300D8DA51 /* HYLabelDemo-Swift */, 46 | 187A89EF1C8FC0A300D8DA51 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | 187A89EF1C8FC0A300D8DA51 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | 187A89EE1C8FC0A300D8DA51 /* HYLabelDemo-Swift.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | 187A89F01C8FC0A300D8DA51 /* HYLabelDemo-Swift */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 187A89F11C8FC0A300D8DA51 /* AppDelegate.swift */, 62 | 187A89F31C8FC0A300D8DA51 /* ViewController.swift */, 63 | 187A89F51C8FC0A300D8DA51 /* Main.storyboard */, 64 | 187A89F81C8FC0A300D8DA51 /* Assets.xcassets */, 65 | 187A89FA1C8FC0A300D8DA51 /* LaunchScreen.storyboard */, 66 | 187A89FD1C8FC0A300D8DA51 /* Info.plist */, 67 | ); 68 | path = "HYLabelDemo-Swift"; 69 | sourceTree = ""; 70 | }; 71 | /* End PBXGroup section */ 72 | 73 | /* Begin PBXNativeTarget section */ 74 | 187A89ED1C8FC0A300D8DA51 /* HYLabelDemo-Swift */ = { 75 | isa = PBXNativeTarget; 76 | buildConfigurationList = 187A8A001C8FC0A300D8DA51 /* Build configuration list for PBXNativeTarget "HYLabelDemo-Swift" */; 77 | buildPhases = ( 78 | 187A89EA1C8FC0A300D8DA51 /* Sources */, 79 | 187A89EB1C8FC0A300D8DA51 /* Frameworks */, 80 | 187A89EC1C8FC0A300D8DA51 /* Resources */, 81 | ); 82 | buildRules = ( 83 | ); 84 | dependencies = ( 85 | ); 86 | name = "HYLabelDemo-Swift"; 87 | productName = "HYLabelDemo-Swift"; 88 | productReference = 187A89EE1C8FC0A300D8DA51 /* HYLabelDemo-Swift.app */; 89 | productType = "com.apple.product-type.application"; 90 | }; 91 | /* End PBXNativeTarget section */ 92 | 93 | /* Begin PBXProject section */ 94 | 187A89E61C8FC0A300D8DA51 /* Project object */ = { 95 | isa = PBXProject; 96 | attributes = { 97 | LastSwiftUpdateCheck = 0720; 98 | LastUpgradeCheck = 0720; 99 | ORGANIZATIONNAME = xiaomage; 100 | TargetAttributes = { 101 | 187A89ED1C8FC0A300D8DA51 = { 102 | CreatedOnToolsVersion = 7.2; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = 187A89E91C8FC0A300D8DA51 /* Build configuration list for PBXProject "HYLabelDemo-Swift" */; 107 | compatibilityVersion = "Xcode 3.2"; 108 | developmentRegion = English; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = 187A89E51C8FC0A300D8DA51; 115 | productRefGroup = 187A89EF1C8FC0A300D8DA51 /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | 187A89ED1C8FC0A300D8DA51 /* HYLabelDemo-Swift */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXResourcesBuildPhase section */ 125 | 187A89EC1C8FC0A300D8DA51 /* Resources */ = { 126 | isa = PBXResourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | 187A89FC1C8FC0A300D8DA51 /* LaunchScreen.storyboard in Resources */, 130 | 187A89F91C8FC0A300D8DA51 /* Assets.xcassets in Resources */, 131 | 187A89F71C8FC0A300D8DA51 /* Main.storyboard in Resources */, 132 | ); 133 | runOnlyForDeploymentPostprocessing = 0; 134 | }; 135 | /* End PBXResourcesBuildPhase section */ 136 | 137 | /* Begin PBXSourcesBuildPhase section */ 138 | 187A89EA1C8FC0A300D8DA51 /* Sources */ = { 139 | isa = PBXSourcesBuildPhase; 140 | buildActionMask = 2147483647; 141 | files = ( 142 | 187A89F41C8FC0A300D8DA51 /* ViewController.swift in Sources */, 143 | 187A89F21C8FC0A300D8DA51 /* AppDelegate.swift in Sources */, 144 | ); 145 | runOnlyForDeploymentPostprocessing = 0; 146 | }; 147 | /* End PBXSourcesBuildPhase section */ 148 | 149 | /* Begin PBXVariantGroup section */ 150 | 187A89F51C8FC0A300D8DA51 /* Main.storyboard */ = { 151 | isa = PBXVariantGroup; 152 | children = ( 153 | 187A89F61C8FC0A300D8DA51 /* Base */, 154 | ); 155 | name = Main.storyboard; 156 | sourceTree = ""; 157 | }; 158 | 187A89FA1C8FC0A300D8DA51 /* LaunchScreen.storyboard */ = { 159 | isa = PBXVariantGroup; 160 | children = ( 161 | 187A89FB1C8FC0A300D8DA51 /* Base */, 162 | ); 163 | name = LaunchScreen.storyboard; 164 | sourceTree = ""; 165 | }; 166 | /* End PBXVariantGroup section */ 167 | 168 | /* Begin XCBuildConfiguration section */ 169 | 187A89FE1C8FC0A300D8DA51 /* Debug */ = { 170 | isa = XCBuildConfiguration; 171 | buildSettings = { 172 | ALWAYS_SEARCH_USER_PATHS = NO; 173 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 174 | CLANG_CXX_LIBRARY = "libc++"; 175 | CLANG_ENABLE_MODULES = YES; 176 | CLANG_ENABLE_OBJC_ARC = YES; 177 | CLANG_WARN_BOOL_CONVERSION = YES; 178 | CLANG_WARN_CONSTANT_CONVERSION = YES; 179 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 180 | CLANG_WARN_EMPTY_BODY = YES; 181 | CLANG_WARN_ENUM_CONVERSION = YES; 182 | CLANG_WARN_INT_CONVERSION = YES; 183 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 184 | CLANG_WARN_UNREACHABLE_CODE = YES; 185 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 186 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 187 | COPY_PHASE_STRIP = NO; 188 | DEBUG_INFORMATION_FORMAT = dwarf; 189 | ENABLE_STRICT_OBJC_MSGSEND = YES; 190 | ENABLE_TESTABILITY = YES; 191 | GCC_C_LANGUAGE_STANDARD = gnu99; 192 | GCC_DYNAMIC_NO_PIC = NO; 193 | GCC_NO_COMMON_BLOCKS = YES; 194 | GCC_OPTIMIZATION_LEVEL = 0; 195 | GCC_PREPROCESSOR_DEFINITIONS = ( 196 | "DEBUG=1", 197 | "$(inherited)", 198 | ); 199 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 200 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 201 | GCC_WARN_UNDECLARED_SELECTOR = YES; 202 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 203 | GCC_WARN_UNUSED_FUNCTION = YES; 204 | GCC_WARN_UNUSED_VARIABLE = YES; 205 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 206 | MTL_ENABLE_DEBUG_INFO = YES; 207 | ONLY_ACTIVE_ARCH = YES; 208 | SDKROOT = iphoneos; 209 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 210 | }; 211 | name = Debug; 212 | }; 213 | 187A89FF1C8FC0A300D8DA51 /* Release */ = { 214 | isa = XCBuildConfiguration; 215 | buildSettings = { 216 | ALWAYS_SEARCH_USER_PATHS = NO; 217 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 218 | CLANG_CXX_LIBRARY = "libc++"; 219 | CLANG_ENABLE_MODULES = YES; 220 | CLANG_ENABLE_OBJC_ARC = YES; 221 | CLANG_WARN_BOOL_CONVERSION = YES; 222 | CLANG_WARN_CONSTANT_CONVERSION = YES; 223 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 224 | CLANG_WARN_EMPTY_BODY = YES; 225 | CLANG_WARN_ENUM_CONVERSION = YES; 226 | CLANG_WARN_INT_CONVERSION = YES; 227 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 228 | CLANG_WARN_UNREACHABLE_CODE = YES; 229 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 230 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 231 | COPY_PHASE_STRIP = NO; 232 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 233 | ENABLE_NS_ASSERTIONS = NO; 234 | ENABLE_STRICT_OBJC_MSGSEND = YES; 235 | GCC_C_LANGUAGE_STANDARD = gnu99; 236 | GCC_NO_COMMON_BLOCKS = YES; 237 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 238 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 239 | GCC_WARN_UNDECLARED_SELECTOR = YES; 240 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 241 | GCC_WARN_UNUSED_FUNCTION = YES; 242 | GCC_WARN_UNUSED_VARIABLE = YES; 243 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 244 | MTL_ENABLE_DEBUG_INFO = NO; 245 | SDKROOT = iphoneos; 246 | VALIDATE_PRODUCT = YES; 247 | }; 248 | name = Release; 249 | }; 250 | 187A8A011C8FC0A300D8DA51 /* Debug */ = { 251 | isa = XCBuildConfiguration; 252 | buildSettings = { 253 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 254 | INFOPLIST_FILE = "HYLabelDemo-Swift/Info.plist"; 255 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 256 | PRODUCT_BUNDLE_IDENTIFIER = "com.520it.HYLabelDemo-Swift"; 257 | PRODUCT_NAME = "$(TARGET_NAME)"; 258 | }; 259 | name = Debug; 260 | }; 261 | 187A8A021C8FC0A300D8DA51 /* Release */ = { 262 | isa = XCBuildConfiguration; 263 | buildSettings = { 264 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 265 | INFOPLIST_FILE = "HYLabelDemo-Swift/Info.plist"; 266 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 267 | PRODUCT_BUNDLE_IDENTIFIER = "com.520it.HYLabelDemo-Swift"; 268 | PRODUCT_NAME = "$(TARGET_NAME)"; 269 | }; 270 | name = Release; 271 | }; 272 | /* End XCBuildConfiguration section */ 273 | 274 | /* Begin XCConfigurationList section */ 275 | 187A89E91C8FC0A300D8DA51 /* Build configuration list for PBXProject "HYLabelDemo-Swift" */ = { 276 | isa = XCConfigurationList; 277 | buildConfigurations = ( 278 | 187A89FE1C8FC0A300D8DA51 /* Debug */, 279 | 187A89FF1C8FC0A300D8DA51 /* Release */, 280 | ); 281 | defaultConfigurationIsVisible = 0; 282 | defaultConfigurationName = Release; 283 | }; 284 | 187A8A001C8FC0A300D8DA51 /* Build configuration list for PBXNativeTarget "HYLabelDemo-Swift" */ = { 285 | isa = XCConfigurationList; 286 | buildConfigurations = ( 287 | 187A8A011C8FC0A300D8DA51 /* Debug */, 288 | 187A8A021C8FC0A300D8DA51 /* Release */, 289 | ); 290 | defaultConfigurationIsVisible = 0; 291 | }; 292 | /* End XCConfigurationList section */ 293 | }; 294 | rootObject = 187A89E61C8FC0A300D8DA51 /* Project object */; 295 | } 296 | -------------------------------------------------------------------------------- /HYLabelDemo-Swift/HYLabelDemo-Swift/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // HYLabelDemo-Swift 4 | // 5 | // Created by apple on 16/3/9. 6 | // Copyright © 2016年 xiaomage. 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: [NSObject: AnyObject]?) -> 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 | -------------------------------------------------------------------------------- /HYLabelDemo-Swift/HYLabelDemo-Swift/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 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /HYLabelDemo-Swift/HYLabelDemo-Swift/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 | -------------------------------------------------------------------------------- /HYLabelDemo-Swift/HYLabelDemo-Swift/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /HYLabelDemo-Swift/HYLabelDemo-Swift/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 | 40 | 41 | -------------------------------------------------------------------------------- /HYLabelDemo-Swift/HYLabelDemo-Swift/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // HYLabelDemo-Swift 4 | // 5 | // Created by apple on 16/3/9. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import HYLabel 11 | 12 | class ViewController: UIViewController { 13 | 14 | @IBOutlet weak var demoLabel: HYLabel! 15 | 16 | override func viewDidLoad() { 17 | super.viewDidLoad() 18 | 19 | // 监听@谁谁谁的点击 20 | demoLabel.userTapHandler = { (label, user, range) in 21 | print(label) 22 | print(user) 23 | print(range) 24 | } 25 | 26 | // 监听链接的点击 27 | demoLabel.linkTapHandler = { (label, link, range) in 28 | print(label) 29 | print(link) 30 | print(range) 31 | } 32 | 33 | // 监听话题的点击 34 | demoLabel.topicTapHandler = { (label, topic, range) in 35 | print(label) 36 | print(topic) 37 | print(range) 38 | } 39 | } 40 | 41 | override func touchesBegan(touches: Set, withEvent event: UIEvent?) { 42 | demoLabel.text = "作者:@coderwhy 话题:#Label字符串识别# 网址:http://www.520it.com" 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 18B0EE1F1C97B4F700BD3B93 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 18B0EE1E1C97B4F700BD3B93 /* main.m */; }; 11 | 18B0EE221C97B4F700BD3B93 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 18B0EE211C97B4F700BD3B93 /* AppDelegate.m */; }; 12 | 18B0EE251C97B4F700BD3B93 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18B0EE241C97B4F700BD3B93 /* ViewController.m */; }; 13 | 18B0EE281C97B4F700BD3B93 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18B0EE261C97B4F700BD3B93 /* Main.storyboard */; }; 14 | 18B0EE2A1C97B4F700BD3B93 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18B0EE291C97B4F700BD3B93 /* Assets.xcassets */; }; 15 | 18B0EE2D1C97B4F700BD3B93 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18B0EE2B1C97B4F700BD3B93 /* LaunchScreen.storyboard */; }; 16 | 18B0EE391C97B6C500BD3B93 /* HYLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18B0EE381C97B6C500BD3B93 /* HYLabel.swift */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 18B0EE1A1C97B4F700BD3B93 /* HYLabelDemoOC.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HYLabelDemoOC.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 18B0EE1E1C97B4F700BD3B93 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 22 | 18B0EE201C97B4F700BD3B93 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 23 | 18B0EE211C97B4F700BD3B93 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 24 | 18B0EE231C97B4F700BD3B93 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 25 | 18B0EE241C97B4F700BD3B93 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 26 | 18B0EE271C97B4F700BD3B93 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | 18B0EE291C97B4F700BD3B93 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | 18B0EE2C1C97B4F700BD3B93 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | 18B0EE2E1C97B4F700BD3B93 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | 18B0EE381C97B6C500BD3B93 /* HYLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HYLabel.swift; sourceTree = ""; }; 31 | /* End PBXFileReference section */ 32 | 33 | /* Begin PBXFrameworksBuildPhase section */ 34 | 18B0EE171C97B4F700BD3B93 /* Frameworks */ = { 35 | isa = PBXFrameworksBuildPhase; 36 | buildActionMask = 2147483647; 37 | files = ( 38 | ); 39 | runOnlyForDeploymentPostprocessing = 0; 40 | }; 41 | /* End PBXFrameworksBuildPhase section */ 42 | 43 | /* Begin PBXGroup section */ 44 | 18B0EE111C97B4F700BD3B93 = { 45 | isa = PBXGroup; 46 | children = ( 47 | 18B0EE1C1C97B4F700BD3B93 /* HYLabelDemoOC */, 48 | 18B0EE1B1C97B4F700BD3B93 /* Products */, 49 | ); 50 | sourceTree = ""; 51 | }; 52 | 18B0EE1B1C97B4F700BD3B93 /* Products */ = { 53 | isa = PBXGroup; 54 | children = ( 55 | 18B0EE1A1C97B4F700BD3B93 /* HYLabelDemoOC.app */, 56 | ); 57 | name = Products; 58 | sourceTree = ""; 59 | }; 60 | 18B0EE1C1C97B4F700BD3B93 /* HYLabelDemoOC */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | 18B0EE371C97B6C500BD3B93 /* Source */, 64 | 18B0EE201C97B4F700BD3B93 /* AppDelegate.h */, 65 | 18B0EE211C97B4F700BD3B93 /* AppDelegate.m */, 66 | 18B0EE231C97B4F700BD3B93 /* ViewController.h */, 67 | 18B0EE241C97B4F700BD3B93 /* ViewController.m */, 68 | 18B0EE261C97B4F700BD3B93 /* Main.storyboard */, 69 | 18B0EE291C97B4F700BD3B93 /* Assets.xcassets */, 70 | 18B0EE2B1C97B4F700BD3B93 /* LaunchScreen.storyboard */, 71 | 18B0EE2E1C97B4F700BD3B93 /* Info.plist */, 72 | 18B0EE1D1C97B4F700BD3B93 /* Supporting Files */, 73 | ); 74 | path = HYLabelDemoOC; 75 | sourceTree = ""; 76 | }; 77 | 18B0EE1D1C97B4F700BD3B93 /* Supporting Files */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | 18B0EE1E1C97B4F700BD3B93 /* main.m */, 81 | ); 82 | name = "Supporting Files"; 83 | sourceTree = ""; 84 | }; 85 | 18B0EE371C97B6C500BD3B93 /* Source */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | 18B0EE381C97B6C500BD3B93 /* HYLabel.swift */, 89 | ); 90 | path = Source; 91 | sourceTree = ""; 92 | }; 93 | /* End PBXGroup section */ 94 | 95 | /* Begin PBXNativeTarget section */ 96 | 18B0EE191C97B4F700BD3B93 /* HYLabelDemoOC */ = { 97 | isa = PBXNativeTarget; 98 | buildConfigurationList = 18B0EE311C97B4F700BD3B93 /* Build configuration list for PBXNativeTarget "HYLabelDemoOC" */; 99 | buildPhases = ( 100 | 18B0EE161C97B4F700BD3B93 /* Sources */, 101 | 18B0EE171C97B4F700BD3B93 /* Frameworks */, 102 | 18B0EE181C97B4F700BD3B93 /* Resources */, 103 | ); 104 | buildRules = ( 105 | ); 106 | dependencies = ( 107 | ); 108 | name = HYLabelDemoOC; 109 | productName = HYLabelDemoOC; 110 | productReference = 18B0EE1A1C97B4F700BD3B93 /* HYLabelDemoOC.app */; 111 | productType = "com.apple.product-type.application"; 112 | }; 113 | /* End PBXNativeTarget section */ 114 | 115 | /* Begin PBXProject section */ 116 | 18B0EE121C97B4F700BD3B93 /* Project object */ = { 117 | isa = PBXProject; 118 | attributes = { 119 | LastUpgradeCheck = 0720; 120 | ORGANIZATIONNAME = xiaomage; 121 | TargetAttributes = { 122 | 18B0EE191C97B4F700BD3B93 = { 123 | CreatedOnToolsVersion = 7.2; 124 | }; 125 | }; 126 | }; 127 | buildConfigurationList = 18B0EE151C97B4F700BD3B93 /* Build configuration list for PBXProject "HYLabelDemoOC" */; 128 | compatibilityVersion = "Xcode 3.2"; 129 | developmentRegion = English; 130 | hasScannedForEncodings = 0; 131 | knownRegions = ( 132 | en, 133 | Base, 134 | ); 135 | mainGroup = 18B0EE111C97B4F700BD3B93; 136 | productRefGroup = 18B0EE1B1C97B4F700BD3B93 /* Products */; 137 | projectDirPath = ""; 138 | projectRoot = ""; 139 | targets = ( 140 | 18B0EE191C97B4F700BD3B93 /* HYLabelDemoOC */, 141 | ); 142 | }; 143 | /* End PBXProject section */ 144 | 145 | /* Begin PBXResourcesBuildPhase section */ 146 | 18B0EE181C97B4F700BD3B93 /* Resources */ = { 147 | isa = PBXResourcesBuildPhase; 148 | buildActionMask = 2147483647; 149 | files = ( 150 | 18B0EE2D1C97B4F700BD3B93 /* LaunchScreen.storyboard in Resources */, 151 | 18B0EE2A1C97B4F700BD3B93 /* Assets.xcassets in Resources */, 152 | 18B0EE281C97B4F700BD3B93 /* Main.storyboard in Resources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXResourcesBuildPhase section */ 157 | 158 | /* Begin PBXSourcesBuildPhase section */ 159 | 18B0EE161C97B4F700BD3B93 /* Sources */ = { 160 | isa = PBXSourcesBuildPhase; 161 | buildActionMask = 2147483647; 162 | files = ( 163 | 18B0EE251C97B4F700BD3B93 /* ViewController.m in Sources */, 164 | 18B0EE391C97B6C500BD3B93 /* HYLabel.swift in Sources */, 165 | 18B0EE221C97B4F700BD3B93 /* AppDelegate.m in Sources */, 166 | 18B0EE1F1C97B4F700BD3B93 /* main.m in Sources */, 167 | ); 168 | runOnlyForDeploymentPostprocessing = 0; 169 | }; 170 | /* End PBXSourcesBuildPhase section */ 171 | 172 | /* Begin PBXVariantGroup section */ 173 | 18B0EE261C97B4F700BD3B93 /* Main.storyboard */ = { 174 | isa = PBXVariantGroup; 175 | children = ( 176 | 18B0EE271C97B4F700BD3B93 /* Base */, 177 | ); 178 | name = Main.storyboard; 179 | sourceTree = ""; 180 | }; 181 | 18B0EE2B1C97B4F700BD3B93 /* LaunchScreen.storyboard */ = { 182 | isa = PBXVariantGroup; 183 | children = ( 184 | 18B0EE2C1C97B4F700BD3B93 /* Base */, 185 | ); 186 | name = LaunchScreen.storyboard; 187 | sourceTree = ""; 188 | }; 189 | /* End PBXVariantGroup section */ 190 | 191 | /* Begin XCBuildConfiguration section */ 192 | 18B0EE2F1C97B4F700BD3B93 /* Debug */ = { 193 | isa = XCBuildConfiguration; 194 | buildSettings = { 195 | ALWAYS_SEARCH_USER_PATHS = NO; 196 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 197 | CLANG_CXX_LIBRARY = "libc++"; 198 | CLANG_ENABLE_MODULES = YES; 199 | CLANG_ENABLE_OBJC_ARC = YES; 200 | CLANG_WARN_BOOL_CONVERSION = YES; 201 | CLANG_WARN_CONSTANT_CONVERSION = YES; 202 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 203 | CLANG_WARN_EMPTY_BODY = YES; 204 | CLANG_WARN_ENUM_CONVERSION = YES; 205 | CLANG_WARN_INT_CONVERSION = YES; 206 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 207 | CLANG_WARN_UNREACHABLE_CODE = YES; 208 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 209 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 210 | COPY_PHASE_STRIP = NO; 211 | DEBUG_INFORMATION_FORMAT = dwarf; 212 | ENABLE_STRICT_OBJC_MSGSEND = YES; 213 | ENABLE_TESTABILITY = YES; 214 | GCC_C_LANGUAGE_STANDARD = gnu99; 215 | GCC_DYNAMIC_NO_PIC = NO; 216 | GCC_NO_COMMON_BLOCKS = YES; 217 | GCC_OPTIMIZATION_LEVEL = 0; 218 | GCC_PREPROCESSOR_DEFINITIONS = ( 219 | "DEBUG=1", 220 | "$(inherited)", 221 | ); 222 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 223 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 224 | GCC_WARN_UNDECLARED_SELECTOR = YES; 225 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 226 | GCC_WARN_UNUSED_FUNCTION = YES; 227 | GCC_WARN_UNUSED_VARIABLE = YES; 228 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 229 | MTL_ENABLE_DEBUG_INFO = YES; 230 | ONLY_ACTIVE_ARCH = YES; 231 | SDKROOT = iphoneos; 232 | }; 233 | name = Debug; 234 | }; 235 | 18B0EE301C97B4F700BD3B93 /* Release */ = { 236 | isa = XCBuildConfiguration; 237 | buildSettings = { 238 | ALWAYS_SEARCH_USER_PATHS = NO; 239 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 240 | CLANG_CXX_LIBRARY = "libc++"; 241 | CLANG_ENABLE_MODULES = YES; 242 | CLANG_ENABLE_OBJC_ARC = YES; 243 | CLANG_WARN_BOOL_CONVERSION = YES; 244 | CLANG_WARN_CONSTANT_CONVERSION = YES; 245 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 246 | CLANG_WARN_EMPTY_BODY = YES; 247 | CLANG_WARN_ENUM_CONVERSION = YES; 248 | CLANG_WARN_INT_CONVERSION = YES; 249 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 250 | CLANG_WARN_UNREACHABLE_CODE = YES; 251 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 252 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 253 | COPY_PHASE_STRIP = NO; 254 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 255 | ENABLE_NS_ASSERTIONS = NO; 256 | ENABLE_STRICT_OBJC_MSGSEND = YES; 257 | GCC_C_LANGUAGE_STANDARD = gnu99; 258 | GCC_NO_COMMON_BLOCKS = YES; 259 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 260 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 261 | GCC_WARN_UNDECLARED_SELECTOR = YES; 262 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 263 | GCC_WARN_UNUSED_FUNCTION = YES; 264 | GCC_WARN_UNUSED_VARIABLE = YES; 265 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 266 | MTL_ENABLE_DEBUG_INFO = NO; 267 | SDKROOT = iphoneos; 268 | VALIDATE_PRODUCT = YES; 269 | }; 270 | name = Release; 271 | }; 272 | 18B0EE321C97B4F700BD3B93 /* Debug */ = { 273 | isa = XCBuildConfiguration; 274 | buildSettings = { 275 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 276 | INFOPLIST_FILE = HYLabelDemoOC/Info.plist; 277 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 278 | PRODUCT_BUNDLE_IDENTIFIER = com.520it.HYLabelDemoOC; 279 | PRODUCT_NAME = "$(TARGET_NAME)"; 280 | }; 281 | name = Debug; 282 | }; 283 | 18B0EE331C97B4F700BD3B93 /* Release */ = { 284 | isa = XCBuildConfiguration; 285 | buildSettings = { 286 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 287 | INFOPLIST_FILE = HYLabelDemoOC/Info.plist; 288 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 289 | PRODUCT_BUNDLE_IDENTIFIER = com.520it.HYLabelDemoOC; 290 | PRODUCT_NAME = "$(TARGET_NAME)"; 291 | }; 292 | name = Release; 293 | }; 294 | /* End XCBuildConfiguration section */ 295 | 296 | /* Begin XCConfigurationList section */ 297 | 18B0EE151C97B4F700BD3B93 /* Build configuration list for PBXProject "HYLabelDemoOC" */ = { 298 | isa = XCConfigurationList; 299 | buildConfigurations = ( 300 | 18B0EE2F1C97B4F700BD3B93 /* Debug */, 301 | 18B0EE301C97B4F700BD3B93 /* Release */, 302 | ); 303 | defaultConfigurationIsVisible = 0; 304 | defaultConfigurationName = Release; 305 | }; 306 | 18B0EE311C97B4F700BD3B93 /* Build configuration list for PBXNativeTarget "HYLabelDemoOC" */ = { 307 | isa = XCConfigurationList; 308 | buildConfigurations = ( 309 | 18B0EE321C97B4F700BD3B93 /* Debug */, 310 | 18B0EE331C97B4F700BD3B93 /* Release */, 311 | ); 312 | defaultConfigurationIsVisible = 0; 313 | }; 314 | /* End XCConfigurationList section */ 315 | }; 316 | rootObject = 18B0EE121C97B4F700BD3B93 /* Project object */; 317 | } 318 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // HYLabelDemoOC 4 | // 5 | // Created by apple on 16/3/15. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // HYLabelDemoOC 4 | // 5 | // Created by apple on 16/3/15. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // 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. 25 | // 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. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // 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. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // 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. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // 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. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/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 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/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 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/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 | 40 | 41 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/Source/HYLabel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HYLabel.swift 3 | // HYLabel 4 | // 5 | // Created by apple on 16/3/8. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | enum TapHandlerType : Int { 12 | case noneHandle = 0 13 | case userHandle = 1 14 | case topicHandle = 2 15 | case linkHandle = 3 16 | } 17 | 18 | public class HYLabel: UILabel { 19 | 20 | // MARK:- 属性 21 | // 重写系统的属性 22 | override public var text : String? { 23 | didSet { 24 | prepareText() 25 | } 26 | } 27 | 28 | override public var attributedText: NSAttributedString? { 29 | didSet { 30 | prepareText() 31 | } 32 | } 33 | 34 | override public var font: UIFont! { 35 | didSet { 36 | prepareText() 37 | } 38 | } 39 | 40 | override public var textColor: UIColor! { 41 | didSet { 42 | prepareText() 43 | } 44 | } 45 | 46 | // 懒加载属性 47 | private lazy var textStorage : NSTextStorage = NSTextStorage() // NSMutableAttributeString的子类 48 | private lazy var layoutManager : NSLayoutManager = NSLayoutManager() // 布局管理者 49 | private lazy var textContainer : NSTextContainer = NSTextContainer() // 容器,需要设置容器的大小 50 | 51 | // 用于记录下标值 52 | private lazy var linkRanges : [NSRange] = [NSRange]() 53 | private lazy var userRanges : [NSRange] = [NSRange]() 54 | private lazy var topicRanges : [NSRange] = [NSRange]() 55 | 56 | // 用于记录用户选中的range 57 | private var selectedRange : NSRange? 58 | 59 | // 用户记录点击还是松开 60 | private var isSelected : Bool = false 61 | 62 | // 闭包属性,用于回调 63 | private var tapHandlerType : TapHandlerType = TapHandlerType.noneHandle 64 | 65 | public typealias HYTapHandler = (HYLabel, String, NSRange) -> Void 66 | public var linkTapHandler : HYTapHandler? 67 | public var topicTapHandler : HYTapHandler? 68 | public var userTapHandler : HYTapHandler? 69 | 70 | // MARK:- 构造函数 71 | override init(frame: CGRect) { 72 | super.init(frame: frame) 73 | 74 | prepareTextSystem() 75 | } 76 | 77 | required public init?(coder aDecoder: NSCoder) { 78 | super.init(coder: aDecoder) 79 | 80 | prepareTextSystem() 81 | } 82 | 83 | // MARK:- 布局子控件 84 | override public func layoutSubviews() { 85 | super.layoutSubviews() 86 | 87 | // 设置容器的大小为Label的尺寸 88 | textContainer.size = frame.size 89 | } 90 | 91 | // MARK:- 重写drawTextInRect方法 92 | override public func drawTextInRect(rect: CGRect) { 93 | // 1.绘制背景 94 | if selectedRange != nil { 95 | // 2.0.确定颜色 96 | let selectedColor = isSelected ? UIColor(white: 0.7, alpha: 0.2) : UIColor.clearColor() 97 | 98 | // 2.1.设置颜色 99 | textStorage.addAttribute(NSBackgroundColorAttributeName, value: selectedColor, range: selectedRange!) 100 | 101 | // 2.2.绘制背景 102 | layoutManager.drawBackgroundForGlyphRange(selectedRange!, atPoint: CGPoint(x: 0, y: 0)) 103 | } 104 | 105 | // 2.绘制字形 106 | // 需要绘制的范围 107 | let range = NSRange(location: 0, length: textStorage.length) 108 | layoutManager.drawGlyphsForGlyphRange(range, atPoint: CGPointZero) 109 | } 110 | } 111 | 112 | extension HYLabel { 113 | /// 准备文本系统 114 | private func prepareTextSystem() { 115 | // 0.准备文本 116 | prepareText() 117 | 118 | // 1.将布局添加到storeage中 119 | textStorage.addLayoutManager(layoutManager) 120 | 121 | // 2.将容器添加到布局中 122 | layoutManager.addTextContainer(textContainer) 123 | 124 | // 3.让label可以和用户交互 125 | userInteractionEnabled = true 126 | 127 | // 4.设置间距为0 128 | textContainer.lineFragmentPadding = 0 129 | } 130 | 131 | /// 准备文本 132 | private func prepareText() { 133 | // 1.准备字符串 134 | var attrString : NSAttributedString? 135 | if attributedText != nil { 136 | attrString = attributedText 137 | } else if text != nil { 138 | attrString = NSAttributedString(string: text!) 139 | } else { 140 | attrString = NSAttributedString(string: "") 141 | } 142 | 143 | selectedRange = nil 144 | 145 | // 2.设置换行模型 146 | let attrStringM = addLineBreak(attrString!) 147 | 148 | attrStringM.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: attrStringM.length)) 149 | 150 | // 3.设置textStorage的内容 151 | textStorage.setAttributedString(attrStringM) 152 | 153 | // 4.匹配URL 154 | if let linkRanges = getLinkRanges() { 155 | self.linkRanges = linkRanges 156 | for range in linkRanges { 157 | textStorage.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: range) 158 | } 159 | } 160 | 161 | // 5.匹配@用户 162 | if let userRanges = getRanges("@[\\u4e00-\\u9fa5a-zA-Z0-9_-]*") { 163 | self.userRanges = userRanges 164 | for range in userRanges { 165 | textStorage.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: range) 166 | } 167 | } 168 | 169 | 170 | // 6.匹配话题## 171 | if let topicRanges = getRanges("#.*?#") { 172 | self.topicRanges = topicRanges 173 | for range in topicRanges { 174 | textStorage.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: range) 175 | } 176 | } 177 | 178 | setNeedsDisplay() 179 | } 180 | } 181 | 182 | 183 | // MARK:- 字符串匹配封装 184 | extension HYLabel { 185 | private func getRanges(pattern : String) -> [NSRange]? { 186 | // 创建正则表达式对象 187 | guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { 188 | return nil 189 | } 190 | 191 | return getRangesFromResult(regex) 192 | } 193 | 194 | private func getLinkRanges() -> [NSRange]? { 195 | // 创建正则表达式 196 | guard let detector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue) else { 197 | return nil 198 | } 199 | 200 | return getRangesFromResult(detector) 201 | } 202 | 203 | private func getRangesFromResult(regex : NSRegularExpression) -> [NSRange] { 204 | // 1.匹配结果 205 | let results = regex.matchesInString(textStorage.string, options: [], range: NSRange(location: 0, length: textStorage.length)) 206 | 207 | // 2.遍历结果 208 | var ranges = [NSRange]() 209 | for res in results { 210 | ranges.append(res.range) 211 | } 212 | 213 | return ranges 214 | } 215 | } 216 | 217 | 218 | // MARK:- 点击交互的封装 219 | extension HYLabel { 220 | override public func touchesBegan(touches: Set, withEvent event: UIEvent?) { 221 | // 0.记录点击 222 | isSelected = true 223 | 224 | // 1.获取用户点击的点 225 | let selectedPoint = touches.first!.locationInView(self) 226 | 227 | // 2.获取该点所在的字符串的range 228 | selectedRange = getSelectRange(selectedPoint) 229 | 230 | // 3.是否处理了事件 231 | if selectedRange == nil { 232 | super.touchesBegan(touches, withEvent: event) 233 | } 234 | } 235 | 236 | override public func touchesEnded(touches: Set, withEvent event: UIEvent?) { 237 | if selectedRange == nil { 238 | super.touchesBegan(touches, withEvent: event) 239 | return 240 | } 241 | 242 | // 0.记录松开 243 | isSelected = false 244 | 245 | // 2.重新绘制 246 | setNeedsDisplay() 247 | 248 | // 3.取出内容 249 | let contentText = (textStorage.string as NSString).substringWithRange(selectedRange!) 250 | 251 | // 3.回调 252 | switch tapHandlerType { 253 | case .linkHandle: 254 | if linkTapHandler != nil { 255 | linkTapHandler!(self, contentText, selectedRange!) 256 | } 257 | case .topicHandle: 258 | if topicTapHandler != nil { 259 | topicTapHandler!(self, contentText, selectedRange!) 260 | } 261 | case .userHandle: 262 | if userTapHandler != nil { 263 | userTapHandler!(self, contentText, selectedRange!) 264 | } 265 | default: 266 | break 267 | } 268 | } 269 | 270 | private func getSelectRange(selectedPoint : CGPoint) -> NSRange? { 271 | // 0.如果属性字符串为nil,则不需要判断 272 | if textStorage.length == 0 { 273 | return nil 274 | } 275 | 276 | // 1.获取选中点所在的下标值(index) 277 | let index = layoutManager.glyphIndexForPoint(selectedPoint, inTextContainer: textContainer) 278 | 279 | // 2.判断下标在什么内 280 | // 2.1.判断是否是一个链接 281 | for linkRange in linkRanges { 282 | if index > linkRange.location && index < linkRange.location + linkRange.length { 283 | setNeedsDisplay() 284 | tapHandlerType = .linkHandle 285 | return linkRange 286 | } 287 | } 288 | 289 | // 2.2.判断是否是一个@用户 290 | for userRange in userRanges { 291 | if index > userRange.location && index < userRange.location + userRange.length { 292 | setNeedsDisplay() 293 | tapHandlerType = .userHandle 294 | return userRange 295 | } 296 | } 297 | 298 | // 2.3.判断是否是一个#话题# 299 | for topicRange in topicRanges { 300 | if index > topicRange.location && index < topicRange.location + topicRange.length { 301 | setNeedsDisplay() 302 | tapHandlerType = .topicHandle 303 | return topicRange 304 | } 305 | } 306 | 307 | return nil 308 | } 309 | } 310 | 311 | // MARK:- 补充 312 | extension HYLabel { 313 | /// 如果用户没有设置lineBreak,则所有内容会绘制到同一行中,因此需要主动设置 314 | private func addLineBreak(attrString: NSAttributedString) -> NSMutableAttributedString { 315 | let attrStringM = NSMutableAttributedString(attributedString: attrString) 316 | 317 | if attrStringM.length == 0 { 318 | return attrStringM 319 | } 320 | 321 | var range = NSRange(location: 0, length: 0) 322 | var attributes = attrStringM.attributesAtIndex(0, effectiveRange: &range) 323 | var paragraphStyle = attributes[NSParagraphStyleAttributeName] as? NSMutableParagraphStyle 324 | 325 | if paragraphStyle != nil { 326 | paragraphStyle!.lineBreakMode = NSLineBreakMode.ByWordWrapping 327 | } else { 328 | paragraphStyle = NSMutableParagraphStyle() 329 | paragraphStyle!.lineBreakMode = NSLineBreakMode.ByWordWrapping 330 | attributes[NSParagraphStyleAttributeName] = paragraphStyle 331 | 332 | attrStringM.setAttributes(attributes, range: range) 333 | } 334 | 335 | return attrStringM 336 | } 337 | } 338 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // HYLabelDemoOC 4 | // 5 | // Created by apple on 16/3/15. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // HYLabelDemoOC 4 | // 5 | // Created by apple on 16/3/15. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "HYLabelDemoOC-Swift.h" 11 | 12 | @interface ViewController () 13 | 14 | @property (weak, nonatomic) IBOutlet HYLabel *demoLabel; 15 | 16 | @end 17 | 18 | @implementation ViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | 23 | self.demoLabel.userTapHandler = ^(HYLabel *label, NSString *user, NSRange range){ 24 | NSLog(@"%@", label); 25 | NSLog(@"%@", user); 26 | NSLog(@"%@", NSStringFromRange(range)); 27 | }; 28 | 29 | self.demoLabel.linkTapHandler = ^(HYLabel *label, NSString *link, NSRange range){ 30 | NSLog(@"%@", label); 31 | NSLog(@"%@", link); 32 | NSLog(@"%@", NSStringFromRange(range)); 33 | }; 34 | 35 | self.demoLabel.userTapHandler = ^(HYLabel *label, NSString *topic, NSRange range){ 36 | NSLog(@"%@", label); 37 | NSLog(@"%@", topic); 38 | NSLog(@"%@", NSStringFromRange(range)); 39 | }; 40 | } 41 | 42 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 43 | { 44 | self.demoLabel.text = @"作者:@coderwhy 话题:#Label字符串识别# 网址:http://www.520it.com"; 45 | } 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /HYLabelDemoOC/HYLabelDemoOC/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // HYLabelDemoOC 4 | // 5 | // Created by apple on 16/3/15. 6 | // Copyright © 2016年 xiaomage. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 coderwhy 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 | # HYLabel 2 | ### 框架的作用 3 | * 用于识别Label中的@用户-话题##-链接 4 | * 框架地址:[https://github.com/coderwhy/HYLabel](https://github.com/coderwhy/HYLabel) 5 | 6 | ### 使用效果 7 | * 如图所示 8 | * 并且可以监听点击
9 | * ![image](https://raw.githubusercontent.com/coderwhy/HYLabel/master/ReadmeImage/image1.png) 10 | 11 | 12 | ### 使用方式 13 | * 导入框架 14 | * 直接将HYLabel.swift拖入项目 15 | * 该框架支持cocoapods:pod 'HYLabel', '~> 1.0.1' 16 | * 将UIlabel修改为HYLabel 17 | * 按照下列方式监听点击即可 18 | * OC监听方式 19 | 20 | ```objc 21 | 22 | self.demoLabel.userTapHandler = ^(HYLabel *label, NSString *user, NSRange range){ 23 | NSLog(@"%@", label); 24 | NSLog(@"%@", user); 25 | NSLog(@"%@", NSStringFromRange(range)); 26 | }; 27 | 28 | self.demoLabel.linkTapHandler = ^(HYLabel *label, NSString *link, NSRange range){ 29 | NSLog(@"%@", label); 30 | NSLog(@"%@", link); 31 | NSLog(@"%@", NSStringFromRange(range)); 32 | }; 33 | 34 | self.demoLabel.userTapHandler = ^(HYLabel *label, NSString *topic, NSRange range){ 35 | NSLog(@"%@", label); 36 | NSLog(@"%@", topic); 37 | NSLog(@"%@", NSStringFromRange(range)); 38 | }; 39 | ``` 40 | 41 | * swift监听方式 42 | 43 | ```swift 44 | 45 | // 监听@谁谁谁的点击 46 | demoLabel.userTapHandler = { (label, user, range) in 47 | print(label) 48 | print(user) 49 | print(range) 50 | } 51 | 52 | // 监听链接的点击 53 | demoLabel.linkTapHandler = { (label, link, range) in 54 | print(label) 55 | print(link) 56 | print(range) 57 | } 58 | 59 | // 监听话题的点击 60 | demoLabel.topicTapHandler = { (label, topic, range) in 61 | print(label) 62 | print(topic) 63 | print(range) 64 | } 65 | ``` 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /ReadmeImage/image1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coderwhy/HYLabel/99ee0531968082331835a75797e3eb40567696f8/ReadmeImage/image1.png --------------------------------------------------------------------------------