├── .gitignore ├── Demo ├── AutoMultipleLineLabel.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata ├── AutoMultipleLineLabel │ ├── AppDelegate.swift │ ├── Base.lproj │ │ ├── LaunchScreen.xib │ │ └── Main.storyboard │ ├── Images.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── Info.plist │ ├── ViewController.swift │ └── ZHAutoLinesLabel.swift └── AutoMultipleLineLabelTests │ ├── AutoMultipleLineLabelTests.swift │ └── Info.plist ├── LICENSE ├── Pic ├── 1.png ├── 2.png ├── 3.png └── Pre1.png ├── README.md └── ZHAutoLinesLabel.swift /.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 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | FAD521D81A4344020068356B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAD521D71A4344020068356B /* AppDelegate.swift */; }; 11 | FAD521DA1A4344020068356B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAD521D91A4344020068356B /* ViewController.swift */; }; 12 | FAD521DD1A4344020068356B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FAD521DB1A4344020068356B /* Main.storyboard */; }; 13 | FAD521DF1A4344020068356B /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FAD521DE1A4344020068356B /* Images.xcassets */; }; 14 | FAD521E21A4344020068356B /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = FAD521E01A4344020068356B /* LaunchScreen.xib */; }; 15 | FAD521EE1A4344020068356B /* AutoMultipleLineLabelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAD521ED1A4344020068356B /* AutoMultipleLineLabelTests.swift */; }; 16 | FAD521F81A4344A10068356B /* ZHAutoLinesLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAD521F71A4344A10068356B /* ZHAutoLinesLabel.swift */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXContainerItemProxy section */ 20 | FAD521E81A4344020068356B /* PBXContainerItemProxy */ = { 21 | isa = PBXContainerItemProxy; 22 | containerPortal = FAD521CA1A4344020068356B /* Project object */; 23 | proxyType = 1; 24 | remoteGlobalIDString = FAD521D11A4344020068356B; 25 | remoteInfo = AutoMultipleLineLabel; 26 | }; 27 | /* End PBXContainerItemProxy section */ 28 | 29 | /* Begin PBXFileReference section */ 30 | FAD521D21A4344020068356B /* AutoMultipleLineLabel.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AutoMultipleLineLabel.app; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | FAD521D61A4344020068356B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32 | FAD521D71A4344020068356B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 33 | FAD521D91A4344020068356B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 34 | FAD521DC1A4344020068356B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 35 | FAD521DE1A4344020068356B /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 36 | FAD521E11A4344020068356B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 37 | FAD521E71A4344020068356B /* AutoMultipleLineLabelTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AutoMultipleLineLabelTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 38 | FAD521EC1A4344020068356B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 39 | FAD521ED1A4344020068356B /* AutoMultipleLineLabelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoMultipleLineLabelTests.swift; sourceTree = ""; }; 40 | FAD521F71A4344A10068356B /* ZHAutoLinesLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZHAutoLinesLabel.swift; sourceTree = ""; }; 41 | /* End PBXFileReference section */ 42 | 43 | /* Begin PBXFrameworksBuildPhase section */ 44 | FAD521CF1A4344020068356B /* Frameworks */ = { 45 | isa = PBXFrameworksBuildPhase; 46 | buildActionMask = 2147483647; 47 | files = ( 48 | ); 49 | runOnlyForDeploymentPostprocessing = 0; 50 | }; 51 | FAD521E41A4344020068356B /* Frameworks */ = { 52 | isa = PBXFrameworksBuildPhase; 53 | buildActionMask = 2147483647; 54 | files = ( 55 | ); 56 | runOnlyForDeploymentPostprocessing = 0; 57 | }; 58 | /* End PBXFrameworksBuildPhase section */ 59 | 60 | /* Begin PBXGroup section */ 61 | FAD521C91A4344020068356B = { 62 | isa = PBXGroup; 63 | children = ( 64 | FAD521D41A4344020068356B /* AutoMultipleLineLabel */, 65 | FAD521EA1A4344020068356B /* AutoMultipleLineLabelTests */, 66 | FAD521D31A4344020068356B /* Products */, 67 | ); 68 | sourceTree = ""; 69 | }; 70 | FAD521D31A4344020068356B /* Products */ = { 71 | isa = PBXGroup; 72 | children = ( 73 | FAD521D21A4344020068356B /* AutoMultipleLineLabel.app */, 74 | FAD521E71A4344020068356B /* AutoMultipleLineLabelTests.xctest */, 75 | ); 76 | name = Products; 77 | sourceTree = ""; 78 | }; 79 | FAD521D41A4344020068356B /* AutoMultipleLineLabel */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | FAD521D71A4344020068356B /* AppDelegate.swift */, 83 | FAD521D91A4344020068356B /* ViewController.swift */, 84 | FAD521DB1A4344020068356B /* Main.storyboard */, 85 | FAD521F71A4344A10068356B /* ZHAutoLinesLabel.swift */, 86 | FAD521DE1A4344020068356B /* Images.xcassets */, 87 | FAD521E01A4344020068356B /* LaunchScreen.xib */, 88 | FAD521D51A4344020068356B /* Supporting Files */, 89 | ); 90 | path = AutoMultipleLineLabel; 91 | sourceTree = ""; 92 | }; 93 | FAD521D51A4344020068356B /* Supporting Files */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | FAD521D61A4344020068356B /* Info.plist */, 97 | ); 98 | name = "Supporting Files"; 99 | sourceTree = ""; 100 | }; 101 | FAD521EA1A4344020068356B /* AutoMultipleLineLabelTests */ = { 102 | isa = PBXGroup; 103 | children = ( 104 | FAD521ED1A4344020068356B /* AutoMultipleLineLabelTests.swift */, 105 | FAD521EB1A4344020068356B /* Supporting Files */, 106 | ); 107 | path = AutoMultipleLineLabelTests; 108 | sourceTree = ""; 109 | }; 110 | FAD521EB1A4344020068356B /* Supporting Files */ = { 111 | isa = PBXGroup; 112 | children = ( 113 | FAD521EC1A4344020068356B /* Info.plist */, 114 | ); 115 | name = "Supporting Files"; 116 | sourceTree = ""; 117 | }; 118 | /* End PBXGroup section */ 119 | 120 | /* Begin PBXNativeTarget section */ 121 | FAD521D11A4344020068356B /* AutoMultipleLineLabel */ = { 122 | isa = PBXNativeTarget; 123 | buildConfigurationList = FAD521F11A4344020068356B /* Build configuration list for PBXNativeTarget "AutoMultipleLineLabel" */; 124 | buildPhases = ( 125 | FAD521CE1A4344020068356B /* Sources */, 126 | FAD521CF1A4344020068356B /* Frameworks */, 127 | FAD521D01A4344020068356B /* Resources */, 128 | ); 129 | buildRules = ( 130 | ); 131 | dependencies = ( 132 | ); 133 | name = AutoMultipleLineLabel; 134 | productName = AutoMultipleLineLabel; 135 | productReference = FAD521D21A4344020068356B /* AutoMultipleLineLabel.app */; 136 | productType = "com.apple.product-type.application"; 137 | }; 138 | FAD521E61A4344020068356B /* AutoMultipleLineLabelTests */ = { 139 | isa = PBXNativeTarget; 140 | buildConfigurationList = FAD521F41A4344020068356B /* Build configuration list for PBXNativeTarget "AutoMultipleLineLabelTests" */; 141 | buildPhases = ( 142 | FAD521E31A4344020068356B /* Sources */, 143 | FAD521E41A4344020068356B /* Frameworks */, 144 | FAD521E51A4344020068356B /* Resources */, 145 | ); 146 | buildRules = ( 147 | ); 148 | dependencies = ( 149 | FAD521E91A4344020068356B /* PBXTargetDependency */, 150 | ); 151 | name = AutoMultipleLineLabelTests; 152 | productName = AutoMultipleLineLabelTests; 153 | productReference = FAD521E71A4344020068356B /* AutoMultipleLineLabelTests.xctest */; 154 | productType = "com.apple.product-type.bundle.unit-test"; 155 | }; 156 | /* End PBXNativeTarget section */ 157 | 158 | /* Begin PBXProject section */ 159 | FAD521CA1A4344020068356B /* Project object */ = { 160 | isa = PBXProject; 161 | attributes = { 162 | LastUpgradeCheck = 0610; 163 | ORGANIZATIONNAME = HonghaoZ; 164 | TargetAttributes = { 165 | FAD521D11A4344020068356B = { 166 | CreatedOnToolsVersion = 6.1.1; 167 | }; 168 | FAD521E61A4344020068356B = { 169 | CreatedOnToolsVersion = 6.1.1; 170 | TestTargetID = FAD521D11A4344020068356B; 171 | }; 172 | }; 173 | }; 174 | buildConfigurationList = FAD521CD1A4344020068356B /* Build configuration list for PBXProject "AutoMultipleLineLabel" */; 175 | compatibilityVersion = "Xcode 3.2"; 176 | developmentRegion = English; 177 | hasScannedForEncodings = 0; 178 | knownRegions = ( 179 | en, 180 | Base, 181 | ); 182 | mainGroup = FAD521C91A4344020068356B; 183 | productRefGroup = FAD521D31A4344020068356B /* Products */; 184 | projectDirPath = ""; 185 | projectRoot = ""; 186 | targets = ( 187 | FAD521D11A4344020068356B /* AutoMultipleLineLabel */, 188 | FAD521E61A4344020068356B /* AutoMultipleLineLabelTests */, 189 | ); 190 | }; 191 | /* End PBXProject section */ 192 | 193 | /* Begin PBXResourcesBuildPhase section */ 194 | FAD521D01A4344020068356B /* Resources */ = { 195 | isa = PBXResourcesBuildPhase; 196 | buildActionMask = 2147483647; 197 | files = ( 198 | FAD521DD1A4344020068356B /* Main.storyboard in Resources */, 199 | FAD521E21A4344020068356B /* LaunchScreen.xib in Resources */, 200 | FAD521DF1A4344020068356B /* Images.xcassets in Resources */, 201 | ); 202 | runOnlyForDeploymentPostprocessing = 0; 203 | }; 204 | FAD521E51A4344020068356B /* Resources */ = { 205 | isa = PBXResourcesBuildPhase; 206 | buildActionMask = 2147483647; 207 | files = ( 208 | ); 209 | runOnlyForDeploymentPostprocessing = 0; 210 | }; 211 | /* End PBXResourcesBuildPhase section */ 212 | 213 | /* Begin PBXSourcesBuildPhase section */ 214 | FAD521CE1A4344020068356B /* Sources */ = { 215 | isa = PBXSourcesBuildPhase; 216 | buildActionMask = 2147483647; 217 | files = ( 218 | FAD521DA1A4344020068356B /* ViewController.swift in Sources */, 219 | FAD521D81A4344020068356B /* AppDelegate.swift in Sources */, 220 | FAD521F81A4344A10068356B /* ZHAutoLinesLabel.swift in Sources */, 221 | ); 222 | runOnlyForDeploymentPostprocessing = 0; 223 | }; 224 | FAD521E31A4344020068356B /* Sources */ = { 225 | isa = PBXSourcesBuildPhase; 226 | buildActionMask = 2147483647; 227 | files = ( 228 | FAD521EE1A4344020068356B /* AutoMultipleLineLabelTests.swift in Sources */, 229 | ); 230 | runOnlyForDeploymentPostprocessing = 0; 231 | }; 232 | /* End PBXSourcesBuildPhase section */ 233 | 234 | /* Begin PBXTargetDependency section */ 235 | FAD521E91A4344020068356B /* PBXTargetDependency */ = { 236 | isa = PBXTargetDependency; 237 | target = FAD521D11A4344020068356B /* AutoMultipleLineLabel */; 238 | targetProxy = FAD521E81A4344020068356B /* PBXContainerItemProxy */; 239 | }; 240 | /* End PBXTargetDependency section */ 241 | 242 | /* Begin PBXVariantGroup section */ 243 | FAD521DB1A4344020068356B /* Main.storyboard */ = { 244 | isa = PBXVariantGroup; 245 | children = ( 246 | FAD521DC1A4344020068356B /* Base */, 247 | ); 248 | name = Main.storyboard; 249 | sourceTree = ""; 250 | }; 251 | FAD521E01A4344020068356B /* LaunchScreen.xib */ = { 252 | isa = PBXVariantGroup; 253 | children = ( 254 | FAD521E11A4344020068356B /* Base */, 255 | ); 256 | name = LaunchScreen.xib; 257 | sourceTree = ""; 258 | }; 259 | /* End PBXVariantGroup section */ 260 | 261 | /* Begin XCBuildConfiguration section */ 262 | FAD521EF1A4344020068356B /* Debug */ = { 263 | isa = XCBuildConfiguration; 264 | buildSettings = { 265 | ALWAYS_SEARCH_USER_PATHS = NO; 266 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 267 | CLANG_CXX_LIBRARY = "libc++"; 268 | CLANG_ENABLE_MODULES = YES; 269 | CLANG_ENABLE_OBJC_ARC = YES; 270 | CLANG_WARN_BOOL_CONVERSION = YES; 271 | CLANG_WARN_CONSTANT_CONVERSION = YES; 272 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 273 | CLANG_WARN_EMPTY_BODY = YES; 274 | CLANG_WARN_ENUM_CONVERSION = YES; 275 | CLANG_WARN_INT_CONVERSION = YES; 276 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 277 | CLANG_WARN_UNREACHABLE_CODE = YES; 278 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 279 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 280 | COPY_PHASE_STRIP = NO; 281 | ENABLE_STRICT_OBJC_MSGSEND = YES; 282 | GCC_C_LANGUAGE_STANDARD = gnu99; 283 | GCC_DYNAMIC_NO_PIC = NO; 284 | GCC_OPTIMIZATION_LEVEL = 0; 285 | GCC_PREPROCESSOR_DEFINITIONS = ( 286 | "DEBUG=1", 287 | "$(inherited)", 288 | ); 289 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 290 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 291 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 292 | GCC_WARN_UNDECLARED_SELECTOR = YES; 293 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 294 | GCC_WARN_UNUSED_FUNCTION = YES; 295 | GCC_WARN_UNUSED_VARIABLE = YES; 296 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 297 | MTL_ENABLE_DEBUG_INFO = YES; 298 | ONLY_ACTIVE_ARCH = YES; 299 | SDKROOT = iphoneos; 300 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 301 | TARGETED_DEVICE_FAMILY = "1,2"; 302 | }; 303 | name = Debug; 304 | }; 305 | FAD521F01A4344020068356B /* Release */ = { 306 | isa = XCBuildConfiguration; 307 | buildSettings = { 308 | ALWAYS_SEARCH_USER_PATHS = NO; 309 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 310 | CLANG_CXX_LIBRARY = "libc++"; 311 | CLANG_ENABLE_MODULES = YES; 312 | CLANG_ENABLE_OBJC_ARC = YES; 313 | CLANG_WARN_BOOL_CONVERSION = YES; 314 | CLANG_WARN_CONSTANT_CONVERSION = YES; 315 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 316 | CLANG_WARN_EMPTY_BODY = YES; 317 | CLANG_WARN_ENUM_CONVERSION = YES; 318 | CLANG_WARN_INT_CONVERSION = YES; 319 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 320 | CLANG_WARN_UNREACHABLE_CODE = YES; 321 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 322 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 323 | COPY_PHASE_STRIP = YES; 324 | ENABLE_NS_ASSERTIONS = NO; 325 | ENABLE_STRICT_OBJC_MSGSEND = YES; 326 | GCC_C_LANGUAGE_STANDARD = gnu99; 327 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 328 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 329 | GCC_WARN_UNDECLARED_SELECTOR = YES; 330 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 331 | GCC_WARN_UNUSED_FUNCTION = YES; 332 | GCC_WARN_UNUSED_VARIABLE = YES; 333 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 334 | MTL_ENABLE_DEBUG_INFO = NO; 335 | SDKROOT = iphoneos; 336 | TARGETED_DEVICE_FAMILY = "1,2"; 337 | VALIDATE_PRODUCT = YES; 338 | }; 339 | name = Release; 340 | }; 341 | FAD521F21A4344020068356B /* Debug */ = { 342 | isa = XCBuildConfiguration; 343 | buildSettings = { 344 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 345 | INFOPLIST_FILE = AutoMultipleLineLabel/Info.plist; 346 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 347 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 348 | PRODUCT_NAME = "$(TARGET_NAME)"; 349 | }; 350 | name = Debug; 351 | }; 352 | FAD521F31A4344020068356B /* Release */ = { 353 | isa = XCBuildConfiguration; 354 | buildSettings = { 355 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 356 | INFOPLIST_FILE = AutoMultipleLineLabel/Info.plist; 357 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 358 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 359 | PRODUCT_NAME = "$(TARGET_NAME)"; 360 | }; 361 | name = Release; 362 | }; 363 | FAD521F51A4344020068356B /* Debug */ = { 364 | isa = XCBuildConfiguration; 365 | buildSettings = { 366 | BUNDLE_LOADER = "$(TEST_HOST)"; 367 | FRAMEWORK_SEARCH_PATHS = ( 368 | "$(SDKROOT)/Developer/Library/Frameworks", 369 | "$(inherited)", 370 | ); 371 | GCC_PREPROCESSOR_DEFINITIONS = ( 372 | "DEBUG=1", 373 | "$(inherited)", 374 | ); 375 | INFOPLIST_FILE = AutoMultipleLineLabelTests/Info.plist; 376 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 377 | PRODUCT_NAME = "$(TARGET_NAME)"; 378 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AutoMultipleLineLabel.app/AutoMultipleLineLabel"; 379 | }; 380 | name = Debug; 381 | }; 382 | FAD521F61A4344020068356B /* Release */ = { 383 | isa = XCBuildConfiguration; 384 | buildSettings = { 385 | BUNDLE_LOADER = "$(TEST_HOST)"; 386 | FRAMEWORK_SEARCH_PATHS = ( 387 | "$(SDKROOT)/Developer/Library/Frameworks", 388 | "$(inherited)", 389 | ); 390 | INFOPLIST_FILE = AutoMultipleLineLabelTests/Info.plist; 391 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 392 | PRODUCT_NAME = "$(TARGET_NAME)"; 393 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AutoMultipleLineLabel.app/AutoMultipleLineLabel"; 394 | }; 395 | name = Release; 396 | }; 397 | /* End XCBuildConfiguration section */ 398 | 399 | /* Begin XCConfigurationList section */ 400 | FAD521CD1A4344020068356B /* Build configuration list for PBXProject "AutoMultipleLineLabel" */ = { 401 | isa = XCConfigurationList; 402 | buildConfigurations = ( 403 | FAD521EF1A4344020068356B /* Debug */, 404 | FAD521F01A4344020068356B /* Release */, 405 | ); 406 | defaultConfigurationIsVisible = 0; 407 | defaultConfigurationName = Release; 408 | }; 409 | FAD521F11A4344020068356B /* Build configuration list for PBXNativeTarget "AutoMultipleLineLabel" */ = { 410 | isa = XCConfigurationList; 411 | buildConfigurations = ( 412 | FAD521F21A4344020068356B /* Debug */, 413 | FAD521F31A4344020068356B /* Release */, 414 | ); 415 | defaultConfigurationIsVisible = 0; 416 | defaultConfigurationName = Release; 417 | }; 418 | FAD521F41A4344020068356B /* Build configuration list for PBXNativeTarget "AutoMultipleLineLabelTests" */ = { 419 | isa = XCConfigurationList; 420 | buildConfigurations = ( 421 | FAD521F51A4344020068356B /* Debug */, 422 | FAD521F61A4344020068356B /* Release */, 423 | ); 424 | defaultConfigurationIsVisible = 0; 425 | defaultConfigurationName = Release; 426 | }; 427 | /* End XCConfigurationList section */ 428 | }; 429 | rootObject = FAD521CA1A4344020068356B /* Project object */; 430 | } 431 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // AutoMultipleLineLabel 4 | // 5 | // Created by Honghao Zhang on 2014-12-18. 6 | // Copyright (c) 2014 HonghaoZ. 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 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel/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 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.honghaoz.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // AutoMultipleLineLabel 4 | // 5 | // Created by Honghao Zhang on 2014-12-18. 6 | // Copyright (c) 2014 HonghaoZ. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | @IBOutlet weak var label: ZHAutoLinesLabel! 14 | @IBOutlet weak var constraintLabelWidth: NSLayoutConstraint! 15 | 16 | override func viewDidLoad() { 17 | super.viewDidLoad() 18 | println("viewDidLoad") 19 | label.contentInset = UIEdgeInsetsMake(10, 20, 10, 20) 20 | } 21 | 22 | @IBAction func changeWidth(sender: AnyObject) { 23 | constraintLabelWidth.constant = random(Int(UIScreen.mainScreen().bounds.width - 10 - label.contentInset.left - label.contentInset.right)) + label.contentInset.left + label.contentInset.right 24 | self.view.layoutIfNeeded() 25 | } 26 | 27 | @IBAction func changeContentInset(sender: AnyObject) { 28 | label.contentInset = UIEdgeInsetsMake(random(50), random(50), random(50), random(50)) 29 | } 30 | } 31 | 32 | func random(range: Int) -> CGFloat { 33 | return CGFloat(arc4random_uniform(UInt32(range))) 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabel/ZHAutoLinesLabel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ZHAutoLinesLabel.swift 3 | // ZHAutoLinesLabel 4 | // 5 | // Created by Honghao Zhang on 2014-12-18. 6 | // Copyright (c) 2014 HonghaoZ. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ZHAutoLinesLabel: UILabel { 12 | var contentInset: UIEdgeInsets = UIEdgeInsetsZero { 13 | didSet { 14 | // Force label to update 15 | let originalText = self.text 16 | self.text = "" 17 | self.text = originalText 18 | self.superview?.setNeedsLayout() 19 | } 20 | } 21 | 22 | convenience override init() { 23 | self.init(frame: CGRectZero) 24 | } 25 | 26 | override init(frame: CGRect) { 27 | super.init(frame: frame) 28 | setup() 29 | } 30 | 31 | required init(coder aDecoder: NSCoder) { 32 | super.init(coder: aDecoder) 33 | setup() 34 | } 35 | 36 | private func setup() { 37 | // Content is never compressed 38 | self.setContentCompressionResistancePriority(1000, forAxis: .Horizontal) 39 | self.setContentCompressionResistancePriority(1000, forAxis: .Vertical) 40 | } 41 | 42 | override func layoutSubviews() { 43 | super.layoutSubviews() 44 | let targetWidth = CGRectGetWidth(self.bounds) 45 | // Once label's widht is changed, update preferredMaxLayoutWidth, this will lead recall textRectForBounds 46 | if self.preferredMaxLayoutWidth != targetWidth { 47 | self.preferredMaxLayoutWidth = targetWidth 48 | self.superview?.setNeedsLayout() 49 | } 50 | self.superview?.setNeedsLayout() 51 | } 52 | 53 | override func drawTextInRect(rect: CGRect) { 54 | // Rect has been veritcally expanded in textRectForBounds 55 | super.drawTextInRect(UIEdgeInsetsInsetRect(rect, contentInset)) 56 | } 57 | 58 | override func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect { 59 | // Use a shrinked rect to calculate new rect, this will lead to a higher rectangle to draw 60 | // The width is same as preferredMaxLayoutWidth 61 | // Reference: http://stackoverflow.com/questions/21167226/resizing-a-uilabel-to-accomodate-insets 62 | 63 | var rect = super.textRectForBounds(UIEdgeInsetsInsetRect(bounds, contentInset), limitedToNumberOfLines: numberOfLines) 64 | // Move rect to origin 65 | rect.origin.x -= contentInset.left; 66 | rect.origin.y -= contentInset.top; 67 | rect.size.width += (contentInset.left + contentInset.right); 68 | rect.size.height += (contentInset.top + contentInset.bottom); 69 | return rect 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabelTests/AutoMultipleLineLabelTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AutoMultipleLineLabelTests.swift 3 | // AutoMultipleLineLabelTests 4 | // 5 | // Created by Honghao Zhang on 2014-12-18. 6 | // Copyright (c) 2014 HonghaoZ. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import XCTest 11 | 12 | class AutoMultipleLineLabelTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | XCTAssert(true, "Pass") 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measureBlock() { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Demo/AutoMultipleLineLabelTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.honghaoz.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Honghao Zhang 张宏昊 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 | -------------------------------------------------------------------------------- /Pic/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/honghaoz/ZHAutoLinesLabel/8f17a7bf5e5dffea370dce81fb6c6f73b0097371/Pic/1.png -------------------------------------------------------------------------------- /Pic/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/honghaoz/ZHAutoLinesLabel/8f17a7bf5e5dffea370dce81fb6c6f73b0097371/Pic/2.png -------------------------------------------------------------------------------- /Pic/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/honghaoz/ZHAutoLinesLabel/8f17a7bf5e5dffea370dce81fb6c6f73b0097371/Pic/3.png -------------------------------------------------------------------------------- /Pic/Pre1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/honghaoz/ZHAutoLinesLabel/8f17a7bf5e5dffea370dce81fb6c6f73b0097371/Pic/Pre1.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZHAutoLinesLabel 2 | 3 | ZHAutoLinesLabel is a subclass of UILabel, support contentInset and automatic multiple lines 4 | 5 | ![](https://raw.githubusercontent.com/honghaoz/ZHAutoLinesLabel/master/Pic/Pre1.png) 6 | 7 | # Why I create this label 8 | 9 | ### For automatic preferredMaxLayoutWidth 10 | I love Autolayout, for label, I offen do this: 11 | ``` 12 | self.setContentCompressionResistancePriority(1000, forAxis: .Horizontal) 13 | self.setContentCompressionResistancePriority(1000, forAxis: .Vertical) 14 | self.setContentHuggingPriority(1000, forAxis: .Horizontal) 15 | self.setContentHuggingPriority(1000, forAxis: .Vertical) 16 | ``` 17 | 18 | If we set `numberOfLines = 0`, label will automatically resize it approtiately. 19 | 20 | I love Interface Builder, I offen set these four priorities in IB like this 21 | 22 | ![](https://raw.githubusercontent.com/honghaoz/ZHAutoLinesLabel/master/Pic/1.png) 23 | 24 | However, there's a property `preferredMaxLayoutWidth`, in IB, it's 25 | 26 | ![](https://raw.githubusercontent.com/honghaoz/ZHAutoLinesLabel/master/Pic/2.png) 27 | 28 | This can be automatic only for iOS8, which means the value will be equal to view's width. 29 | 30 | If our project supports for iOS7 or iOS6, we will have a warning: 31 | ``` 32 | Main.storyboard: warning: Attribute Unavailable: Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0 33 | ``` 34 | we have to give it an explicit value in IB to get rid of it. If we want it's automatic, we have to link this label, then update `preferredMaxLayoutWidth` in code. This is trivial. 35 | 36 | To let iOS7 have same behavior like iOS8 and get rid of this kind of warning, I created this label. 37 | 38 | What you need to do is always set `preferredMaxLayoutWidth` explicitly and it will work automatically. 39 | 40 | ![](https://raw.githubusercontent.com/honghaoz/ZHAutoLinesLabel/master/Pic/3.png) 41 | 42 | (Better to choose same width value as label, this will give you correct layout in IB) 43 | 44 | ### For contentInset just like UITextView 45 | 46 | Sometimes, we want label to have a background and margin around it. One way is to creat a container view and add a lable on that, this is bad in IB or code. 47 | 48 | ZHAutoLinesLabel has a new property `contentInset: UIEdgeInsets`, same with UITextView, also, it works with Autolayout. 49 | 50 | # The MIT License (MIT) 51 | The MIT License (MIT) 52 | Copyright (c) 2014 Honghao Zhang 53 | 54 | Permission is hereby granted, free of charge, to any person obtaining a copy 55 | of this software and associated documentation files (the "Software"), to deal 56 | in the Software without restriction, including without limitation the rights 57 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 58 | copies of the Software, and to permit persons to whom the Software is 59 | furnished to do so, subject to the following conditions: 60 | 61 | The above copyright notice and this permission notice shall be included in all 62 | copies or substantial portions of the Software. 63 | 64 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 65 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 66 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 67 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 68 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 69 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 70 | SOFTWARE. 71 | -------------------------------------------------------------------------------- /ZHAutoLinesLabel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ZHAutoLinesLabel.swift 3 | // ZHAutoLinesLabel 4 | // 5 | // Created by Honghao Zhang on 2014-12-18. 6 | // Copyright (c) 2014 HonghaoZ. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ZHAutoLinesLabel: UILabel { 12 | var contentInset: UIEdgeInsets = UIEdgeInsetsZero { 13 | didSet { 14 | // Force label to update 15 | let originalText = self.text 16 | self.text = "" 17 | self.text = originalText 18 | self.superview?.setNeedsLayout() 19 | } 20 | } 21 | 22 | override init(frame: CGRect) { 23 | super.init(frame: frame) 24 | setup() 25 | } 26 | 27 | required init(coder aDecoder: NSCoder) { 28 | super.init(coder: aDecoder) 29 | setup() 30 | } 31 | 32 | private func setup() { 33 | // Content is never compressed 34 | self.setContentCompressionResistancePriority(1000, forAxis: .Horizontal) 35 | self.setContentCompressionResistancePriority(1000, forAxis: .Vertical) 36 | } 37 | 38 | override func layoutSubviews() { 39 | super.layoutSubviews() 40 | let targetWidth = CGRectGetWidth(self.bounds) 41 | // Once label's widht is changed, update preferredMaxLayoutWidth, this will lead recall textRectForBounds 42 | if self.preferredMaxLayoutWidth != targetWidth { 43 | self.preferredMaxLayoutWidth = targetWidth 44 | self.superview?.setNeedsLayout() 45 | } 46 | self.superview?.setNeedsLayout() 47 | } 48 | 49 | override func drawTextInRect(rect: CGRect) { 50 | // Rect has been veritcally expanded in textRectForBounds 51 | super.drawTextInRect(UIEdgeInsetsInsetRect(rect, contentInset)) 52 | } 53 | 54 | override func textRectForBounds(bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect { 55 | // Use a shrinked rect to calculate new rect, this will lead to a higher rectangle to draw 56 | // The width is same as preferredMaxLayoutWidth 57 | // Reference: http://stackoverflow.com/questions/21167226/resizing-a-uilabel-to-accomodate-insets 58 | 59 | var rect = super.textRectForBounds(UIEdgeInsetsInsetRect(bounds, contentInset), limitedToNumberOfLines: numberOfLines) 60 | // Move rect to origin 61 | rect.origin.x -= contentInset.left; 62 | rect.origin.y -= contentInset.top; 63 | rect.size.width += (contentInset.left + contentInset.right); 64 | rect.size.height += (contentInset.top + contentInset.bottom); 65 | return rect 66 | } 67 | } 68 | --------------------------------------------------------------------------------