├── .gitignore ├── .travis.yml ├── AttributedLabel.podspec ├── AttributedLabel.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcshareddata │ └── xcschemes │ ├── AttributedLabel-tvOS.xcscheme │ ├── AttributedLabel.xcscheme │ └── AttributedLabelTests.xcscheme ├── AttributedLabel ├── AttributedLabel.h ├── AttributedLabel.swift └── Info.plist ├── AttributedLabelExample ├── AttributedLabelExample.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── AttributedLabelExample │ ├── AppDelegate.swift │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ └── Main.storyboard │ ├── Benchmark.swift │ ├── CollectionViewController.swift │ ├── CustomizeViewController.swift │ ├── Info.plist │ └── LaunchScreen.storyboard ├── AttributedLabelTests ├── AttributedLabelTests.swift └── Info.plist ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: objective-c 2 | os: osx 3 | osx_image: xcode10.2 4 | before_install: 5 | - gem install xcpretty 6 | before_script: 7 | - set -o pipefail 8 | script: 9 | - xcodebuild test -project ./AttributedLabel.xcodeproj -scheme AttributedLabelTests -configuration Debug -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 8' | xcpretty -c 10 | notifications: 11 | email: false 12 | -------------------------------------------------------------------------------- /AttributedLabel.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "AttributedLabel" 3 | s.version = "3.0.0" 4 | s.summary = "Easy to use, fast, and higher performance than UILabel." 5 | s.homepage = "https://github.com/KyoheiG3/AttributedLabel" 6 | s.license = { :type => "MIT", :file => "LICENSE" } 7 | s.author = { "Kyohei Ito" => "je.suis.kyohei@gmail.com" } 8 | s.swift_version = '5.0' 9 | s.ios.deployment_target = '8.0' 10 | s.tvos.deployment_target = '9.0' 11 | s.source = { :git => "https://github.com/KyoheiG3/AttributedLabel.git", :tag => s.version.to_s } 12 | s.source_files = "AttributedLabel/**/*.{h,swift}" 13 | s.requires_arc = true 14 | s.frameworks = "UIKit" 15 | end 16 | -------------------------------------------------------------------------------- /AttributedLabel.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | C75201FF1F73458A005E1197 /* AttributedLabelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75201FE1F73458A005E1197 /* AttributedLabelTests.swift */; }; 11 | C75202011F73458A005E1197 /* AttributedLabel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C7F60DC51B582DCC006761A8 /* AttributedLabel.framework */; }; 12 | C7F60DC91B582DCC006761A8 /* AttributedLabel.h in Headers */ = {isa = PBXBuildFile; fileRef = C7F60DC81B582DCC006761A8 /* AttributedLabel.h */; settings = {ATTRIBUTES = (Public, ); }; }; 13 | C7F60DD11B582E1D006761A8 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7F60DD01B582E1D006761A8 /* AttributedLabel.swift */; }; 14 | EDC739CD1F7B582300206679 /* AttributedLabel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EDC739C41F7B582300206679 /* AttributedLabel.framework */; }; 15 | EDC739DB1F7B584600206679 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7F60DD01B582E1D006761A8 /* AttributedLabel.swift */; }; 16 | EDC739DC1F7B585700206679 /* AttributedLabelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75201FE1F73458A005E1197 /* AttributedLabelTests.swift */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXContainerItemProxy section */ 20 | C75202021F73458A005E1197 /* PBXContainerItemProxy */ = { 21 | isa = PBXContainerItemProxy; 22 | containerPortal = C7F60DBC1B582DCC006761A8 /* Project object */; 23 | proxyType = 1; 24 | remoteGlobalIDString = C7F60DC41B582DCC006761A8; 25 | remoteInfo = AttributedLabel; 26 | }; 27 | EDC739CE1F7B582300206679 /* PBXContainerItemProxy */ = { 28 | isa = PBXContainerItemProxy; 29 | containerPortal = C7F60DBC1B582DCC006761A8 /* Project object */; 30 | proxyType = 1; 31 | remoteGlobalIDString = EDC739C31F7B582300206679; 32 | remoteInfo = "AttributedLabel-tvOS"; 33 | }; 34 | /* End PBXContainerItemProxy section */ 35 | 36 | /* Begin PBXFileReference section */ 37 | C75201FC1F73458A005E1197 /* AttributedLabelTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AttributedLabelTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 38 | C75201FE1F73458A005E1197 /* AttributedLabelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedLabelTests.swift; sourceTree = ""; }; 39 | C75202001F73458A005E1197 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 40 | C7F60DC51B582DCC006761A8 /* AttributedLabel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AttributedLabel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 41 | C7F60DC81B582DCC006761A8 /* AttributedLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AttributedLabel.h; sourceTree = ""; }; 42 | C7F60DCA1B582DCC006761A8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 43 | C7F60DD01B582E1D006761A8 /* AttributedLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttributedLabel.swift; sourceTree = ""; }; 44 | EDC739C41F7B582300206679 /* AttributedLabel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AttributedLabel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 45 | EDC739CC1F7B582300206679 /* AttributedLabel-tvOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "AttributedLabel-tvOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 46 | /* End PBXFileReference section */ 47 | 48 | /* Begin PBXFrameworksBuildPhase section */ 49 | C75201F91F73458A005E1197 /* Frameworks */ = { 50 | isa = PBXFrameworksBuildPhase; 51 | buildActionMask = 2147483647; 52 | files = ( 53 | C75202011F73458A005E1197 /* AttributedLabel.framework in Frameworks */, 54 | ); 55 | runOnlyForDeploymentPostprocessing = 0; 56 | }; 57 | C7F60DC11B582DCC006761A8 /* Frameworks */ = { 58 | isa = PBXFrameworksBuildPhase; 59 | buildActionMask = 2147483647; 60 | files = ( 61 | ); 62 | runOnlyForDeploymentPostprocessing = 0; 63 | }; 64 | EDC739C01F7B582300206679 /* Frameworks */ = { 65 | isa = PBXFrameworksBuildPhase; 66 | buildActionMask = 2147483647; 67 | files = ( 68 | ); 69 | runOnlyForDeploymentPostprocessing = 0; 70 | }; 71 | EDC739C91F7B582300206679 /* Frameworks */ = { 72 | isa = PBXFrameworksBuildPhase; 73 | buildActionMask = 2147483647; 74 | files = ( 75 | EDC739CD1F7B582300206679 /* AttributedLabel.framework in Frameworks */, 76 | ); 77 | runOnlyForDeploymentPostprocessing = 0; 78 | }; 79 | /* End PBXFrameworksBuildPhase section */ 80 | 81 | /* Begin PBXGroup section */ 82 | C75201FD1F73458A005E1197 /* AttributedLabelTests */ = { 83 | isa = PBXGroup; 84 | children = ( 85 | C75201FE1F73458A005E1197 /* AttributedLabelTests.swift */, 86 | C75202001F73458A005E1197 /* Info.plist */, 87 | ); 88 | path = AttributedLabelTests; 89 | sourceTree = ""; 90 | }; 91 | C7F60DBB1B582DCC006761A8 = { 92 | isa = PBXGroup; 93 | children = ( 94 | C7F60DC71B582DCC006761A8 /* AttributedLabel */, 95 | C75201FD1F73458A005E1197 /* AttributedLabelTests */, 96 | C7F60DC61B582DCC006761A8 /* Products */, 97 | ); 98 | sourceTree = ""; 99 | }; 100 | C7F60DC61B582DCC006761A8 /* Products */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | C7F60DC51B582DCC006761A8 /* AttributedLabel.framework */, 104 | C75201FC1F73458A005E1197 /* AttributedLabelTests.xctest */, 105 | EDC739C41F7B582300206679 /* AttributedLabel.framework */, 106 | EDC739CC1F7B582300206679 /* AttributedLabel-tvOSTests.xctest */, 107 | ); 108 | name = Products; 109 | sourceTree = ""; 110 | }; 111 | C7F60DC71B582DCC006761A8 /* AttributedLabel */ = { 112 | isa = PBXGroup; 113 | children = ( 114 | C7F60DC81B582DCC006761A8 /* AttributedLabel.h */, 115 | C7F60DD01B582E1D006761A8 /* AttributedLabel.swift */, 116 | C7F60DCA1B582DCC006761A8 /* Info.plist */, 117 | ); 118 | path = AttributedLabel; 119 | sourceTree = ""; 120 | }; 121 | /* End PBXGroup section */ 122 | 123 | /* Begin PBXHeadersBuildPhase section */ 124 | C7F60DC21B582DCC006761A8 /* Headers */ = { 125 | isa = PBXHeadersBuildPhase; 126 | buildActionMask = 2147483647; 127 | files = ( 128 | C7F60DC91B582DCC006761A8 /* AttributedLabel.h in Headers */, 129 | ); 130 | runOnlyForDeploymentPostprocessing = 0; 131 | }; 132 | EDC739C11F7B582300206679 /* Headers */ = { 133 | isa = PBXHeadersBuildPhase; 134 | buildActionMask = 2147483647; 135 | files = ( 136 | ); 137 | runOnlyForDeploymentPostprocessing = 0; 138 | }; 139 | /* End PBXHeadersBuildPhase section */ 140 | 141 | /* Begin PBXNativeTarget section */ 142 | C75201FB1F73458A005E1197 /* AttributedLabelTests */ = { 143 | isa = PBXNativeTarget; 144 | buildConfigurationList = C75202061F73458A005E1197 /* Build configuration list for PBXNativeTarget "AttributedLabelTests" */; 145 | buildPhases = ( 146 | C75201F81F73458A005E1197 /* Sources */, 147 | C75201F91F73458A005E1197 /* Frameworks */, 148 | C75201FA1F73458A005E1197 /* Resources */, 149 | ); 150 | buildRules = ( 151 | ); 152 | dependencies = ( 153 | C75202031F73458A005E1197 /* PBXTargetDependency */, 154 | ); 155 | name = AttributedLabelTests; 156 | productName = AttributedLabelTests; 157 | productReference = C75201FC1F73458A005E1197 /* AttributedLabelTests.xctest */; 158 | productType = "com.apple.product-type.bundle.unit-test"; 159 | }; 160 | C7F60DC41B582DCC006761A8 /* AttributedLabel */ = { 161 | isa = PBXNativeTarget; 162 | buildConfigurationList = C7F60DCD1B582DCC006761A8 /* Build configuration list for PBXNativeTarget "AttributedLabel" */; 163 | buildPhases = ( 164 | C7F60DC01B582DCC006761A8 /* Sources */, 165 | C7F60DC11B582DCC006761A8 /* Frameworks */, 166 | C7F60DC21B582DCC006761A8 /* Headers */, 167 | C7F60DC31B582DCC006761A8 /* Resources */, 168 | ); 169 | buildRules = ( 170 | ); 171 | dependencies = ( 172 | ); 173 | name = AttributedLabel; 174 | productName = AttributedLabel; 175 | productReference = C7F60DC51B582DCC006761A8 /* AttributedLabel.framework */; 176 | productType = "com.apple.product-type.framework"; 177 | }; 178 | EDC739C31F7B582300206679 /* AttributedLabel-tvOS */ = { 179 | isa = PBXNativeTarget; 180 | buildConfigurationList = EDC739D91F7B582300206679 /* Build configuration list for PBXNativeTarget "AttributedLabel-tvOS" */; 181 | buildPhases = ( 182 | EDC739BF1F7B582300206679 /* Sources */, 183 | EDC739C01F7B582300206679 /* Frameworks */, 184 | EDC739C11F7B582300206679 /* Headers */, 185 | EDC739C21F7B582300206679 /* Resources */, 186 | ); 187 | buildRules = ( 188 | ); 189 | dependencies = ( 190 | ); 191 | name = "AttributedLabel-tvOS"; 192 | productName = "AttributedLabel-tvOS"; 193 | productReference = EDC739C41F7B582300206679 /* AttributedLabel.framework */; 194 | productType = "com.apple.product-type.framework"; 195 | }; 196 | EDC739CB1F7B582300206679 /* AttributedLabel-tvOSTests */ = { 197 | isa = PBXNativeTarget; 198 | buildConfigurationList = EDC739DA1F7B582300206679 /* Build configuration list for PBXNativeTarget "AttributedLabel-tvOSTests" */; 199 | buildPhases = ( 200 | EDC739C81F7B582300206679 /* Sources */, 201 | EDC739C91F7B582300206679 /* Frameworks */, 202 | EDC739CA1F7B582300206679 /* Resources */, 203 | ); 204 | buildRules = ( 205 | ); 206 | dependencies = ( 207 | EDC739CF1F7B582300206679 /* PBXTargetDependency */, 208 | ); 209 | name = "AttributedLabel-tvOSTests"; 210 | productName = "AttributedLabel-tvOSTests"; 211 | productReference = EDC739CC1F7B582300206679 /* AttributedLabel-tvOSTests.xctest */; 212 | productType = "com.apple.product-type.bundle.unit-test"; 213 | }; 214 | /* End PBXNativeTarget section */ 215 | 216 | /* Begin PBXProject section */ 217 | C7F60DBC1B582DCC006761A8 /* Project object */ = { 218 | isa = PBXProject; 219 | attributes = { 220 | LastSwiftUpdateCheck = 0900; 221 | LastUpgradeCheck = 1020; 222 | ORGANIZATIONNAME = "Kyohei Ito"; 223 | TargetAttributes = { 224 | C75201FB1F73458A005E1197 = { 225 | CreatedOnToolsVersion = 9.0; 226 | LastSwiftMigration = 1020; 227 | ProvisioningStyle = Automatic; 228 | }; 229 | C7F60DC41B582DCC006761A8 = { 230 | CreatedOnToolsVersion = 7.0; 231 | LastSwiftMigration = 1020; 232 | }; 233 | EDC739C31F7B582300206679 = { 234 | CreatedOnToolsVersion = 9.0; 235 | LastSwiftMigration = 1020; 236 | ProvisioningStyle = Automatic; 237 | }; 238 | EDC739CB1F7B582300206679 = { 239 | CreatedOnToolsVersion = 9.0; 240 | LastSwiftMigration = 1020; 241 | ProvisioningStyle = Automatic; 242 | }; 243 | }; 244 | }; 245 | buildConfigurationList = C7F60DBF1B582DCC006761A8 /* Build configuration list for PBXProject "AttributedLabel" */; 246 | compatibilityVersion = "Xcode 3.2"; 247 | developmentRegion = en; 248 | hasScannedForEncodings = 0; 249 | knownRegions = ( 250 | en, 251 | ); 252 | mainGroup = C7F60DBB1B582DCC006761A8; 253 | productRefGroup = C7F60DC61B582DCC006761A8 /* Products */; 254 | projectDirPath = ""; 255 | projectRoot = ""; 256 | targets = ( 257 | C7F60DC41B582DCC006761A8 /* AttributedLabel */, 258 | C75201FB1F73458A005E1197 /* AttributedLabelTests */, 259 | EDC739C31F7B582300206679 /* AttributedLabel-tvOS */, 260 | EDC739CB1F7B582300206679 /* AttributedLabel-tvOSTests */, 261 | ); 262 | }; 263 | /* End PBXProject section */ 264 | 265 | /* Begin PBXResourcesBuildPhase section */ 266 | C75201FA1F73458A005E1197 /* Resources */ = { 267 | isa = PBXResourcesBuildPhase; 268 | buildActionMask = 2147483647; 269 | files = ( 270 | ); 271 | runOnlyForDeploymentPostprocessing = 0; 272 | }; 273 | C7F60DC31B582DCC006761A8 /* Resources */ = { 274 | isa = PBXResourcesBuildPhase; 275 | buildActionMask = 2147483647; 276 | files = ( 277 | ); 278 | runOnlyForDeploymentPostprocessing = 0; 279 | }; 280 | EDC739C21F7B582300206679 /* Resources */ = { 281 | isa = PBXResourcesBuildPhase; 282 | buildActionMask = 2147483647; 283 | files = ( 284 | ); 285 | runOnlyForDeploymentPostprocessing = 0; 286 | }; 287 | EDC739CA1F7B582300206679 /* Resources */ = { 288 | isa = PBXResourcesBuildPhase; 289 | buildActionMask = 2147483647; 290 | files = ( 291 | ); 292 | runOnlyForDeploymentPostprocessing = 0; 293 | }; 294 | /* End PBXResourcesBuildPhase section */ 295 | 296 | /* Begin PBXSourcesBuildPhase section */ 297 | C75201F81F73458A005E1197 /* Sources */ = { 298 | isa = PBXSourcesBuildPhase; 299 | buildActionMask = 2147483647; 300 | files = ( 301 | C75201FF1F73458A005E1197 /* AttributedLabelTests.swift in Sources */, 302 | ); 303 | runOnlyForDeploymentPostprocessing = 0; 304 | }; 305 | C7F60DC01B582DCC006761A8 /* Sources */ = { 306 | isa = PBXSourcesBuildPhase; 307 | buildActionMask = 2147483647; 308 | files = ( 309 | C7F60DD11B582E1D006761A8 /* AttributedLabel.swift in Sources */, 310 | ); 311 | runOnlyForDeploymentPostprocessing = 0; 312 | }; 313 | EDC739BF1F7B582300206679 /* Sources */ = { 314 | isa = PBXSourcesBuildPhase; 315 | buildActionMask = 2147483647; 316 | files = ( 317 | EDC739DB1F7B584600206679 /* AttributedLabel.swift in Sources */, 318 | ); 319 | runOnlyForDeploymentPostprocessing = 0; 320 | }; 321 | EDC739C81F7B582300206679 /* Sources */ = { 322 | isa = PBXSourcesBuildPhase; 323 | buildActionMask = 2147483647; 324 | files = ( 325 | EDC739DC1F7B585700206679 /* AttributedLabelTests.swift in Sources */, 326 | ); 327 | runOnlyForDeploymentPostprocessing = 0; 328 | }; 329 | /* End PBXSourcesBuildPhase section */ 330 | 331 | /* Begin PBXTargetDependency section */ 332 | C75202031F73458A005E1197 /* PBXTargetDependency */ = { 333 | isa = PBXTargetDependency; 334 | target = C7F60DC41B582DCC006761A8 /* AttributedLabel */; 335 | targetProxy = C75202021F73458A005E1197 /* PBXContainerItemProxy */; 336 | }; 337 | EDC739CF1F7B582300206679 /* PBXTargetDependency */ = { 338 | isa = PBXTargetDependency; 339 | target = EDC739C31F7B582300206679 /* AttributedLabel-tvOS */; 340 | targetProxy = EDC739CE1F7B582300206679 /* PBXContainerItemProxy */; 341 | }; 342 | /* End PBXTargetDependency section */ 343 | 344 | /* Begin XCBuildConfiguration section */ 345 | C75202041F73458A005E1197 /* Debug */ = { 346 | isa = XCBuildConfiguration; 347 | buildSettings = { 348 | CLANG_ANALYZER_NONNULL = YES; 349 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 350 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 351 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 352 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 353 | CODE_SIGN_IDENTITY = "iPhone Developer"; 354 | CODE_SIGN_STYLE = Automatic; 355 | DEVELOPMENT_TEAM = ""; 356 | GCC_C_LANGUAGE_STANDARD = gnu11; 357 | INFOPLIST_FILE = AttributedLabelTests/Info.plist; 358 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 359 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 360 | PRODUCT_BUNDLE_IDENTIFIER = com.kyoheiito.AttributedLabelTests; 361 | PRODUCT_NAME = "$(TARGET_NAME)"; 362 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 363 | SWIFT_VERSION = 5.0; 364 | TARGETED_DEVICE_FAMILY = "1,2"; 365 | }; 366 | name = Debug; 367 | }; 368 | C75202051F73458A005E1197 /* Release */ = { 369 | isa = XCBuildConfiguration; 370 | buildSettings = { 371 | CLANG_ANALYZER_NONNULL = YES; 372 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 373 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 374 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 375 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 376 | CODE_SIGN_IDENTITY = "iPhone Developer"; 377 | CODE_SIGN_STYLE = Automatic; 378 | DEVELOPMENT_TEAM = ""; 379 | GCC_C_LANGUAGE_STANDARD = gnu11; 380 | INFOPLIST_FILE = AttributedLabelTests/Info.plist; 381 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 382 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 383 | PRODUCT_BUNDLE_IDENTIFIER = com.kyoheiito.AttributedLabelTests; 384 | PRODUCT_NAME = "$(TARGET_NAME)"; 385 | SWIFT_VERSION = 5.0; 386 | TARGETED_DEVICE_FAMILY = "1,2"; 387 | }; 388 | name = Release; 389 | }; 390 | C7F60DCB1B582DCC006761A8 /* Debug */ = { 391 | isa = XCBuildConfiguration; 392 | buildSettings = { 393 | ALWAYS_SEARCH_USER_PATHS = NO; 394 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 395 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 396 | CLANG_CXX_LIBRARY = "libc++"; 397 | CLANG_ENABLE_MODULES = YES; 398 | CLANG_ENABLE_OBJC_ARC = YES; 399 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 400 | CLANG_WARN_BOOL_CONVERSION = YES; 401 | CLANG_WARN_COMMA = YES; 402 | CLANG_WARN_CONSTANT_CONVERSION = YES; 403 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 404 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 405 | CLANG_WARN_EMPTY_BODY = YES; 406 | CLANG_WARN_ENUM_CONVERSION = YES; 407 | CLANG_WARN_INFINITE_RECURSION = YES; 408 | CLANG_WARN_INT_CONVERSION = YES; 409 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 410 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 411 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 412 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 413 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 414 | CLANG_WARN_STRICT_PROTOTYPES = YES; 415 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 416 | CLANG_WARN_UNREACHABLE_CODE = YES; 417 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 418 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 419 | COPY_PHASE_STRIP = NO; 420 | CURRENT_PROJECT_VERSION = 1; 421 | DEBUG_INFORMATION_FORMAT = dwarf; 422 | ENABLE_STRICT_OBJC_MSGSEND = YES; 423 | ENABLE_TESTABILITY = YES; 424 | GCC_C_LANGUAGE_STANDARD = gnu99; 425 | GCC_DYNAMIC_NO_PIC = NO; 426 | GCC_NO_COMMON_BLOCKS = YES; 427 | GCC_OPTIMIZATION_LEVEL = 0; 428 | GCC_PREPROCESSOR_DEFINITIONS = ( 429 | "DEBUG=1", 430 | "$(inherited)", 431 | ); 432 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 433 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 434 | GCC_WARN_UNDECLARED_SELECTOR = YES; 435 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 436 | GCC_WARN_UNUSED_FUNCTION = YES; 437 | GCC_WARN_UNUSED_VARIABLE = YES; 438 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 439 | MTL_ENABLE_DEBUG_INFO = YES; 440 | ONLY_ACTIVE_ARCH = YES; 441 | SDKROOT = iphoneos; 442 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 443 | TARGETED_DEVICE_FAMILY = "1,2"; 444 | VERSIONING_SYSTEM = "apple-generic"; 445 | VERSION_INFO_PREFIX = ""; 446 | }; 447 | name = Debug; 448 | }; 449 | C7F60DCC1B582DCC006761A8 /* Release */ = { 450 | isa = XCBuildConfiguration; 451 | buildSettings = { 452 | ALWAYS_SEARCH_USER_PATHS = NO; 453 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 454 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 455 | CLANG_CXX_LIBRARY = "libc++"; 456 | CLANG_ENABLE_MODULES = YES; 457 | CLANG_ENABLE_OBJC_ARC = YES; 458 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 459 | CLANG_WARN_BOOL_CONVERSION = YES; 460 | CLANG_WARN_COMMA = YES; 461 | CLANG_WARN_CONSTANT_CONVERSION = YES; 462 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 463 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 464 | CLANG_WARN_EMPTY_BODY = YES; 465 | CLANG_WARN_ENUM_CONVERSION = YES; 466 | CLANG_WARN_INFINITE_RECURSION = YES; 467 | CLANG_WARN_INT_CONVERSION = YES; 468 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 469 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 470 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 471 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 472 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 473 | CLANG_WARN_STRICT_PROTOTYPES = YES; 474 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 475 | CLANG_WARN_UNREACHABLE_CODE = YES; 476 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 477 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 478 | COPY_PHASE_STRIP = NO; 479 | CURRENT_PROJECT_VERSION = 1; 480 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 481 | ENABLE_NS_ASSERTIONS = NO; 482 | ENABLE_STRICT_OBJC_MSGSEND = YES; 483 | GCC_C_LANGUAGE_STANDARD = gnu99; 484 | GCC_NO_COMMON_BLOCKS = YES; 485 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 486 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 487 | GCC_WARN_UNDECLARED_SELECTOR = YES; 488 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 489 | GCC_WARN_UNUSED_FUNCTION = YES; 490 | GCC_WARN_UNUSED_VARIABLE = YES; 491 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 492 | MTL_ENABLE_DEBUG_INFO = NO; 493 | SDKROOT = iphoneos; 494 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 495 | TARGETED_DEVICE_FAMILY = "1,2"; 496 | VALIDATE_PRODUCT = YES; 497 | VERSIONING_SYSTEM = "apple-generic"; 498 | VERSION_INFO_PREFIX = ""; 499 | }; 500 | name = Release; 501 | }; 502 | C7F60DCE1B582DCC006761A8 /* Debug */ = { 503 | isa = XCBuildConfiguration; 504 | buildSettings = { 505 | CLANG_ENABLE_MODULES = YES; 506 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; 507 | DEFINES_MODULE = YES; 508 | DYLIB_COMPATIBILITY_VERSION = 1; 509 | DYLIB_CURRENT_VERSION = 1; 510 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 511 | INFOPLIST_FILE = AttributedLabel/Info.plist; 512 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 513 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 514 | PRODUCT_BUNDLE_IDENTIFIER = com.kyoheiito.AttributedLabel; 515 | PRODUCT_NAME = "$(TARGET_NAME)"; 516 | SKIP_INSTALL = YES; 517 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 518 | SWIFT_VERSION = 5.0; 519 | }; 520 | name = Debug; 521 | }; 522 | C7F60DCF1B582DCC006761A8 /* Release */ = { 523 | isa = XCBuildConfiguration; 524 | buildSettings = { 525 | CLANG_ENABLE_MODULES = YES; 526 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; 527 | DEFINES_MODULE = YES; 528 | DYLIB_COMPATIBILITY_VERSION = 1; 529 | DYLIB_CURRENT_VERSION = 1; 530 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 531 | INFOPLIST_FILE = AttributedLabel/Info.plist; 532 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 533 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 534 | PRODUCT_BUNDLE_IDENTIFIER = com.kyoheiito.AttributedLabel; 535 | PRODUCT_NAME = "$(TARGET_NAME)"; 536 | SKIP_INSTALL = YES; 537 | SWIFT_VERSION = 5.0; 538 | }; 539 | name = Release; 540 | }; 541 | EDC739D51F7B582300206679 /* Debug */ = { 542 | isa = XCBuildConfiguration; 543 | buildSettings = { 544 | CLANG_ANALYZER_NONNULL = YES; 545 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 546 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 547 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 548 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 549 | CODE_SIGN_IDENTITY = ""; 550 | CODE_SIGN_STYLE = Automatic; 551 | DEFINES_MODULE = YES; 552 | DYLIB_COMPATIBILITY_VERSION = 1; 553 | DYLIB_CURRENT_VERSION = 1; 554 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 555 | GCC_C_LANGUAGE_STANDARD = gnu11; 556 | INFOPLIST_FILE = AttributedLabel/Info.plist; 557 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 558 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 559 | PRODUCT_BUNDLE_IDENTIFIER = "jp.hryk224.AttributedLabel-tvOS"; 560 | PRODUCT_NAME = AttributedLabel; 561 | SDKROOT = appletvos; 562 | SKIP_INSTALL = YES; 563 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 564 | SWIFT_VERSION = 5.0; 565 | TARGETED_DEVICE_FAMILY = 3; 566 | TVOS_DEPLOYMENT_TARGET = 10.0; 567 | }; 568 | name = Debug; 569 | }; 570 | EDC739D61F7B582300206679 /* Release */ = { 571 | isa = XCBuildConfiguration; 572 | buildSettings = { 573 | CLANG_ANALYZER_NONNULL = YES; 574 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 575 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 576 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 577 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 578 | CODE_SIGN_IDENTITY = ""; 579 | CODE_SIGN_STYLE = Automatic; 580 | DEFINES_MODULE = YES; 581 | DYLIB_COMPATIBILITY_VERSION = 1; 582 | DYLIB_CURRENT_VERSION = 1; 583 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 584 | GCC_C_LANGUAGE_STANDARD = gnu11; 585 | INFOPLIST_FILE = AttributedLabel/Info.plist; 586 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 587 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 588 | PRODUCT_BUNDLE_IDENTIFIER = "jp.hryk224.AttributedLabel-tvOS"; 589 | PRODUCT_NAME = AttributedLabel; 590 | SDKROOT = appletvos; 591 | SKIP_INSTALL = YES; 592 | SWIFT_VERSION = 5.0; 593 | TARGETED_DEVICE_FAMILY = 3; 594 | TVOS_DEPLOYMENT_TARGET = 10.0; 595 | }; 596 | name = Release; 597 | }; 598 | EDC739D71F7B582300206679 /* Debug */ = { 599 | isa = XCBuildConfiguration; 600 | buildSettings = { 601 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 602 | CLANG_ANALYZER_NONNULL = YES; 603 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 604 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 605 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 606 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 607 | CODE_SIGN_STYLE = Automatic; 608 | GCC_C_LANGUAGE_STANDARD = gnu11; 609 | INFOPLIST_FILE = AttributedLabelTests/Info.plist; 610 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 611 | PRODUCT_BUNDLE_IDENTIFIER = "jp.hryk224.AttributedLabel-tvOSTests"; 612 | PRODUCT_NAME = "$(TARGET_NAME)"; 613 | SDKROOT = appletvos; 614 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 615 | SWIFT_VERSION = 5.0; 616 | TARGETED_DEVICE_FAMILY = 3; 617 | TVOS_DEPLOYMENT_TARGET = 12.0; 618 | }; 619 | name = Debug; 620 | }; 621 | EDC739D81F7B582300206679 /* Release */ = { 622 | isa = XCBuildConfiguration; 623 | buildSettings = { 624 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 625 | CLANG_ANALYZER_NONNULL = YES; 626 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 627 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 628 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 629 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 630 | CODE_SIGN_STYLE = Automatic; 631 | GCC_C_LANGUAGE_STANDARD = gnu11; 632 | INFOPLIST_FILE = AttributedLabelTests/Info.plist; 633 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 634 | PRODUCT_BUNDLE_IDENTIFIER = "jp.hryk224.AttributedLabel-tvOSTests"; 635 | PRODUCT_NAME = "$(TARGET_NAME)"; 636 | SDKROOT = appletvos; 637 | SWIFT_VERSION = 5.0; 638 | TARGETED_DEVICE_FAMILY = 3; 639 | TVOS_DEPLOYMENT_TARGET = 12.0; 640 | }; 641 | name = Release; 642 | }; 643 | /* End XCBuildConfiguration section */ 644 | 645 | /* Begin XCConfigurationList section */ 646 | C75202061F73458A005E1197 /* Build configuration list for PBXNativeTarget "AttributedLabelTests" */ = { 647 | isa = XCConfigurationList; 648 | buildConfigurations = ( 649 | C75202041F73458A005E1197 /* Debug */, 650 | C75202051F73458A005E1197 /* Release */, 651 | ); 652 | defaultConfigurationIsVisible = 0; 653 | defaultConfigurationName = Release; 654 | }; 655 | C7F60DBF1B582DCC006761A8 /* Build configuration list for PBXProject "AttributedLabel" */ = { 656 | isa = XCConfigurationList; 657 | buildConfigurations = ( 658 | C7F60DCB1B582DCC006761A8 /* Debug */, 659 | C7F60DCC1B582DCC006761A8 /* Release */, 660 | ); 661 | defaultConfigurationIsVisible = 0; 662 | defaultConfigurationName = Release; 663 | }; 664 | C7F60DCD1B582DCC006761A8 /* Build configuration list for PBXNativeTarget "AttributedLabel" */ = { 665 | isa = XCConfigurationList; 666 | buildConfigurations = ( 667 | C7F60DCE1B582DCC006761A8 /* Debug */, 668 | C7F60DCF1B582DCC006761A8 /* Release */, 669 | ); 670 | defaultConfigurationIsVisible = 0; 671 | defaultConfigurationName = Release; 672 | }; 673 | EDC739D91F7B582300206679 /* Build configuration list for PBXNativeTarget "AttributedLabel-tvOS" */ = { 674 | isa = XCConfigurationList; 675 | buildConfigurations = ( 676 | EDC739D51F7B582300206679 /* Debug */, 677 | EDC739D61F7B582300206679 /* Release */, 678 | ); 679 | defaultConfigurationIsVisible = 0; 680 | defaultConfigurationName = Release; 681 | }; 682 | EDC739DA1F7B582300206679 /* Build configuration list for PBXNativeTarget "AttributedLabel-tvOSTests" */ = { 683 | isa = XCConfigurationList; 684 | buildConfigurations = ( 685 | EDC739D71F7B582300206679 /* Debug */, 686 | EDC739D81F7B582300206679 /* Release */, 687 | ); 688 | defaultConfigurationIsVisible = 0; 689 | defaultConfigurationName = Release; 690 | }; 691 | /* End XCConfigurationList section */ 692 | }; 693 | rootObject = C7F60DBC1B582DCC006761A8 /* Project object */; 694 | } 695 | -------------------------------------------------------------------------------- /AttributedLabel.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /AttributedLabel.xcodeproj/xcshareddata/xcschemes/AttributedLabel-tvOS.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 65 | 71 | 72 | 73 | 74 | 75 | 76 | 82 | 83 | 89 | 90 | 91 | 92 | 94 | 95 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /AttributedLabel.xcodeproj/xcshareddata/xcschemes/AttributedLabel.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 70 | 71 | 72 | 73 | 75 | 76 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /AttributedLabel.xcodeproj/xcshareddata/xcschemes/AttributedLabelTests.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 16 | 18 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 40 | 41 | 42 | 43 | 49 | 50 | 52 | 53 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /AttributedLabel/AttributedLabel.h: -------------------------------------------------------------------------------- 1 | // 2 | // AttributedLabel.h 3 | // AttributedLabel 4 | // 5 | // Created by Kyohei Ito on 2015/07/17. 6 | // Copyright © 2015年 Kyohei Ito. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for AttributedLabel. 12 | FOUNDATION_EXPORT double AttributedLabelVersionNumber; 13 | 14 | //! Project version string for AttributedLabel. 15 | FOUNDATION_EXPORT const unsigned char AttributedLabelVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /AttributedLabel/AttributedLabel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AttributedLabel.swift 3 | // AttributedLabel 4 | // 5 | // Created by Kyohei Ito on 2015/07/17. 6 | // Copyright © 2015年 Kyohei Ito. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @IBDesignable 12 | open class AttributedLabel: UIView { 13 | public enum ContentAlignment: Int { 14 | case center 15 | case top 16 | case bottom 17 | case left 18 | case right 19 | case topLeft 20 | case topRight 21 | case bottomLeft 22 | case bottomRight 23 | 24 | func alignOffset(viewSize: CGSize, containerSize: CGSize) -> CGPoint { 25 | let xMargin = viewSize.width - containerSize.width 26 | let yMargin = viewSize.height - containerSize.height 27 | 28 | switch self { 29 | case .center: 30 | return CGPoint(x: max(xMargin / 2, 0), y: max(yMargin / 2, 0)) 31 | case .top: 32 | return CGPoint(x: max(xMargin / 2, 0), y: 0) 33 | case .bottom: 34 | return CGPoint(x: max(xMargin / 2, 0), y: max(yMargin, 0)) 35 | case .left: 36 | return CGPoint(x: 0, y: max(yMargin / 2, 0)) 37 | case .right: 38 | return CGPoint(x: max(xMargin, 0), y: max(yMargin / 2, 0)) 39 | case .topLeft: 40 | return CGPoint(x: 0, y: 0) 41 | case .topRight: 42 | return CGPoint(x: max(xMargin, 0), y: 0) 43 | case .bottomLeft: 44 | return CGPoint(x: 0, y: max(yMargin, 0)) 45 | case .bottomRight: 46 | return CGPoint(x: max(xMargin, 0), y: max(yMargin, 0)) 47 | } 48 | } 49 | } 50 | 51 | /// default is `0`. 52 | @IBInspectable 53 | open var numberOfLines: Int { 54 | get { return container.maximumNumberOfLines } 55 | set { 56 | container.maximumNumberOfLines = newValue 57 | setNeedsDisplay() 58 | } 59 | } 60 | /// default is `Left`. 61 | open var contentAlignment: ContentAlignment = .left { 62 | didSet { setNeedsDisplay() } 63 | } 64 | /// `lineFragmentPadding` of `NSTextContainer`. default is `0`. 65 | @IBInspectable 66 | open var padding: CGFloat { 67 | get { return container.lineFragmentPadding } 68 | set { 69 | container.lineFragmentPadding = newValue 70 | setNeedsDisplay() 71 | } 72 | } 73 | /// default is system font 17 plain. 74 | open var font = UIFont.systemFont(ofSize: 17) { 75 | didSet { setNeedsDisplay() } 76 | } 77 | /// default is `ByTruncatingTail`. 78 | open var lineBreakMode: NSLineBreakMode { 79 | get { return container.lineBreakMode } 80 | set { 81 | container.lineBreakMode = newValue 82 | setNeedsDisplay() 83 | } 84 | } 85 | /// default is nil (text draws black). 86 | @IBInspectable 87 | open var textColor: UIColor? { 88 | didSet { setNeedsDisplay() } 89 | } 90 | /// default is nil. 91 | open var paragraphStyle: NSParagraphStyle? { 92 | didSet { setNeedsDisplay() } 93 | } 94 | /// default is nil. 95 | open var shadow: NSShadow? { 96 | didSet { setNeedsDisplay() } 97 | } 98 | /// default is nil. 99 | open var attributedText: NSAttributedString? { 100 | didSet { setNeedsDisplay() } 101 | } 102 | /// default is nil. 103 | @IBInspectable 104 | open var text: String? { 105 | get { 106 | return attributedText?.string 107 | } 108 | set { 109 | if let value = newValue { 110 | attributedText = NSAttributedString(string: value) 111 | } else { 112 | attributedText = nil 113 | } 114 | } 115 | } 116 | /// Support for constraint-based layout (auto layout) 117 | /// If nonzero, this is used when determining -intrinsicContentSize for multiline labels 118 | open var preferredMaxLayoutWidth: CGFloat = 0 119 | 120 | /// If need to use intrinsicContentSize set true. 121 | /// Also should call invalidateIntrinsicContentSize when intrinsicContentSize is cached. When text was changed for example. 122 | public var usesIntrinsicContentSize = false 123 | 124 | var mergedAttributedText: NSAttributedString? { 125 | if let attributedText = attributedText { 126 | return mergeAttributes(attributedText) 127 | } 128 | return nil 129 | } 130 | 131 | let container = NSTextContainer() 132 | let layoutManager = NSLayoutManager() 133 | 134 | public override init(frame: CGRect) { 135 | super.init(frame: frame) 136 | 137 | isOpaque = false 138 | contentMode = .redraw 139 | lineBreakMode = .byTruncatingTail 140 | padding = 0 141 | layoutManager.addTextContainer(container) 142 | } 143 | 144 | public required init?(coder aDecoder: NSCoder) { 145 | super.init(coder: aDecoder) 146 | 147 | isOpaque = false 148 | contentMode = .redraw 149 | lineBreakMode = .byTruncatingTail 150 | padding = 0 151 | layoutManager.addTextContainer(container) 152 | } 153 | 154 | open override func setNeedsDisplay() { 155 | if Thread.isMainThread { 156 | super.setNeedsDisplay() 157 | } 158 | } 159 | 160 | open override var intrinsicContentSize: CGSize { 161 | if usesIntrinsicContentSize { 162 | let width = preferredMaxLayoutWidth == 0 ? bounds.width : preferredMaxLayoutWidth 163 | let size = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude) 164 | return sizeThatFits(size) 165 | } else { 166 | return bounds.size 167 | } 168 | } 169 | 170 | open override func draw(_ rect: CGRect) { 171 | guard let attributedText = mergedAttributedText else { 172 | return 173 | } 174 | 175 | let storage = NSTextStorage(attributedString: attributedText) 176 | storage.addLayoutManager(layoutManager) 177 | 178 | container.size = rect.size 179 | let frame = layoutManager.usedRect(for: container) 180 | let point = contentAlignment.alignOffset(viewSize: rect.size, containerSize: frame.integral.size) 181 | 182 | let glyphRange = layoutManager.glyphRange(for: container) 183 | layoutManager.drawBackground(forGlyphRange: glyphRange, at: point) 184 | layoutManager.drawGlyphs(forGlyphRange: glyphRange, at: point) 185 | } 186 | 187 | open override func sizeThatFits(_ size: CGSize) -> CGSize { 188 | guard let attributedText = mergedAttributedText else { 189 | return .zero 190 | } 191 | 192 | let storage = NSTextStorage(attributedString: attributedText) 193 | storage.addLayoutManager(layoutManager) 194 | 195 | container.size = size 196 | let frame = layoutManager.usedRect(for: container) 197 | return frame.integral.size 198 | } 199 | 200 | open override func sizeToFit() { 201 | super.sizeToFit() 202 | 203 | let width = preferredMaxLayoutWidth == 0 ? CGFloat.greatestFiniteMagnitude : preferredMaxLayoutWidth 204 | let size = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude) 205 | frame.size = sizeThatFits(size) 206 | } 207 | 208 | func mergeAttributes(_ attributedText: NSAttributedString) -> NSAttributedString { 209 | let attrString = NSMutableAttributedString(attributedString: attributedText) 210 | 211 | attrString.addAttribute(.font, attr: font) 212 | 213 | if let textColor = textColor { 214 | attrString.addAttribute(.foregroundColor, attr: textColor) 215 | } 216 | 217 | if let paragraphStyle = paragraphStyle { 218 | attrString.addAttribute(.paragraphStyle, attr: paragraphStyle) 219 | } 220 | 221 | if let shadow = shadow { 222 | attrString.addAttribute(.shadow, attr: shadow) 223 | } 224 | 225 | return attrString 226 | } 227 | 228 | } 229 | 230 | extension NSMutableAttributedString { 231 | @discardableResult 232 | func addAttribute(_ attrName: NSAttributedString.Key, attr: AnyObject, in range: NSRange? = nil) -> Self { 233 | let range = range ?? NSRange(location: 0, length: length) 234 | enumerateAttribute(attrName, in: range, options: .reverse) { object, range, pointer in 235 | if object == nil { 236 | addAttributes([attrName: attr], range: range) 237 | } 238 | } 239 | 240 | return self 241 | } 242 | } 243 | 244 | -------------------------------------------------------------------------------- /AttributedLabel/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 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | C77EE75C1B5C4F0E005842D4 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C77EE75B1B5C4F0E005842D4 /* LaunchScreen.storyboard */; }; 11 | C79C837C1B5C38AC00338E63 /* Benchmark.swift in Sources */ = {isa = PBXBuildFile; fileRef = C79C837B1B5C38AC00338E63 /* Benchmark.swift */; }; 12 | C7AF9F321B58426D00FA96B9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7AF9F311B58426D00FA96B9 /* AppDelegate.swift */; }; 13 | C7AF9F341B58426D00FA96B9 /* CustomizeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7AF9F331B58426D00FA96B9 /* CustomizeViewController.swift */; }; 14 | C7AF9F371B58426D00FA96B9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C7AF9F351B58426D00FA96B9 /* Main.storyboard */; }; 15 | C7AF9F391B58426D00FA96B9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C7AF9F381B58426D00FA96B9 /* Assets.xcassets */; }; 16 | C7AF9F551B5842D700FA96B9 /* AttributedLabel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C7AF9F541B5842AF00FA96B9 /* AttributedLabel.framework */; }; 17 | C7AF9F561B5842D700FA96B9 /* AttributedLabel.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C7AF9F541B5842AF00FA96B9 /* AttributedLabel.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 18 | C7E080C61B5C1DF800AD9593 /* CollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7E080C51B5C1DF800AD9593 /* CollectionViewController.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | 0FD22BB9214DC33C00345BB6 /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = C7AF9F4F1B5842AF00FA96B9 /* AttributedLabel.xcodeproj */; 25 | proxyType = 2; 26 | remoteGlobalIDString = EDC739C41F7B582300206679; 27 | remoteInfo = "AttributedLabel-tvOS"; 28 | }; 29 | 0FD22BBB214DC33C00345BB6 /* PBXContainerItemProxy */ = { 30 | isa = PBXContainerItemProxy; 31 | containerPortal = C7AF9F4F1B5842AF00FA96B9 /* AttributedLabel.xcodeproj */; 32 | proxyType = 2; 33 | remoteGlobalIDString = EDC739CC1F7B582300206679; 34 | remoteInfo = "AttributedLabel-tvOSTests"; 35 | }; 36 | C752020C1F738D5A005E1197 /* PBXContainerItemProxy */ = { 37 | isa = PBXContainerItemProxy; 38 | containerPortal = C7AF9F4F1B5842AF00FA96B9 /* AttributedLabel.xcodeproj */; 39 | proxyType = 2; 40 | remoteGlobalIDString = C75201FC1F73458A005E1197; 41 | remoteInfo = AttributedLabelTests; 42 | }; 43 | C7AF9F531B5842AF00FA96B9 /* PBXContainerItemProxy */ = { 44 | isa = PBXContainerItemProxy; 45 | containerPortal = C7AF9F4F1B5842AF00FA96B9 /* AttributedLabel.xcodeproj */; 46 | proxyType = 2; 47 | remoteGlobalIDString = C7F60DC51B582DCC006761A8; 48 | remoteInfo = AttributedLabel; 49 | }; 50 | C7AF9F571B5842D700FA96B9 /* PBXContainerItemProxy */ = { 51 | isa = PBXContainerItemProxy; 52 | containerPortal = C7AF9F4F1B5842AF00FA96B9 /* AttributedLabel.xcodeproj */; 53 | proxyType = 1; 54 | remoteGlobalIDString = C7F60DC41B582DCC006761A8; 55 | remoteInfo = AttributedLabel; 56 | }; 57 | /* End PBXContainerItemProxy section */ 58 | 59 | /* Begin PBXCopyFilesBuildPhase section */ 60 | C7AF9F591B5842D700FA96B9 /* Embed Frameworks */ = { 61 | isa = PBXCopyFilesBuildPhase; 62 | buildActionMask = 2147483647; 63 | dstPath = ""; 64 | dstSubfolderSpec = 10; 65 | files = ( 66 | C7AF9F561B5842D700FA96B9 /* AttributedLabel.framework in Embed Frameworks */, 67 | ); 68 | name = "Embed Frameworks"; 69 | runOnlyForDeploymentPostprocessing = 0; 70 | }; 71 | /* End PBXCopyFilesBuildPhase section */ 72 | 73 | /* Begin PBXFileReference section */ 74 | C77EE75B1B5C4F0E005842D4 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = ""; }; 75 | C79C837B1B5C38AC00338E63 /* Benchmark.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Benchmark.swift; sourceTree = ""; }; 76 | C7AF9F2E1B58426D00FA96B9 /* AttributedLabelExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AttributedLabelExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 77 | C7AF9F311B58426D00FA96B9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 78 | C7AF9F331B58426D00FA96B9 /* CustomizeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomizeViewController.swift; sourceTree = ""; }; 79 | C7AF9F361B58426D00FA96B9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 80 | C7AF9F381B58426D00FA96B9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 81 | C7AF9F3D1B58426D00FA96B9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 82 | C7AF9F4F1B5842AF00FA96B9 /* AttributedLabel.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = AttributedLabel.xcodeproj; path = ../AttributedLabel.xcodeproj; sourceTree = ""; }; 83 | C7E080C51B5C1DF800AD9593 /* CollectionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewController.swift; sourceTree = ""; }; 84 | /* End PBXFileReference section */ 85 | 86 | /* Begin PBXFrameworksBuildPhase section */ 87 | C7AF9F2B1B58426D00FA96B9 /* Frameworks */ = { 88 | isa = PBXFrameworksBuildPhase; 89 | buildActionMask = 2147483647; 90 | files = ( 91 | C7AF9F551B5842D700FA96B9 /* AttributedLabel.framework in Frameworks */, 92 | ); 93 | runOnlyForDeploymentPostprocessing = 0; 94 | }; 95 | /* End PBXFrameworksBuildPhase section */ 96 | 97 | /* Begin PBXGroup section */ 98 | C7AF9F251B58426D00FA96B9 = { 99 | isa = PBXGroup; 100 | children = ( 101 | C7AF9F4F1B5842AF00FA96B9 /* AttributedLabel.xcodeproj */, 102 | C7AF9F301B58426D00FA96B9 /* AttributedLabelExample */, 103 | C7AF9F2F1B58426D00FA96B9 /* Products */, 104 | ); 105 | sourceTree = ""; 106 | }; 107 | C7AF9F2F1B58426D00FA96B9 /* Products */ = { 108 | isa = PBXGroup; 109 | children = ( 110 | C7AF9F2E1B58426D00FA96B9 /* AttributedLabelExample.app */, 111 | ); 112 | name = Products; 113 | sourceTree = ""; 114 | }; 115 | C7AF9F301B58426D00FA96B9 /* AttributedLabelExample */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | C7AF9F311B58426D00FA96B9 /* AppDelegate.swift */, 119 | C79C837B1B5C38AC00338E63 /* Benchmark.swift */, 120 | C7E080C51B5C1DF800AD9593 /* CollectionViewController.swift */, 121 | C7AF9F331B58426D00FA96B9 /* CustomizeViewController.swift */, 122 | C7AF9F351B58426D00FA96B9 /* Main.storyboard */, 123 | C7AF9F381B58426D00FA96B9 /* Assets.xcassets */, 124 | C77EE75B1B5C4F0E005842D4 /* LaunchScreen.storyboard */, 125 | C7AF9F3D1B58426D00FA96B9 /* Info.plist */, 126 | ); 127 | path = AttributedLabelExample; 128 | sourceTree = ""; 129 | }; 130 | C7AF9F501B5842AF00FA96B9 /* Products */ = { 131 | isa = PBXGroup; 132 | children = ( 133 | C7AF9F541B5842AF00FA96B9 /* AttributedLabel.framework */, 134 | C752020D1F738D5A005E1197 /* AttributedLabelTests.xctest */, 135 | 0FD22BBA214DC33C00345BB6 /* AttributedLabel.framework */, 136 | 0FD22BBC214DC33C00345BB6 /* AttributedLabel-tvOSTests.xctest */, 137 | ); 138 | name = Products; 139 | sourceTree = ""; 140 | }; 141 | /* End PBXGroup section */ 142 | 143 | /* Begin PBXNativeTarget section */ 144 | C7AF9F2D1B58426D00FA96B9 /* AttributedLabelExample */ = { 145 | isa = PBXNativeTarget; 146 | buildConfigurationList = C7AF9F401B58426D00FA96B9 /* Build configuration list for PBXNativeTarget "AttributedLabelExample" */; 147 | buildPhases = ( 148 | C7AF9F2A1B58426D00FA96B9 /* Sources */, 149 | C7AF9F2B1B58426D00FA96B9 /* Frameworks */, 150 | C7AF9F2C1B58426D00FA96B9 /* Resources */, 151 | C7AF9F591B5842D700FA96B9 /* Embed Frameworks */, 152 | ); 153 | buildRules = ( 154 | ); 155 | dependencies = ( 156 | C7AF9F581B5842D700FA96B9 /* PBXTargetDependency */, 157 | ); 158 | name = AttributedLabelExample; 159 | productName = AttributedLabelExample; 160 | productReference = C7AF9F2E1B58426D00FA96B9 /* AttributedLabelExample.app */; 161 | productType = "com.apple.product-type.application"; 162 | }; 163 | /* End PBXNativeTarget section */ 164 | 165 | /* Begin PBXProject section */ 166 | C7AF9F261B58426D00FA96B9 /* Project object */ = { 167 | isa = PBXProject; 168 | attributes = { 169 | LastUpgradeCheck = 1020; 170 | ORGANIZATIONNAME = "Kyohei Ito"; 171 | TargetAttributes = { 172 | C7AF9F2D1B58426D00FA96B9 = { 173 | CreatedOnToolsVersion = 7.0; 174 | LastSwiftMigration = 1020; 175 | }; 176 | }; 177 | }; 178 | buildConfigurationList = C7AF9F291B58426D00FA96B9 /* Build configuration list for PBXProject "AttributedLabelExample" */; 179 | compatibilityVersion = "Xcode 3.2"; 180 | developmentRegion = en; 181 | hasScannedForEncodings = 0; 182 | knownRegions = ( 183 | en, 184 | Base, 185 | ); 186 | mainGroup = C7AF9F251B58426D00FA96B9; 187 | productRefGroup = C7AF9F2F1B58426D00FA96B9 /* Products */; 188 | projectDirPath = ""; 189 | projectReferences = ( 190 | { 191 | ProductGroup = C7AF9F501B5842AF00FA96B9 /* Products */; 192 | ProjectRef = C7AF9F4F1B5842AF00FA96B9 /* AttributedLabel.xcodeproj */; 193 | }, 194 | ); 195 | projectRoot = ""; 196 | targets = ( 197 | C7AF9F2D1B58426D00FA96B9 /* AttributedLabelExample */, 198 | ); 199 | }; 200 | /* End PBXProject section */ 201 | 202 | /* Begin PBXReferenceProxy section */ 203 | 0FD22BBA214DC33C00345BB6 /* AttributedLabel.framework */ = { 204 | isa = PBXReferenceProxy; 205 | fileType = wrapper.framework; 206 | path = AttributedLabel.framework; 207 | remoteRef = 0FD22BB9214DC33C00345BB6 /* PBXContainerItemProxy */; 208 | sourceTree = BUILT_PRODUCTS_DIR; 209 | }; 210 | 0FD22BBC214DC33C00345BB6 /* AttributedLabel-tvOSTests.xctest */ = { 211 | isa = PBXReferenceProxy; 212 | fileType = wrapper.cfbundle; 213 | path = "AttributedLabel-tvOSTests.xctest"; 214 | remoteRef = 0FD22BBB214DC33C00345BB6 /* PBXContainerItemProxy */; 215 | sourceTree = BUILT_PRODUCTS_DIR; 216 | }; 217 | C752020D1F738D5A005E1197 /* AttributedLabelTests.xctest */ = { 218 | isa = PBXReferenceProxy; 219 | fileType = wrapper.cfbundle; 220 | path = AttributedLabelTests.xctest; 221 | remoteRef = C752020C1F738D5A005E1197 /* PBXContainerItemProxy */; 222 | sourceTree = BUILT_PRODUCTS_DIR; 223 | }; 224 | C7AF9F541B5842AF00FA96B9 /* AttributedLabel.framework */ = { 225 | isa = PBXReferenceProxy; 226 | fileType = wrapper.framework; 227 | path = AttributedLabel.framework; 228 | remoteRef = C7AF9F531B5842AF00FA96B9 /* PBXContainerItemProxy */; 229 | sourceTree = BUILT_PRODUCTS_DIR; 230 | }; 231 | /* End PBXReferenceProxy section */ 232 | 233 | /* Begin PBXResourcesBuildPhase section */ 234 | C7AF9F2C1B58426D00FA96B9 /* Resources */ = { 235 | isa = PBXResourcesBuildPhase; 236 | buildActionMask = 2147483647; 237 | files = ( 238 | C77EE75C1B5C4F0E005842D4 /* LaunchScreen.storyboard in Resources */, 239 | C7AF9F391B58426D00FA96B9 /* Assets.xcassets in Resources */, 240 | C7AF9F371B58426D00FA96B9 /* Main.storyboard in Resources */, 241 | ); 242 | runOnlyForDeploymentPostprocessing = 0; 243 | }; 244 | /* End PBXResourcesBuildPhase section */ 245 | 246 | /* Begin PBXSourcesBuildPhase section */ 247 | C7AF9F2A1B58426D00FA96B9 /* Sources */ = { 248 | isa = PBXSourcesBuildPhase; 249 | buildActionMask = 2147483647; 250 | files = ( 251 | C7E080C61B5C1DF800AD9593 /* CollectionViewController.swift in Sources */, 252 | C79C837C1B5C38AC00338E63 /* Benchmark.swift in Sources */, 253 | C7AF9F341B58426D00FA96B9 /* CustomizeViewController.swift in Sources */, 254 | C7AF9F321B58426D00FA96B9 /* AppDelegate.swift in Sources */, 255 | ); 256 | runOnlyForDeploymentPostprocessing = 0; 257 | }; 258 | /* End PBXSourcesBuildPhase section */ 259 | 260 | /* Begin PBXTargetDependency section */ 261 | C7AF9F581B5842D700FA96B9 /* PBXTargetDependency */ = { 262 | isa = PBXTargetDependency; 263 | name = AttributedLabel; 264 | targetProxy = C7AF9F571B5842D700FA96B9 /* PBXContainerItemProxy */; 265 | }; 266 | /* End PBXTargetDependency section */ 267 | 268 | /* Begin PBXVariantGroup section */ 269 | C7AF9F351B58426D00FA96B9 /* Main.storyboard */ = { 270 | isa = PBXVariantGroup; 271 | children = ( 272 | C7AF9F361B58426D00FA96B9 /* Base */, 273 | ); 274 | name = Main.storyboard; 275 | sourceTree = ""; 276 | }; 277 | /* End PBXVariantGroup section */ 278 | 279 | /* Begin XCBuildConfiguration section */ 280 | C7AF9F3E1B58426D00FA96B9 /* Debug */ = { 281 | isa = XCBuildConfiguration; 282 | buildSettings = { 283 | ALWAYS_SEARCH_USER_PATHS = NO; 284 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 285 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 286 | CLANG_CXX_LIBRARY = "libc++"; 287 | CLANG_ENABLE_MODULES = YES; 288 | CLANG_ENABLE_OBJC_ARC = YES; 289 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 290 | CLANG_WARN_BOOL_CONVERSION = YES; 291 | CLANG_WARN_COMMA = YES; 292 | CLANG_WARN_CONSTANT_CONVERSION = YES; 293 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 294 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 295 | CLANG_WARN_EMPTY_BODY = YES; 296 | CLANG_WARN_ENUM_CONVERSION = YES; 297 | CLANG_WARN_INFINITE_RECURSION = YES; 298 | CLANG_WARN_INT_CONVERSION = YES; 299 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 300 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 301 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 302 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 303 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 304 | CLANG_WARN_STRICT_PROTOTYPES = YES; 305 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 306 | CLANG_WARN_UNREACHABLE_CODE = YES; 307 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 308 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 309 | COPY_PHASE_STRIP = NO; 310 | DEBUG_INFORMATION_FORMAT = dwarf; 311 | ENABLE_STRICT_OBJC_MSGSEND = YES; 312 | ENABLE_TESTABILITY = YES; 313 | GCC_C_LANGUAGE_STANDARD = gnu99; 314 | GCC_DYNAMIC_NO_PIC = NO; 315 | GCC_NO_COMMON_BLOCKS = YES; 316 | GCC_OPTIMIZATION_LEVEL = 0; 317 | GCC_PREPROCESSOR_DEFINITIONS = ( 318 | "DEBUG=1", 319 | "$(inherited)", 320 | ); 321 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 322 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 323 | GCC_WARN_UNDECLARED_SELECTOR = YES; 324 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 325 | GCC_WARN_UNUSED_FUNCTION = YES; 326 | GCC_WARN_UNUSED_VARIABLE = YES; 327 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 328 | MTL_ENABLE_DEBUG_INFO = YES; 329 | ONLY_ACTIVE_ARCH = YES; 330 | SDKROOT = iphoneos; 331 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 332 | }; 333 | name = Debug; 334 | }; 335 | C7AF9F3F1B58426D00FA96B9 /* Release */ = { 336 | isa = XCBuildConfiguration; 337 | buildSettings = { 338 | ALWAYS_SEARCH_USER_PATHS = NO; 339 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 340 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 341 | CLANG_CXX_LIBRARY = "libc++"; 342 | CLANG_ENABLE_MODULES = YES; 343 | CLANG_ENABLE_OBJC_ARC = YES; 344 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 345 | CLANG_WARN_BOOL_CONVERSION = YES; 346 | CLANG_WARN_COMMA = YES; 347 | CLANG_WARN_CONSTANT_CONVERSION = YES; 348 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 349 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 350 | CLANG_WARN_EMPTY_BODY = YES; 351 | CLANG_WARN_ENUM_CONVERSION = YES; 352 | CLANG_WARN_INFINITE_RECURSION = YES; 353 | CLANG_WARN_INT_CONVERSION = YES; 354 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 355 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 356 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 357 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 358 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 359 | CLANG_WARN_STRICT_PROTOTYPES = YES; 360 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 361 | CLANG_WARN_UNREACHABLE_CODE = YES; 362 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 363 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 364 | COPY_PHASE_STRIP = NO; 365 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 366 | ENABLE_NS_ASSERTIONS = NO; 367 | ENABLE_STRICT_OBJC_MSGSEND = YES; 368 | GCC_C_LANGUAGE_STANDARD = gnu99; 369 | GCC_NO_COMMON_BLOCKS = YES; 370 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 371 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 372 | GCC_WARN_UNDECLARED_SELECTOR = YES; 373 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 374 | GCC_WARN_UNUSED_FUNCTION = YES; 375 | GCC_WARN_UNUSED_VARIABLE = YES; 376 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 377 | MTL_ENABLE_DEBUG_INFO = NO; 378 | SDKROOT = iphoneos; 379 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 380 | VALIDATE_PRODUCT = YES; 381 | }; 382 | name = Release; 383 | }; 384 | C7AF9F411B58426D00FA96B9 /* Debug */ = { 385 | isa = XCBuildConfiguration; 386 | buildSettings = { 387 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 388 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 389 | INFOPLIST_FILE = AttributedLabelExample/Info.plist; 390 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 391 | PRODUCT_BUNDLE_IDENTIFIER = com.kyoheiito.AttributedLabelExample; 392 | PRODUCT_NAME = "$(TARGET_NAME)"; 393 | SWIFT_VERSION = 5.0; 394 | }; 395 | name = Debug; 396 | }; 397 | C7AF9F421B58426D00FA96B9 /* Release */ = { 398 | isa = XCBuildConfiguration; 399 | buildSettings = { 400 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 401 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 402 | INFOPLIST_FILE = AttributedLabelExample/Info.plist; 403 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 404 | PRODUCT_BUNDLE_IDENTIFIER = com.kyoheiito.AttributedLabelExample; 405 | PRODUCT_NAME = "$(TARGET_NAME)"; 406 | SWIFT_VERSION = 5.0; 407 | }; 408 | name = Release; 409 | }; 410 | /* End XCBuildConfiguration section */ 411 | 412 | /* Begin XCConfigurationList section */ 413 | C7AF9F291B58426D00FA96B9 /* Build configuration list for PBXProject "AttributedLabelExample" */ = { 414 | isa = XCConfigurationList; 415 | buildConfigurations = ( 416 | C7AF9F3E1B58426D00FA96B9 /* Debug */, 417 | C7AF9F3F1B58426D00FA96B9 /* Release */, 418 | ); 419 | defaultConfigurationIsVisible = 0; 420 | defaultConfigurationName = Release; 421 | }; 422 | C7AF9F401B58426D00FA96B9 /* Build configuration list for PBXNativeTarget "AttributedLabelExample" */ = { 423 | isa = XCConfigurationList; 424 | buildConfigurations = ( 425 | C7AF9F411B58426D00FA96B9 /* Debug */, 426 | C7AF9F421B58426D00FA96B9 /* Release */, 427 | ); 428 | defaultConfigurationIsVisible = 0; 429 | defaultConfigurationName = Release; 430 | }; 431 | /* End XCConfigurationList section */ 432 | }; 433 | rootObject = C7AF9F261B58426D00FA96B9 /* Project object */; 434 | } 435 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // AttributedLabelExample 4 | // 5 | // Created by Kyohei Ito on 2015/07/17. 6 | // Copyright © 2015年 Kyohei Ito. 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 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample/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 | } -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample/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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 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 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 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 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 189 | 195 | 201 | 207 | 213 | 219 | 225 | 231 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 269 | 275 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample/Benchmark.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Benchmark.swift 3 | // AttributedLabelExample 4 | // 5 | // Created by Kyohei Ito on 2015/07/20. 6 | // Copyright © 2015年 Kyohei Ito. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class Benchmark { 12 | var startTime: Date! 13 | 14 | func start() { 15 | print("start") 16 | startTime = Date() 17 | } 18 | 19 | func finish() { 20 | let elapsed = Date().timeIntervalSince(startTime) as Double 21 | let string = String(format: "%.8f", elapsed) 22 | print(string) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample/CollectionViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CollectionViewController.swift 3 | // AttributedLabelExample 4 | // 5 | // Created by Kyohei Ito on 2015/07/20. 6 | // Copyright © 2015年 Kyohei Ito. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import AttributedLabel 11 | 12 | protocol Configurable { 13 | func configure(_ text: NSAttributedString) 14 | } 15 | 16 | class UILabelCollectionViewCell: UICollectionViewCell, Configurable { 17 | var benchMark = Benchmark() 18 | @IBOutlet weak var label: UILabel! 19 | 20 | override func layoutSubviews() { 21 | super.layoutSubviews() 22 | 23 | benchMark.finish() 24 | } 25 | 26 | override func awakeFromNib() { 27 | super.awakeFromNib() 28 | 29 | label.textColor = UIColor.lightGray 30 | } 31 | 32 | func configure(_ text: NSAttributedString) { 33 | benchMark.start() 34 | label.attributedText = text 35 | } 36 | } 37 | 38 | class AttributedCollectionViewCell: UICollectionViewCell, Configurable { 39 | var benchMark = Benchmark() 40 | @IBOutlet weak var label: AttributedLabel! 41 | 42 | override func layoutSubviews() { 43 | super.layoutSubviews() 44 | 45 | benchMark.finish() 46 | } 47 | 48 | override func awakeFromNib() { 49 | super.awakeFromNib() 50 | 51 | label.numberOfLines = 0 52 | label.font = UIFont.systemFont(ofSize: 12) 53 | label.textColor = UIColor.lightGray 54 | } 55 | 56 | func configure(_ text: NSAttributedString) { 57 | benchMark.start() 58 | label.attributedText = text 59 | } 60 | } 61 | 62 | class CollectionViewController: UIViewController, UICollectionViewDataSource { 63 | @IBOutlet weak var collectionView: UICollectionView! 64 | 65 | override func viewDidLoad() { 66 | super.viewDidLoad() 67 | 68 | collectionView.dataSource = self 69 | } 70 | 71 | private func contentAttributeText() -> NSMutableAttributedString { 72 | let number = String(arc4random_uniform(100)) 73 | let content = "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." 74 | let string = "\(number) \(content)" 75 | let rangeOfMinutes = (string as NSString).range(of: number) 76 | let attributedString = NSMutableAttributedString(string: string as String) 77 | 78 | attributedString.setAttributes([.foregroundColor: UIColor.black], range: rangeOfMinutes) 79 | return attributedString 80 | } 81 | 82 | func numberOfSections(in collectionView: UICollectionView) -> Int { 83 | return 100 84 | } 85 | 86 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 87 | return 300 88 | } 89 | 90 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 91 | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) 92 | if let labelCell = cell as? Configurable { 93 | labelCell.configure(contentAttributeText()) 94 | } 95 | return cell 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample/CustomizeViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CustomizeViewController.swift 3 | // AttributedLabelExample 4 | // 5 | // Created by Kyohei Ito on 2015/07/17. 6 | // Copyright © 2015年 Kyohei Ito. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import AttributedLabel 11 | 12 | class CustomizeViewController: UIViewController { 13 | @IBOutlet weak var attributedLabel: AttributedLabel! 14 | 15 | override func viewDidLoad() { 16 | super.viewDidLoad() 17 | 18 | attributedLabel.text = "AttributedLabel" 19 | } 20 | 21 | private func rand(_ random: UInt32) -> CGFloat { 22 | return CGFloat(arc4random_uniform(random)) 23 | } 24 | 25 | private func randColor(_ random: UInt32) -> UIColor { 26 | return UIColor(hue: rand(random) / 10, saturation: 0.8, brightness: 0.8, alpha: 1) 27 | } 28 | 29 | @IBAction func buttonTapped(_ sender: AnyObject) { 30 | if let button = sender as? UIButton, let alignment = AttributedLabel.ContentAlignment(rawValue: button.tag) { 31 | attributedLabel.contentAlignment = alignment 32 | } 33 | } 34 | 35 | @IBAction func changeTextColor() { 36 | attributedLabel.textColor = randColor(10) 37 | } 38 | 39 | @IBAction func changeFontSize() { 40 | attributedLabel.font = UIFont.systemFont(ofSize: rand(20) + 12) 41 | } 42 | 43 | @IBAction func changeShadow() { 44 | let shadow = NSShadow() 45 | shadow.shadowBlurRadius = rand(5) 46 | shadow.shadowOffset = CGSize(width: rand(5), height: rand(5)) 47 | shadow.shadowColor = randColor(10) 48 | attributedLabel.shadow = shadow 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample/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 | -------------------------------------------------------------------------------- /AttributedLabelExample/AttributedLabelExample/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 | 29 | -------------------------------------------------------------------------------- /AttributedLabelTests/AttributedLabelTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AttributedLabelTests.swift 3 | // AttributedLabelTests 4 | // 5 | // Created by Kyohei Ito on 2017/09/21. 6 | // Copyright © 2017年 Kyohei Ito. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import AttributedLabel 11 | 12 | class AttributedLabelTests: XCTestCase { 13 | let string = "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." 14 | 15 | func testContentAlignment() { 16 | typealias alignment = AttributedLabel.ContentAlignment 17 | let vSize = CGSize(width: 200, height: 200) 18 | let cSize = CGSize(width: 100, height: 100) 19 | 20 | XCTAssertEqual(alignment.center.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 50, y: 50)) 21 | XCTAssertEqual(alignment.top.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 50, y: 0)) 22 | XCTAssertEqual(alignment.bottom.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 50, y: 100)) 23 | XCTAssertEqual(alignment.left.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 0, y: 50)) 24 | XCTAssertEqual(alignment.right.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 100, y: 50)) 25 | XCTAssertEqual(alignment.topLeft.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 0, y: 0)) 26 | XCTAssertEqual(alignment.topRight.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 100, y: 0)) 27 | XCTAssertEqual(alignment.bottomLeft.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 0, y: 100)) 28 | XCTAssertEqual(alignment.bottomRight.alignOffset(viewSize: vSize, containerSize: cSize), CGPoint(x: 100, y: 100)) 29 | } 30 | 31 | func testProperties() { 32 | let attributedLabel = AttributedLabel(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 100))) 33 | 34 | attributedLabel.text = string 35 | XCTAssertEqual(attributedLabel.text, string) 36 | XCTAssertEqual(attributedLabel.attributedText?.string, string) 37 | XCTAssertEqual(attributedLabel.mergedAttributedText?.string, string) 38 | 39 | attributedLabel.text = nil 40 | XCTAssertEqual(attributedLabel.text, nil) 41 | XCTAssertEqual(attributedLabel.attributedText, nil) 42 | XCTAssertEqual(attributedLabel.mergedAttributedText, nil) 43 | 44 | attributedLabel.attributedText = NSAttributedString(string: string) 45 | XCTAssertEqual(attributedLabel.text, string) 46 | XCTAssertEqual(attributedLabel.attributedText?.string, string) 47 | XCTAssertEqual(attributedLabel.mergedAttributedText?.string, string) 48 | } 49 | 50 | func testContentSize() { 51 | let attributedLabel = AttributedLabel(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 100))) 52 | let tallestSize = CGSize(width: attributedLabel.bounds.width, height: CGFloat.greatestFiniteMagnitude) 53 | 54 | attributedLabel.usesIntrinsicContentSize = true 55 | XCTAssertEqual(attributedLabel.intrinsicContentSize, .zero) 56 | XCTAssertEqual(attributedLabel.sizeThatFits(tallestSize), .zero) 57 | 58 | let attrString = NSMutableAttributedString(string: string) 59 | 60 | attrString.addAttribute(.font, attr: UIFont.systemFont(ofSize: 10)) 61 | attrString.addAttribute(.font, attr: UIFont.systemFont(ofSize: 11)) 62 | attrString.addAttribute(.font, attr: UIFont.systemFont(ofSize: 12)) 63 | attrString.addAttribute(.font, attr: UIFont.systemFont(ofSize: 13)) 64 | attrString.addAttribute(.font, attr: UIFont.systemFont(ofSize: 14)) 65 | attrString.addAttribute(.paragraphStyle, attr: NSParagraphStyle.random()) 66 | attrString.addAttribute(.paragraphStyle, attr: NSParagraphStyle.random()) 67 | attrString.addAttribute(.paragraphStyle, attr: NSParagraphStyle.random()) 68 | attrString.addAttribute(.paragraphStyle, attr: NSParagraphStyle.random()) 69 | attrString.addAttribute(.paragraphStyle, attr: NSParagraphStyle.random()) 70 | attributedLabel.attributedText = attrString 71 | 72 | XCTAssertEqual(attributedLabel.intrinsicContentSize, attributedLabel.sizeThatFits(tallestSize)) 73 | 74 | attributedLabel.usesIntrinsicContentSize = false 75 | XCTAssertEqual(attributedLabel.intrinsicContentSize, attributedLabel.bounds.size) 76 | 77 | attributedLabel.usesIntrinsicContentSize = true 78 | XCTAssertNotEqual(attributedLabel.intrinsicContentSize, attributedLabel.bounds.size) 79 | 80 | attributedLabel.sizeToFit() 81 | XCTAssertEqual(attributedLabel.intrinsicContentSize, attributedLabel.bounds.size) 82 | } 83 | 84 | func testMergeAttributes() { 85 | let font = UIFont.systemFont(ofSize: 12) 86 | let color = UIColor.blue 87 | let style = NSParagraphStyle.random() 88 | let shadow = NSShadow.random() 89 | 90 | let attributedLabel = AttributedLabel() 91 | var mergedAttrString = attributedLabel.mergeAttributes(NSAttributedString(string: string)) 92 | 93 | XCTAssertNotEqual(mergedAttrString.attribute(.font, at: 0, effectiveRange: nil) as? UIFont, font) 94 | XCTAssertNotEqual(mergedAttrString.attribute(.font, at: string.count - 1, effectiveRange: nil) as? UIFont, font) 95 | XCTAssertNotEqual(mergedAttrString.attribute(.font, at: 0, effectiveRange: nil) as? UIFont, font) 96 | XCTAssertNotEqual(mergedAttrString.attribute(.font, at: string.count - 1, effectiveRange: nil) as? UIFont, font) 97 | XCTAssertNotEqual(mergedAttrString.attribute(.paragraphStyle, at: 0, effectiveRange: nil) as? NSParagraphStyle, style) 98 | XCTAssertNotEqual(mergedAttrString.attribute(.paragraphStyle, at: string.count - 1, effectiveRange: nil) as? NSParagraphStyle, style) 99 | XCTAssertNotEqual(mergedAttrString.attribute(.shadow, at: 0, effectiveRange: nil) as? NSShadow, shadow) 100 | XCTAssertNotEqual(mergedAttrString.attribute(.shadow, at: string.count - 1, effectiveRange: nil) as? NSShadow, shadow) 101 | 102 | attributedLabel.font = font 103 | attributedLabel.textColor = color 104 | attributedLabel.paragraphStyle = style 105 | attributedLabel.shadow = shadow 106 | 107 | mergedAttrString = attributedLabel.mergeAttributes(NSAttributedString(string: string)) 108 | 109 | XCTAssertEqual(mergedAttrString.attribute(.font, at: 0, effectiveRange: nil) as? UIFont, font) 110 | XCTAssertEqual(mergedAttrString.attribute(.font, at: string.count - 1, effectiveRange: nil) as? UIFont, font) 111 | XCTAssertEqual(mergedAttrString.attribute(.font, at: 0, effectiveRange: nil) as? UIFont, font) 112 | XCTAssertEqual(mergedAttrString.attribute(.font, at: string.count - 1, effectiveRange: nil) as? UIFont, font) 113 | XCTAssertEqual(mergedAttrString.attribute(.paragraphStyle, at: 0, effectiveRange: nil) as? NSParagraphStyle, style) 114 | XCTAssertEqual(mergedAttrString.attribute(.paragraphStyle, at: string.count - 1, effectiveRange: nil) as? NSParagraphStyle, style) 115 | XCTAssertEqual(mergedAttrString.attribute(.shadow, at: 0, effectiveRange: nil) as? NSShadow, shadow) 116 | XCTAssertEqual(mergedAttrString.attribute(.shadow, at: string.count - 1, effectiveRange: nil) as? NSShadow, shadow) 117 | } 118 | } 119 | 120 | extension NSShadow { 121 | static func random() -> NSShadow { 122 | let shadow = NSShadow() 123 | shadow.shadowBlurRadius = CGFloat(5.random()) 124 | shadow.shadowOffset = CGSize(width: 5.random(), height: 5.random()) 125 | shadow.shadowColor = UIColor(hue: CGFloat(10.random()) / 10, saturation: 0.8, brightness: 0.8, alpha: 1) 126 | return shadow 127 | } 128 | } 129 | 130 | extension NSParagraphStyle { 131 | private static func lineBreakMode() -> NSLineBreakMode { 132 | let mode: [NSLineBreakMode] = [.byWordWrapping, .byCharWrapping, .byClipping, .byTruncatingHead, .byTruncatingTail, .byTruncatingMiddle] 133 | return mode[mode.count.random()] 134 | } 135 | 136 | static func random() -> NSParagraphStyle { 137 | let paragraphStyle = NSMutableParagraphStyle() 138 | paragraphStyle.minimumLineHeight = CGFloat(10.random() + 10) 139 | paragraphStyle.maximumLineHeight = CGFloat(10.random() + 10) 140 | paragraphStyle.lineSpacing = CGFloat(10.random() - 5) 141 | paragraphStyle.lineBreakMode = lineBreakMode() 142 | return paragraphStyle 143 | } 144 | } 145 | 146 | extension NSMutableAttributedString { 147 | @discardableResult 148 | func addAttribute(_ attrName: NSAttributedString.Key, attr: AnyObject) -> Self { 149 | let location = self.length.random() 150 | let length = (self.length - location).random() 151 | let range = NSRange(location: location, length: length) 152 | 153 | enumerateAttribute(attrName, in: range, options: .reverse) { object, range, pointer in 154 | if object == nil { 155 | addAttributes([attrName: attr], range: range) 156 | } 157 | } 158 | 159 | return self 160 | } 161 | } 162 | 163 | extension Int { 164 | func random() -> Int { 165 | return Int(arc4random_uniform(UInt32(self))) 166 | } 167 | } 168 | 169 | -------------------------------------------------------------------------------- /AttributedLabelTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 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 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Kyohei Ito 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AttributedLabel 2 | 3 | [![Build Status](https://travis-ci.org/KyoheiG3/AttributedLabel.svg?branch=master)](https://travis-ci.org/KyoheiG3/AttributedLabel) 4 | [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) 5 | [![Version](https://img.shields.io/cocoapods/v/AttributedLabel.svg?style=flat)](http://cocoadocs.org/docsets/AttributedLabel) 6 | [![License](https://img.shields.io/cocoapods/l/AttributedLabel.svg?style=flat)](http://cocoadocs.org/docsets/AttributedLabel) 7 | [![Platform](https://img.shields.io/cocoapods/p/AttributedLabel.svg?style=flat)](http://cocoadocs.org/docsets/AttributedLabel) 8 | 9 | ![Graph](https://user-images.githubusercontent.com/5707132/33195812-1a93b3ac-d11e-11e7-89de-f5b5d21ac4a8.png) 10 | 11 | ##### Higher performance than `UILabel`. 12 | 13 | #### [Appetize's Demo](https://appetize.io/app/7q459fyg56828caye3ucdntqp0) 14 | 15 | ![Label](https://user-images.githubusercontent.com/5707132/33195811-182c5128-d11e-11e7-8945-0c4244429e19.gif) 16 | 17 | ## Overview 18 | 19 | This is a better performance than `UILabel` and can be used like a standard UI component. 20 | Also, Easier to use than `UILabel`. 21 | 22 | Since `UIView` is inherited instead of `UILabel`, there is little wasteful processing. 23 | It uses the function of TextKit to draw characters. 24 | 25 | However, please note that content layout is not done automatically. 26 | If want to automatically fix the height of the content, set `usesIntrinsicContentSize` to `true`. 27 | 28 | ##### Customization is easy. 29 | 30 | ![Customize](https://user-images.githubusercontent.com/5707132/33195810-159c27bc-d11e-11e7-9a8b-45c9c20567fd.gif) 31 | 32 | Left tab is customizable label. The center tab AttributedLabel So fast. Right tab is UILabel So slow. Fast more than 10 times from 5 times. 33 | 34 | - use the UIlabel 35 | 36 | ![UILabel](https://user-images.githubusercontent.com/5707132/33195814-1d55f622-d11e-11e7-8e8a-71ac9bc72375.png) 37 | 38 | - use the AttributedLabel 39 | 40 | ![AttributedLabel](https://user-images.githubusercontent.com/5707132/33195809-138ddda8-d11e-11e7-80fe-1ee33441d4b8.png) 41 | ! 42 | ## Requirements 43 | 44 | - Swift 5.0 45 | - iOS 7.0 or later 46 | - tvOS 9.0 or later 47 | 48 | ## How to Install AttributedLabel 49 | 50 | ### iOS 8+, tvOS 51 | 52 | #### CocoaPods 53 | 54 | Add the following to your `Podfile`: 55 | 56 | ```Ruby 57 | pod "AttributedLabel" 58 | ``` 59 | 60 | #### Carthage 61 | 62 | Add the following to your `Cartfile`: 63 | 64 | ```Ruby 65 | github "KyoheiG3/AttributedLabel" 66 | ``` 67 | 68 | ### iOS 7 69 | 70 | Just add everything in the `AttributedLabel.swift` file to your project. 71 | 72 | ## Usage 73 | 74 | ### Variable 75 | 76 | ```swift 77 | var numberOfLines: Int 78 | ``` 79 | - Same as `numberOfLines` of `UILabel`. 80 | - Default is `0`. 81 | 82 | ```swift 83 | var contentAlignment: AttributedLabel.ContentAlignment 84 | ``` 85 | - Alignment of content. 86 | - Default is `left`. 87 | 88 | ```swift 89 | @IBInspectable var padding: CGFloat 90 | ``` 91 | - `lineFragmentPadding` of `NSTextContainer`. 92 | - default is `0`. 93 | 94 | ```swift 95 | var font: UIFont 96 | ``` 97 | - Text font. 98 | - Default is system font 17 plain. 99 | 100 | ```swift 101 | var lineBreakMode: NSLineBreakMode 102 | ``` 103 | - Same as `lineBreakMode` of `UILabel`. 104 | - Default is `ByTruncatingTail`. 105 | 106 | ```swift 107 | @IBInspectable var textColor: UIColor? 108 | ``` 109 | - Default is nil (text draws black). 110 | 111 | ```swift 112 | var paragraphStyle: NSParagraphStyle? 113 | ``` 114 | - Default is nil. 115 | 116 | ```swift 117 | var shadow: NSShadow? 118 | ``` 119 | - Default is nil. 120 | 121 | ```swift 122 | var attributedText: NSAttributedString? 123 | ``` 124 | - Default is nil. 125 | 126 | ```swift 127 | @IBInspectable var text: String? 128 | ``` 129 | - Default is nil. 130 | 131 | ```swift 132 | var usesIntrinsicContentSize: Bool 133 | ``` 134 | - If need to use intrinsicContentSize set true. 135 | - Should call invalidateIntrinsicContentSize when intrinsicContentSize is cached. When text was changed for example. 136 | - Default is `false`. 137 | 138 | ```swift 139 | var preferredMaxLayoutWidth: CGFloat 140 | ``` 141 | - Support for constraint-based layout (auto layout) 142 | - If nonzero, this is used when determining intrinsicContentSize for multiline labels 143 | 144 | ### Function 145 | 146 | ```swift 147 | override func sizeThatFits(size: CGSize) -> CGSize 148 | ``` 149 | - Same as `sizeThatFits` of `UILabel`. 150 | 151 | ```swift 152 | override func sizeToFit() 153 | ``` 154 | - Fit like `UILabel`. 155 | 156 | ## Author 157 | 158 | #### Kyohei Ito 159 | 160 | - [GitHub](https://github.com/kyoheig3) 161 | - [Twitter](https://twitter.com/kyoheig3) 162 | 163 | Follow me 🎉 164 | 165 | ## LICENSE 166 | 167 | Under the MIT license. See LICENSE file for details. 168 | --------------------------------------------------------------------------------