├── README.md ├── StringCalculate.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcuserdata │ │ └── ruiwendaier.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── ruiwendaier.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ └── xcschememanagement.plist ├── StringCalculate ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── StringCalculateManager.swift ├── ViewController.swift └── bridge.h ├── StringCalculateTests ├── Info.plist └── StringCalculateTests.swift └── StringCalculateUITests ├── Info.plist └── StringCalculateUITests.swift /README.md: -------------------------------------------------------------------------------- 1 | # StringCalculate 2 | 3 | 一种更加方便和高效计算多行Label高度的新方法 4 | 5 | 与系统的提供的boundingRect相比,优点主要有以下两点: 6 | 7 | 1.耗时较少 8 | 9 | 在Demo和我们实际项目中测试结果中,时间消耗约为系统的boundingRect方法的30%。 10 | 11 | 2.调用更加方便。 12 | 13 | 在使用系统的boundingRect方法进行Label高度计算时,通常情况下,我们需要根据最大行数来估算Label最大的高度,类似于这样: 14 | ``` 15 | let maxLine = 3//最大行数 16 | let singleLineHeight = 20//单行高度 17 | let maxHeight = CGFloat(maxLine * singleLineHeight)//计算得到最大宽度 18 | let rect = text.boundingRect(with: CGSize(width: UIScreen.main.bounds.size.width, height: maxHeight), 19 | options: .usesLineFragmentOrigin, 20 | attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16)], 21 | context: nil).size.width 22 | ``` 23 | 但是在使用这种新方法计算时,我们只需要指定最大高度就行了,类似于这样: 24 | ``` 25 | let rect = text.boundingRectFast(withMaxWidth: UIScreen.main.bounds.size.width, 26 | font: UIFont.boldSystemFont(ofSize: 16), 27 | maxLine: maxLine) 28 | ``` 29 | 30 | 介绍文章:https://mp.weixin.qq.com/s/zHAlxALPMFZLv8M8bJaRKA 31 | 32 | 有问题或者建议欢迎加我微信ruiwendelll,我们一起探讨学习,谢谢了! 33 | 34 | -------------------------------------------------------------------------------- /StringCalculate.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | EE68BFC321BA49F300B7CD9A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE68BFC221BA49F300B7CD9A /* AppDelegate.swift */; }; 11 | EE68BFC521BA49F300B7CD9A /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE68BFC421BA49F300B7CD9A /* ViewController.swift */; }; 12 | EE68BFC821BA49F300B7CD9A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EE68BFC621BA49F300B7CD9A /* Main.storyboard */; }; 13 | EE68BFCA21BA49F600B7CD9A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = EE68BFC921BA49F600B7CD9A /* Assets.xcassets */; }; 14 | EE68BFCD21BA49F600B7CD9A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EE68BFCB21BA49F600B7CD9A /* LaunchScreen.storyboard */; }; 15 | EE68BFD821BA49F600B7CD9A /* StringCalculateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE68BFD721BA49F600B7CD9A /* StringCalculateTests.swift */; }; 16 | EE68BFE321BA49F600B7CD9A /* StringCalculateUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE68BFE221BA49F600B7CD9A /* StringCalculateUITests.swift */; }; 17 | EE68BFF121BA4A0F00B7CD9A /* StringCalculateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE68BFF021BA4A0F00B7CD9A /* StringCalculateManager.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXContainerItemProxy section */ 21 | EE68BFD421BA49F600B7CD9A /* PBXContainerItemProxy */ = { 22 | isa = PBXContainerItemProxy; 23 | containerPortal = EE68BFB721BA49F300B7CD9A /* Project object */; 24 | proxyType = 1; 25 | remoteGlobalIDString = EE68BFBE21BA49F300B7CD9A; 26 | remoteInfo = StringCalculate; 27 | }; 28 | EE68BFDF21BA49F600B7CD9A /* PBXContainerItemProxy */ = { 29 | isa = PBXContainerItemProxy; 30 | containerPortal = EE68BFB721BA49F300B7CD9A /* Project object */; 31 | proxyType = 1; 32 | remoteGlobalIDString = EE68BFBE21BA49F300B7CD9A; 33 | remoteInfo = StringCalculate; 34 | }; 35 | /* End PBXContainerItemProxy section */ 36 | 37 | /* Begin PBXFileReference section */ 38 | EE68BFBF21BA49F300B7CD9A /* StringCalculate.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = StringCalculate.app; sourceTree = BUILT_PRODUCTS_DIR; }; 39 | EE68BFC221BA49F300B7CD9A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 40 | EE68BFC421BA49F300B7CD9A /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 41 | EE68BFC721BA49F300B7CD9A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 42 | EE68BFC921BA49F600B7CD9A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 43 | EE68BFCC21BA49F600B7CD9A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 44 | EE68BFCE21BA49F600B7CD9A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 45 | EE68BFD321BA49F600B7CD9A /* StringCalculateTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = StringCalculateTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 46 | EE68BFD721BA49F600B7CD9A /* StringCalculateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringCalculateTests.swift; sourceTree = ""; }; 47 | EE68BFD921BA49F600B7CD9A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 48 | EE68BFDE21BA49F600B7CD9A /* StringCalculateUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = StringCalculateUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 49 | EE68BFE221BA49F600B7CD9A /* StringCalculateUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringCalculateUITests.swift; sourceTree = ""; }; 50 | EE68BFE421BA49F600B7CD9A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 51 | EE68BFF021BA4A0F00B7CD9A /* StringCalculateManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringCalculateManager.swift; sourceTree = ""; }; 52 | EE68BFF421BA610300B7CD9A /* bridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bridge.h; sourceTree = ""; }; 53 | /* End PBXFileReference section */ 54 | 55 | /* Begin PBXFrameworksBuildPhase section */ 56 | EE68BFBC21BA49F300B7CD9A /* Frameworks */ = { 57 | isa = PBXFrameworksBuildPhase; 58 | buildActionMask = 2147483647; 59 | files = ( 60 | ); 61 | runOnlyForDeploymentPostprocessing = 0; 62 | }; 63 | EE68BFD021BA49F600B7CD9A /* Frameworks */ = { 64 | isa = PBXFrameworksBuildPhase; 65 | buildActionMask = 2147483647; 66 | files = ( 67 | ); 68 | runOnlyForDeploymentPostprocessing = 0; 69 | }; 70 | EE68BFDB21BA49F600B7CD9A /* Frameworks */ = { 71 | isa = PBXFrameworksBuildPhase; 72 | buildActionMask = 2147483647; 73 | files = ( 74 | ); 75 | runOnlyForDeploymentPostprocessing = 0; 76 | }; 77 | /* End PBXFrameworksBuildPhase section */ 78 | 79 | /* Begin PBXGroup section */ 80 | EE68BFB621BA49F300B7CD9A = { 81 | isa = PBXGroup; 82 | children = ( 83 | EE68BFC121BA49F300B7CD9A /* StringCalculate */, 84 | EE68BFD621BA49F600B7CD9A /* StringCalculateTests */, 85 | EE68BFE121BA49F600B7CD9A /* StringCalculateUITests */, 86 | EE68BFC021BA49F300B7CD9A /* Products */, 87 | ); 88 | sourceTree = ""; 89 | }; 90 | EE68BFC021BA49F300B7CD9A /* Products */ = { 91 | isa = PBXGroup; 92 | children = ( 93 | EE68BFBF21BA49F300B7CD9A /* StringCalculate.app */, 94 | EE68BFD321BA49F600B7CD9A /* StringCalculateTests.xctest */, 95 | EE68BFDE21BA49F600B7CD9A /* StringCalculateUITests.xctest */, 96 | ); 97 | name = Products; 98 | sourceTree = ""; 99 | }; 100 | EE68BFC121BA49F300B7CD9A /* StringCalculate */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | EE68BFC221BA49F300B7CD9A /* AppDelegate.swift */, 104 | EE68BFC421BA49F300B7CD9A /* ViewController.swift */, 105 | EE68BFC621BA49F300B7CD9A /* Main.storyboard */, 106 | EE68BFC921BA49F600B7CD9A /* Assets.xcassets */, 107 | EE68BFCB21BA49F600B7CD9A /* LaunchScreen.storyboard */, 108 | EE68BFCE21BA49F600B7CD9A /* Info.plist */, 109 | EE68BFF021BA4A0F00B7CD9A /* StringCalculateManager.swift */, 110 | EE68BFF421BA610300B7CD9A /* bridge.h */, 111 | ); 112 | path = StringCalculate; 113 | sourceTree = ""; 114 | }; 115 | EE68BFD621BA49F600B7CD9A /* StringCalculateTests */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | EE68BFD721BA49F600B7CD9A /* StringCalculateTests.swift */, 119 | EE68BFD921BA49F600B7CD9A /* Info.plist */, 120 | ); 121 | path = StringCalculateTests; 122 | sourceTree = ""; 123 | }; 124 | EE68BFE121BA49F600B7CD9A /* StringCalculateUITests */ = { 125 | isa = PBXGroup; 126 | children = ( 127 | EE68BFE221BA49F600B7CD9A /* StringCalculateUITests.swift */, 128 | EE68BFE421BA49F600B7CD9A /* Info.plist */, 129 | ); 130 | path = StringCalculateUITests; 131 | sourceTree = ""; 132 | }; 133 | /* End PBXGroup section */ 134 | 135 | /* Begin PBXNativeTarget section */ 136 | EE68BFBE21BA49F300B7CD9A /* StringCalculate */ = { 137 | isa = PBXNativeTarget; 138 | buildConfigurationList = EE68BFE721BA49F600B7CD9A /* Build configuration list for PBXNativeTarget "StringCalculate" */; 139 | buildPhases = ( 140 | EE68BFBB21BA49F300B7CD9A /* Sources */, 141 | EE68BFBC21BA49F300B7CD9A /* Frameworks */, 142 | EE68BFBD21BA49F300B7CD9A /* Resources */, 143 | ); 144 | buildRules = ( 145 | ); 146 | dependencies = ( 147 | ); 148 | name = StringCalculate; 149 | productName = StringCalculate; 150 | productReference = EE68BFBF21BA49F300B7CD9A /* StringCalculate.app */; 151 | productType = "com.apple.product-type.application"; 152 | }; 153 | EE68BFD221BA49F600B7CD9A /* StringCalculateTests */ = { 154 | isa = PBXNativeTarget; 155 | buildConfigurationList = EE68BFEA21BA49F600B7CD9A /* Build configuration list for PBXNativeTarget "StringCalculateTests" */; 156 | buildPhases = ( 157 | EE68BFCF21BA49F600B7CD9A /* Sources */, 158 | EE68BFD021BA49F600B7CD9A /* Frameworks */, 159 | EE68BFD121BA49F600B7CD9A /* Resources */, 160 | ); 161 | buildRules = ( 162 | ); 163 | dependencies = ( 164 | EE68BFD521BA49F600B7CD9A /* PBXTargetDependency */, 165 | ); 166 | name = StringCalculateTests; 167 | productName = StringCalculateTests; 168 | productReference = EE68BFD321BA49F600B7CD9A /* StringCalculateTests.xctest */; 169 | productType = "com.apple.product-type.bundle.unit-test"; 170 | }; 171 | EE68BFDD21BA49F600B7CD9A /* StringCalculateUITests */ = { 172 | isa = PBXNativeTarget; 173 | buildConfigurationList = EE68BFED21BA49F600B7CD9A /* Build configuration list for PBXNativeTarget "StringCalculateUITests" */; 174 | buildPhases = ( 175 | EE68BFDA21BA49F600B7CD9A /* Sources */, 176 | EE68BFDB21BA49F600B7CD9A /* Frameworks */, 177 | EE68BFDC21BA49F600B7CD9A /* Resources */, 178 | ); 179 | buildRules = ( 180 | ); 181 | dependencies = ( 182 | EE68BFE021BA49F600B7CD9A /* PBXTargetDependency */, 183 | ); 184 | name = StringCalculateUITests; 185 | productName = StringCalculateUITests; 186 | productReference = EE68BFDE21BA49F600B7CD9A /* StringCalculateUITests.xctest */; 187 | productType = "com.apple.product-type.bundle.ui-testing"; 188 | }; 189 | /* End PBXNativeTarget section */ 190 | 191 | /* Begin PBXProject section */ 192 | EE68BFB721BA49F300B7CD9A /* Project object */ = { 193 | isa = PBXProject; 194 | attributes = { 195 | LastSwiftUpdateCheck = 1010; 196 | LastUpgradeCheck = 1010; 197 | ORGANIZATIONNAME = "瑞文戴尔"; 198 | TargetAttributes = { 199 | EE68BFBE21BA49F300B7CD9A = { 200 | CreatedOnToolsVersion = 10.1; 201 | }; 202 | EE68BFD221BA49F600B7CD9A = { 203 | CreatedOnToolsVersion = 10.1; 204 | TestTargetID = EE68BFBE21BA49F300B7CD9A; 205 | }; 206 | EE68BFDD21BA49F600B7CD9A = { 207 | CreatedOnToolsVersion = 10.1; 208 | TestTargetID = EE68BFBE21BA49F300B7CD9A; 209 | }; 210 | }; 211 | }; 212 | buildConfigurationList = EE68BFBA21BA49F300B7CD9A /* Build configuration list for PBXProject "StringCalculate" */; 213 | compatibilityVersion = "Xcode 9.3"; 214 | developmentRegion = en; 215 | hasScannedForEncodings = 0; 216 | knownRegions = ( 217 | en, 218 | Base, 219 | ); 220 | mainGroup = EE68BFB621BA49F300B7CD9A; 221 | productRefGroup = EE68BFC021BA49F300B7CD9A /* Products */; 222 | projectDirPath = ""; 223 | projectRoot = ""; 224 | targets = ( 225 | EE68BFBE21BA49F300B7CD9A /* StringCalculate */, 226 | EE68BFD221BA49F600B7CD9A /* StringCalculateTests */, 227 | EE68BFDD21BA49F600B7CD9A /* StringCalculateUITests */, 228 | ); 229 | }; 230 | /* End PBXProject section */ 231 | 232 | /* Begin PBXResourcesBuildPhase section */ 233 | EE68BFBD21BA49F300B7CD9A /* Resources */ = { 234 | isa = PBXResourcesBuildPhase; 235 | buildActionMask = 2147483647; 236 | files = ( 237 | EE68BFCD21BA49F600B7CD9A /* LaunchScreen.storyboard in Resources */, 238 | EE68BFCA21BA49F600B7CD9A /* Assets.xcassets in Resources */, 239 | EE68BFC821BA49F300B7CD9A /* Main.storyboard in Resources */, 240 | ); 241 | runOnlyForDeploymentPostprocessing = 0; 242 | }; 243 | EE68BFD121BA49F600B7CD9A /* Resources */ = { 244 | isa = PBXResourcesBuildPhase; 245 | buildActionMask = 2147483647; 246 | files = ( 247 | ); 248 | runOnlyForDeploymentPostprocessing = 0; 249 | }; 250 | EE68BFDC21BA49F600B7CD9A /* Resources */ = { 251 | isa = PBXResourcesBuildPhase; 252 | buildActionMask = 2147483647; 253 | files = ( 254 | ); 255 | runOnlyForDeploymentPostprocessing = 0; 256 | }; 257 | /* End PBXResourcesBuildPhase section */ 258 | 259 | /* Begin PBXSourcesBuildPhase section */ 260 | EE68BFBB21BA49F300B7CD9A /* Sources */ = { 261 | isa = PBXSourcesBuildPhase; 262 | buildActionMask = 2147483647; 263 | files = ( 264 | EE68BFF121BA4A0F00B7CD9A /* StringCalculateManager.swift in Sources */, 265 | EE68BFC521BA49F300B7CD9A /* ViewController.swift in Sources */, 266 | EE68BFC321BA49F300B7CD9A /* AppDelegate.swift in Sources */, 267 | ); 268 | runOnlyForDeploymentPostprocessing = 0; 269 | }; 270 | EE68BFCF21BA49F600B7CD9A /* Sources */ = { 271 | isa = PBXSourcesBuildPhase; 272 | buildActionMask = 2147483647; 273 | files = ( 274 | EE68BFD821BA49F600B7CD9A /* StringCalculateTests.swift in Sources */, 275 | ); 276 | runOnlyForDeploymentPostprocessing = 0; 277 | }; 278 | EE68BFDA21BA49F600B7CD9A /* Sources */ = { 279 | isa = PBXSourcesBuildPhase; 280 | buildActionMask = 2147483647; 281 | files = ( 282 | EE68BFE321BA49F600B7CD9A /* StringCalculateUITests.swift in Sources */, 283 | ); 284 | runOnlyForDeploymentPostprocessing = 0; 285 | }; 286 | /* End PBXSourcesBuildPhase section */ 287 | 288 | /* Begin PBXTargetDependency section */ 289 | EE68BFD521BA49F600B7CD9A /* PBXTargetDependency */ = { 290 | isa = PBXTargetDependency; 291 | target = EE68BFBE21BA49F300B7CD9A /* StringCalculate */; 292 | targetProxy = EE68BFD421BA49F600B7CD9A /* PBXContainerItemProxy */; 293 | }; 294 | EE68BFE021BA49F600B7CD9A /* PBXTargetDependency */ = { 295 | isa = PBXTargetDependency; 296 | target = EE68BFBE21BA49F300B7CD9A /* StringCalculate */; 297 | targetProxy = EE68BFDF21BA49F600B7CD9A /* PBXContainerItemProxy */; 298 | }; 299 | /* End PBXTargetDependency section */ 300 | 301 | /* Begin PBXVariantGroup section */ 302 | EE68BFC621BA49F300B7CD9A /* Main.storyboard */ = { 303 | isa = PBXVariantGroup; 304 | children = ( 305 | EE68BFC721BA49F300B7CD9A /* Base */, 306 | ); 307 | name = Main.storyboard; 308 | sourceTree = ""; 309 | }; 310 | EE68BFCB21BA49F600B7CD9A /* LaunchScreen.storyboard */ = { 311 | isa = PBXVariantGroup; 312 | children = ( 313 | EE68BFCC21BA49F600B7CD9A /* Base */, 314 | ); 315 | name = LaunchScreen.storyboard; 316 | sourceTree = ""; 317 | }; 318 | /* End PBXVariantGroup section */ 319 | 320 | /* Begin XCBuildConfiguration section */ 321 | EE68BFE521BA49F600B7CD9A /* Debug */ = { 322 | isa = XCBuildConfiguration; 323 | buildSettings = { 324 | ALWAYS_SEARCH_USER_PATHS = NO; 325 | CLANG_ANALYZER_NONNULL = YES; 326 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 327 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 328 | CLANG_CXX_LIBRARY = "libc++"; 329 | CLANG_ENABLE_MODULES = YES; 330 | CLANG_ENABLE_OBJC_ARC = YES; 331 | CLANG_ENABLE_OBJC_WEAK = YES; 332 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 333 | CLANG_WARN_BOOL_CONVERSION = YES; 334 | CLANG_WARN_COMMA = YES; 335 | CLANG_WARN_CONSTANT_CONVERSION = YES; 336 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 337 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 338 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 339 | CLANG_WARN_EMPTY_BODY = YES; 340 | CLANG_WARN_ENUM_CONVERSION = YES; 341 | CLANG_WARN_INFINITE_RECURSION = YES; 342 | CLANG_WARN_INT_CONVERSION = YES; 343 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 344 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 345 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 346 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 347 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 348 | CLANG_WARN_STRICT_PROTOTYPES = YES; 349 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 350 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 351 | CLANG_WARN_UNREACHABLE_CODE = YES; 352 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 353 | CODE_SIGN_IDENTITY = "iPhone Developer"; 354 | COPY_PHASE_STRIP = NO; 355 | DEBUG_INFORMATION_FORMAT = dwarf; 356 | ENABLE_STRICT_OBJC_MSGSEND = YES; 357 | ENABLE_TESTABILITY = YES; 358 | GCC_C_LANGUAGE_STANDARD = gnu11; 359 | GCC_DYNAMIC_NO_PIC = NO; 360 | GCC_NO_COMMON_BLOCKS = YES; 361 | GCC_OPTIMIZATION_LEVEL = 0; 362 | GCC_PREPROCESSOR_DEFINITIONS = ( 363 | "DEBUG=1", 364 | "$(inherited)", 365 | ); 366 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 367 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 368 | GCC_WARN_UNDECLARED_SELECTOR = YES; 369 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 370 | GCC_WARN_UNUSED_FUNCTION = YES; 371 | GCC_WARN_UNUSED_VARIABLE = YES; 372 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 373 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 374 | MTL_FAST_MATH = YES; 375 | ONLY_ACTIVE_ARCH = YES; 376 | SDKROOT = iphoneos; 377 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 378 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 379 | }; 380 | name = Debug; 381 | }; 382 | EE68BFE621BA49F600B7CD9A /* Release */ = { 383 | isa = XCBuildConfiguration; 384 | buildSettings = { 385 | ALWAYS_SEARCH_USER_PATHS = NO; 386 | CLANG_ANALYZER_NONNULL = YES; 387 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 388 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 389 | CLANG_CXX_LIBRARY = "libc++"; 390 | CLANG_ENABLE_MODULES = YES; 391 | CLANG_ENABLE_OBJC_ARC = YES; 392 | CLANG_ENABLE_OBJC_WEAK = YES; 393 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 394 | CLANG_WARN_BOOL_CONVERSION = YES; 395 | CLANG_WARN_COMMA = YES; 396 | CLANG_WARN_CONSTANT_CONVERSION = YES; 397 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 398 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 399 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 400 | CLANG_WARN_EMPTY_BODY = YES; 401 | CLANG_WARN_ENUM_CONVERSION = YES; 402 | CLANG_WARN_INFINITE_RECURSION = YES; 403 | CLANG_WARN_INT_CONVERSION = YES; 404 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 405 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 406 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 407 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 408 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 409 | CLANG_WARN_STRICT_PROTOTYPES = YES; 410 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 411 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 412 | CLANG_WARN_UNREACHABLE_CODE = YES; 413 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 414 | CODE_SIGN_IDENTITY = "iPhone Developer"; 415 | COPY_PHASE_STRIP = NO; 416 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 417 | ENABLE_NS_ASSERTIONS = NO; 418 | ENABLE_STRICT_OBJC_MSGSEND = YES; 419 | GCC_C_LANGUAGE_STANDARD = gnu11; 420 | GCC_NO_COMMON_BLOCKS = YES; 421 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 422 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 423 | GCC_WARN_UNDECLARED_SELECTOR = YES; 424 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 425 | GCC_WARN_UNUSED_FUNCTION = YES; 426 | GCC_WARN_UNUSED_VARIABLE = YES; 427 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 428 | MTL_ENABLE_DEBUG_INFO = NO; 429 | MTL_FAST_MATH = YES; 430 | SDKROOT = iphoneos; 431 | SWIFT_COMPILATION_MODE = wholemodule; 432 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 433 | VALIDATE_PRODUCT = YES; 434 | }; 435 | name = Release; 436 | }; 437 | EE68BFE821BA49F600B7CD9A /* Debug */ = { 438 | isa = XCBuildConfiguration; 439 | buildSettings = { 440 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 441 | CODE_SIGN_STYLE = Automatic; 442 | DEVELOPMENT_TEAM = FJ2RR7PUE2; 443 | INFOPLIST_FILE = StringCalculate/Info.plist; 444 | LD_RUNPATH_SEARCH_PATHS = ( 445 | "$(inherited)", 446 | "@executable_path/Frameworks", 447 | ); 448 | PRODUCT_BUNDLE_IDENTIFIER = com.ruiwendell.StringCalculate; 449 | PRODUCT_NAME = "$(TARGET_NAME)"; 450 | SWIFT_OBJC_BRIDGING_HEADER = StringCalculate/bridge.h; 451 | SWIFT_VERSION = 4.2; 452 | TARGETED_DEVICE_FAMILY = "1,2"; 453 | }; 454 | name = Debug; 455 | }; 456 | EE68BFE921BA49F600B7CD9A /* Release */ = { 457 | isa = XCBuildConfiguration; 458 | buildSettings = { 459 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 460 | CODE_SIGN_STYLE = Automatic; 461 | DEVELOPMENT_TEAM = FJ2RR7PUE2; 462 | INFOPLIST_FILE = StringCalculate/Info.plist; 463 | LD_RUNPATH_SEARCH_PATHS = ( 464 | "$(inherited)", 465 | "@executable_path/Frameworks", 466 | ); 467 | PRODUCT_BUNDLE_IDENTIFIER = com.ruiwendell.StringCalculate; 468 | PRODUCT_NAME = "$(TARGET_NAME)"; 469 | SWIFT_OBJC_BRIDGING_HEADER = StringCalculate/bridge.h; 470 | SWIFT_VERSION = 4.2; 471 | TARGETED_DEVICE_FAMILY = "1,2"; 472 | }; 473 | name = Release; 474 | }; 475 | EE68BFEB21BA49F600B7CD9A /* Debug */ = { 476 | isa = XCBuildConfiguration; 477 | buildSettings = { 478 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 479 | BUNDLE_LOADER = "$(TEST_HOST)"; 480 | CODE_SIGN_STYLE = Automatic; 481 | DEVELOPMENT_TEAM = FJ2RR7PUE2; 482 | INFOPLIST_FILE = StringCalculateTests/Info.plist; 483 | LD_RUNPATH_SEARCH_PATHS = ( 484 | "$(inherited)", 485 | "@executable_path/Frameworks", 486 | "@loader_path/Frameworks", 487 | ); 488 | PRODUCT_BUNDLE_IDENTIFIER = com.ruiwendell.StringCalculateTests; 489 | PRODUCT_NAME = "$(TARGET_NAME)"; 490 | SWIFT_VERSION = 4.2; 491 | TARGETED_DEVICE_FAMILY = "1,2"; 492 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/StringCalculate.app/StringCalculate"; 493 | }; 494 | name = Debug; 495 | }; 496 | EE68BFEC21BA49F600B7CD9A /* Release */ = { 497 | isa = XCBuildConfiguration; 498 | buildSettings = { 499 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 500 | BUNDLE_LOADER = "$(TEST_HOST)"; 501 | CODE_SIGN_STYLE = Automatic; 502 | DEVELOPMENT_TEAM = FJ2RR7PUE2; 503 | INFOPLIST_FILE = StringCalculateTests/Info.plist; 504 | LD_RUNPATH_SEARCH_PATHS = ( 505 | "$(inherited)", 506 | "@executable_path/Frameworks", 507 | "@loader_path/Frameworks", 508 | ); 509 | PRODUCT_BUNDLE_IDENTIFIER = com.ruiwendell.StringCalculateTests; 510 | PRODUCT_NAME = "$(TARGET_NAME)"; 511 | SWIFT_VERSION = 4.2; 512 | TARGETED_DEVICE_FAMILY = "1,2"; 513 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/StringCalculate.app/StringCalculate"; 514 | }; 515 | name = Release; 516 | }; 517 | EE68BFEE21BA49F600B7CD9A /* Debug */ = { 518 | isa = XCBuildConfiguration; 519 | buildSettings = { 520 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 521 | CODE_SIGN_STYLE = Automatic; 522 | DEVELOPMENT_TEAM = FJ2RR7PUE2; 523 | INFOPLIST_FILE = StringCalculateUITests/Info.plist; 524 | LD_RUNPATH_SEARCH_PATHS = ( 525 | "$(inherited)", 526 | "@executable_path/Frameworks", 527 | "@loader_path/Frameworks", 528 | ); 529 | PRODUCT_BUNDLE_IDENTIFIER = com.ruiwendell.StringCalculateUITests; 530 | PRODUCT_NAME = "$(TARGET_NAME)"; 531 | SWIFT_VERSION = 4.2; 532 | TARGETED_DEVICE_FAMILY = "1,2"; 533 | TEST_TARGET_NAME = StringCalculate; 534 | }; 535 | name = Debug; 536 | }; 537 | EE68BFEF21BA49F600B7CD9A /* Release */ = { 538 | isa = XCBuildConfiguration; 539 | buildSettings = { 540 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 541 | CODE_SIGN_STYLE = Automatic; 542 | DEVELOPMENT_TEAM = FJ2RR7PUE2; 543 | INFOPLIST_FILE = StringCalculateUITests/Info.plist; 544 | LD_RUNPATH_SEARCH_PATHS = ( 545 | "$(inherited)", 546 | "@executable_path/Frameworks", 547 | "@loader_path/Frameworks", 548 | ); 549 | PRODUCT_BUNDLE_IDENTIFIER = com.ruiwendell.StringCalculateUITests; 550 | PRODUCT_NAME = "$(TARGET_NAME)"; 551 | SWIFT_VERSION = 4.2; 552 | TARGETED_DEVICE_FAMILY = "1,2"; 553 | TEST_TARGET_NAME = StringCalculate; 554 | }; 555 | name = Release; 556 | }; 557 | /* End XCBuildConfiguration section */ 558 | 559 | /* Begin XCConfigurationList section */ 560 | EE68BFBA21BA49F300B7CD9A /* Build configuration list for PBXProject "StringCalculate" */ = { 561 | isa = XCConfigurationList; 562 | buildConfigurations = ( 563 | EE68BFE521BA49F600B7CD9A /* Debug */, 564 | EE68BFE621BA49F600B7CD9A /* Release */, 565 | ); 566 | defaultConfigurationIsVisible = 0; 567 | defaultConfigurationName = Release; 568 | }; 569 | EE68BFE721BA49F600B7CD9A /* Build configuration list for PBXNativeTarget "StringCalculate" */ = { 570 | isa = XCConfigurationList; 571 | buildConfigurations = ( 572 | EE68BFE821BA49F600B7CD9A /* Debug */, 573 | EE68BFE921BA49F600B7CD9A /* Release */, 574 | ); 575 | defaultConfigurationIsVisible = 0; 576 | defaultConfigurationName = Release; 577 | }; 578 | EE68BFEA21BA49F600B7CD9A /* Build configuration list for PBXNativeTarget "StringCalculateTests" */ = { 579 | isa = XCConfigurationList; 580 | buildConfigurations = ( 581 | EE68BFEB21BA49F600B7CD9A /* Debug */, 582 | EE68BFEC21BA49F600B7CD9A /* Release */, 583 | ); 584 | defaultConfigurationIsVisible = 0; 585 | defaultConfigurationName = Release; 586 | }; 587 | EE68BFED21BA49F600B7CD9A /* Build configuration list for PBXNativeTarget "StringCalculateUITests" */ = { 588 | isa = XCConfigurationList; 589 | buildConfigurations = ( 590 | EE68BFEE21BA49F600B7CD9A /* Debug */, 591 | EE68BFEF21BA49F600B7CD9A /* Release */, 592 | ); 593 | defaultConfigurationIsVisible = 0; 594 | defaultConfigurationName = Release; 595 | }; 596 | /* End XCConfigurationList section */ 597 | }; 598 | rootObject = EE68BFB721BA49F300B7CD9A /* Project object */; 599 | } 600 | -------------------------------------------------------------------------------- /StringCalculate.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /StringCalculate.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /StringCalculate.xcodeproj/project.xcworkspace/xcuserdata/ruiwendaier.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NotFound9/StringCalculate/ed0a60145ec01c088747c5aabcc7460387384416/StringCalculate.xcodeproj/project.xcworkspace/xcuserdata/ruiwendaier.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /StringCalculate.xcodeproj/xcuserdata/ruiwendaier.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /StringCalculate.xcodeproj/xcuserdata/ruiwendaier.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | StringCalculate.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /StringCalculate/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // StringCalculate 4 | // 5 | // Created by ruiwendaier on 2018/12/7. 6 | // Copyright © 2018年 瑞文戴尔. 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 invalidate graphics rendering callbacks. 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 active 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 | -------------------------------------------------------------------------------- /StringCalculate/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /StringCalculate/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /StringCalculate/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /StringCalculate/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 | -------------------------------------------------------------------------------- /StringCalculate/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 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /StringCalculate/StringCalculateManager.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StringCalculateManager.swift 3 | // SohuAuto 4 | // 5 | // Created by ruiwendaier on 2018/12/5. 6 | // Copyright © 2018年 auto. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | class StringCalculateManager { 13 | static let shared = StringCalculateManager() 14 | //fontDictionary是一个Dictionary,例如{".SFUIText-Semibold-16.0": {"0":10.3203125, "Z":10.4140625, "中":16.32, "singleLineHeight":19.09375}}, 15 | //fontDictionary的key是以字体的名字和大小拼接的String,例如".SFUIText-Semibold-16.0" 16 | //fontDictionary的value是一个Dictionary,存储对应字体的各种字符对应的宽度及字体的单行高度,例如{"0":10.3203125, "Z":10.4140625, "中":16.32, "singleLineHeight":19.09375} 17 | var fontDictionary = [String: [String: CGFloat]]() 18 | var numsNeedToSave = 0//更新的数据的条数 19 | var fileUrl: URL = {//fontDictionary在磁盘中的存储路径 20 | let manager = FileManager.default 21 | var filePath = manager.urls(for: .documentDirectory, in: .userDomainMask).first 22 | filePath!.appendPathComponent("font_dictionary.json") 23 | print("font_dictionary.json的路径是===\(filePath!)") 24 | return filePath! 25 | }() 26 | 27 | init() { 28 | readFontDictionaryFromDisk() 29 | NotificationCenter.default.addObserver(self, selector: #selector(saveFontDictionaryToDisk), name: UIApplication.didEnterBackgroundNotification, object: nil) 30 | NotificationCenter.default.addObserver(self, selector: #selector(saveFontDictionaryToDisk), name: UIApplication.willTerminateNotification, object: nil) 31 | } 32 | deinit { 33 | NotificationCenter.default.removeObserver(self) 34 | } 35 | //第一次使用字体时预先计算该字体中各种字符的宽度 36 | func createNewFont(font: UIFont) -> [String: CGFloat] { 37 | let array: [String] = ["中", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "“", ";", "?", ",", "[", "]", "、", "【", "】", "?", "!", ":", "|"] 38 | var widthDictionary = [String: CGFloat]() 39 | var singleWordRect = CGRect.zero 40 | for string in array { 41 | singleWordRect = string.boundingRect(with: CGSize(width: 100, height: 100), 42 | options: .usesLineFragmentOrigin, 43 | attributes: [NSAttributedString.Key.font: font], 44 | context: nil) 45 | widthDictionary[string] = singleWordRect.size.width 46 | } 47 | widthDictionary["singleLineHeight"] = singleWordRect.size.height 48 | let fontKey = "\(font.fontName)-\(font.pointSize)" 49 | fontDictionary[fontKey] = widthDictionary 50 | numsNeedToSave = array.count//代表有更新,需要存入到磁盘 51 | saveFontDictionaryToDisk()//存入本地json 52 | return widthDictionary 53 | } 54 | //限定最大行数的场景下计算Label的bounds 55 | func calculateSize(withString string: String, maxWidth: CGFloat, font: UIFont, maxLine: Int) -> CGRect { 56 | let totalWidth: CGFloat = calculateTotalWidth(string: string, font: font) 57 | var widthDictionary = fetchWidthDictionaryWith(font) 58 | let singleLineHeight = widthDictionary["singleLineHeight"]! 59 | let numsOfLine = ceil(totalWidth/maxWidth)//行数 60 | let maxLineCGFloat = CGFloat(maxLine)//最大 61 | let resultwidth = numsOfLine <= 1 ? totalWidth : maxWidth//小于最大宽度时,取实际宽度的值 62 | let resultLine = numsOfLine < maxLineCGFloat ? numsOfLine : maxLineCGFloat 63 | return CGRect.init(x: 0, y: 0, width: resultwidth, height: resultLine * singleLineHeight) 64 | } 65 | 66 | //行数不限的场景下计算Label的bounds 67 | func calculateSize(withString string: String, maxWidth: CGFloat, font: UIFont) -> CGRect { 68 | let totalWidth: CGFloat = calculateTotalWidth(string: string, font: font) 69 | var widthDictionary = fetchWidthDictionaryWith(font) 70 | let singleLineHeight = widthDictionary["singleLineHeight"]! 71 | let numsOfLine = ceil(totalWidth/maxWidth)//行数 72 | let resultwidth = numsOfLine <= 1 ? totalWidth : maxWidth//小于最大宽度时,取实际宽度的值 73 | return CGRect.init(x: 0, y: 0, width: resultwidth, height: numsOfLine * singleLineHeight) 74 | } 75 | 76 | //限定最大高度的场景下计算Label的bounds 77 | func calculateSize(withString string: String, maxSize: CGSize, font: UIFont) -> CGRect { 78 | let totalWidth: CGFloat = calculateTotalWidth(string: string, font: font) 79 | var widthDictionary = fetchWidthDictionaryWith(font) 80 | let singleLineHeight = widthDictionary["singleLineHeight"]! 81 | let numsOfLine = ceil(totalWidth/maxSize.width)//行数 82 | let maxLineCGFloat = floor(maxSize.height/singleLineHeight) 83 | let resultwidth = numsOfLine <= 1 ? totalWidth : maxSize.width//小于最大宽度时,取实际宽度的值 84 | let resultLine = numsOfLine < maxLineCGFloat ? numsOfLine : maxLineCGFloat 85 | return CGRect.init(x: 0, y: 0, width: resultwidth, height: resultLine * singleLineHeight) 86 | } 87 | 88 | //计算排版在一行的总宽度 89 | func calculateTotalWidth(string: String, font: UIFont) -> CGFloat { 90 | var totalWidth: CGFloat = 0 91 | let fontKey = "\(font.fontName)-\(font.pointSize)" 92 | var widthDictionary = fetchWidthDictionaryWith(font) 93 | let chineseWidth = widthDictionary["中"]! 94 | for character in string { 95 | if "\u{4E00}" <= character && character <= "\u{9FA5}" {//中文 96 | totalWidth += chineseWidth 97 | } else if let width = widthDictionary[String(character)] {//数字,小写字母,大写字母,及常见符号 98 | totalWidth += width 99 | } else {//符号及其他没有预先计算好的字符,对它们进行计算并且缓存到宽度字典中去 100 | let tempString = String(character) 101 | let width = tempString.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), 102 | options: .usesLineFragmentOrigin, 103 | attributes: [NSAttributedString.Key.font: font], 104 | context: nil).size.width 105 | totalWidth += width 106 | widthDictionary[tempString] = width 107 | numsNeedToSave += 1 108 | } 109 | } 110 | fontDictionary[fontKey] = widthDictionary 111 | if numsNeedToSave > 10 { 112 | saveFontDictionaryToDisk() 113 | } 114 | return totalWidth 115 | } 116 | 117 | //获取字体对应的宽度字典 118 | func fetchWidthDictionaryWith(_ font: UIFont) -> [String: CGFloat] { 119 | var widthDictionary = [String: CGFloat]() 120 | let fontKey = "\(font.fontName)-\(font.pointSize)" 121 | if let dictionary = StringCalculateManager.shared.fontDictionary[fontKey] { 122 | widthDictionary = dictionary 123 | } else { 124 | widthDictionary = StringCalculateManager.shared.createNewFont(font: font) 125 | } 126 | return widthDictionary 127 | } 128 | 129 | let queue = DispatchQueue(label: "com.StringCalculateManager.queue") 130 | //存储fontDictionary到磁盘 131 | @objc func saveFontDictionaryToDisk() { 132 | guard numsNeedToSave > 0 else { 133 | return 134 | } 135 | numsNeedToSave = 0 136 | queue.async {//防止多线程同时写入造成冲突 137 | do { 138 | var data: Data? 139 | if #available(iOS 11.0, *) { 140 | data = try? JSONSerialization.data(withJSONObject: self.fontDictionary, options: .sortedKeys) 141 | } else { 142 | data = try? JSONSerialization.data(withJSONObject: self.fontDictionary, options: .prettyPrinted) 143 | } 144 | try data?.write(to: self.fileUrl) 145 | print("font_dictionary存入磁盘,font_dictionary=\(self.fontDictionary)") 146 | } catch { 147 | print("font_dictionary存储失败error=\(error)") 148 | } 149 | } 150 | } 151 | //从磁盘中读取缓存 152 | func readFontDictionaryFromDisk() { 153 | do { 154 | let data = try Data.init(contentsOf: fileUrl) 155 | let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) 156 | guard let dict = json as? [String: [String: CGFloat]] else { 157 | return 158 | } 159 | fontDictionary = dict 160 | print(fontDictionary) 161 | print("font_dictionarys读取成功,font_dictionarys=\(fontDictionary)") 162 | } catch { 163 | print("第一次运行时font_dictionary不存在或者读取失败") 164 | } 165 | } 166 | } 167 | 168 | extension String { 169 | //限制最大行数的场景下,计算Label的bounds 170 | func boundingRectFast(withMaxWidth width: CGFloat, font: UIFont, maxLine: Int) -> CGRect { 171 | let rect = StringCalculateManager.shared.calculateSize(withString: self, maxWidth: width, font: font, maxLine: maxLine) 172 | return rect 173 | } 174 | //行数不限的场景下,计算Label的bounds 175 | func boundingRectFast(withMaxWidth width: CGFloat, font: UIFont) -> CGRect { 176 | let rect = StringCalculateManager.shared.calculateSize(withString: self, maxWidth: width, font: font) 177 | return rect 178 | } 179 | 180 | //限定最大高度的场景下,计算Label的bounds 181 | func boundingRectFast(withMaxSize size: CGSize, font: UIFont) -> CGRect { 182 | let rect = StringCalculateManager.shared.calculateSize(withString: self, maxSize: size, font: font) 183 | return rect 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /StringCalculate/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // StringCalculate 4 | // 5 | // Created by ruiwendaier on 2018/12/7. 6 | // Copyright © 2018年 瑞文戴尔. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | 16 | let _ = StringCalculateManager.shared 17 | 18 | oldWay(value: 1) 19 | newWay(value: 1) 20 | 21 | oldWay(value: 10) 22 | newWay(value: 10) 23 | 24 | oldWay(value: 100) 25 | newWay(value: 100) 26 | 27 | oldWay(value: 1000) 28 | newWay(value: 1000) 29 | 30 | oldWay(value: 10000) 31 | newWay(value: 10000) 32 | 33 | oldWay(value: 100000) 34 | newWay(value: 100000) 35 | 36 | oldWay(value: 1000000) 37 | newWay(value: 1000000) 38 | 39 | print("测试完成") 40 | } 41 | 42 | func oldWay(value: Int) { 43 | let attribute = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16)] 44 | let constraintRect = CGSize.init(width: 100, height: 60) 45 | 46 | var title = "" 47 | var rect = CGRect.zero 48 | print("oldWay开始计算了") 49 | let startTime = CFAbsoluteTimeGetCurrent(); 50 | for i in 0...value { 51 | title = "iOS性能优化之计算多行Label高度的新方法\(i)" 52 | rect = title.boundingRect(with: constraintRect, 53 | options: .usesLineFragmentOrigin, 54 | attributes: attribute, 55 | context: nil) 56 | } 57 | let duration = (CFAbsoluteTimeGetCurrent() - startTime) * 1000.0; 58 | print("\(rect)") 59 | print("使用oldWay计算\(value)次,总耗时\(duration) ms") 60 | } 61 | 62 | func newWay(value: Int) { 63 | let constraintSize = CGSize.init(width: 100, height: 60) 64 | var title = "" 65 | var rect = CGRect.zero 66 | print("newWay开始计算了") 67 | let startTime = CFAbsoluteTimeGetCurrent(); 68 | for i in 0...value { 69 | title = "iOS性能优化之计算多行Label高度的新方法\(i)" 70 | rect = title.boundingRectFast(withMaxSize: constraintSize, font: UIFont.boldSystemFont(ofSize: 16)) 71 | } 72 | let duration = (CFAbsoluteTimeGetCurrent() - startTime) * 1000.0; 73 | print("\(rect)") 74 | print("使用newWay计算\(value)次,总耗时\(duration) ms") 75 | } 76 | 77 | func useWays() { 78 | let text = "iOS性能优化之计算多行Label高度的新方法,iOS性能优化之计算多行Label高度的新方法"; 79 | //限定最大行数场景下,计算Label高度 80 | let maxLine = 3 81 | //系统方法 82 | let singleLineHeight = 20 83 | let maxHeight = CGFloat(maxLine * singleLineHeight) 84 | let rect1 = text.boundingRect(with: CGSize(width: UIScreen.main.bounds.size.width, height: maxHeight), 85 | options: .usesLineFragmentOrigin, 86 | attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16)], 87 | context: nil).size.width 88 | 89 | let rect2 = text.boundingRectFast(withMaxWidth: UIScreen.main.bounds.size.width, font: UIFont.boldSystemFont(ofSize: 16), maxLine: 3) 90 | } 91 | } 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /StringCalculate/bridge.h: -------------------------------------------------------------------------------- 1 | // 2 | // bridge.h 3 | // StringCalculate 4 | // 5 | // Created by ruiwendaier on 2018/12/7. 6 | // Copyright © 2018年 瑞文戴尔. All rights reserved. 7 | // 8 | 9 | #ifndef bridge_h 10 | #define bridge_h 11 | 12 | #import 13 | 14 | #endif /* bridge_h */ 15 | -------------------------------------------------------------------------------- /StringCalculateTests/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 | -------------------------------------------------------------------------------- /StringCalculateTests/StringCalculateTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StringCalculateTests.swift 3 | // StringCalculateTests 4 | // 5 | // Created by ruiwendaier on 2018/12/7. 6 | // Copyright © 2018年 瑞文戴尔. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import StringCalculate 11 | 12 | class StringCalculateTests: XCTestCase { 13 | 14 | override func setUp() { 15 | // Put setup code here. This method is called before the invocation of each test method in the class. 16 | } 17 | 18 | override func tearDown() { 19 | // Put teardown code here. This method is called after the invocation of each test method in the class. 20 | } 21 | 22 | func testExample() { 23 | // This is an example of a functional test case. 24 | // Use XCTAssert and related functions to verify your tests produce the correct results. 25 | } 26 | 27 | func testPerformanceExample() { 28 | // This is an example of a performance test case. 29 | self.measure { 30 | // Put the code you want to measure the time of here. 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /StringCalculateUITests/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 | -------------------------------------------------------------------------------- /StringCalculateUITests/StringCalculateUITests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StringCalculateUITests.swift 3 | // StringCalculateUITests 4 | // 5 | // Created by ruiwendaier on 2018/12/7. 6 | // Copyright © 2018年 瑞文戴尔. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | 11 | class StringCalculateUITests: XCTestCase { 12 | 13 | override func setUp() { 14 | // Put setup code here. This method is called before the invocation of each test method in the class. 15 | 16 | // In UI tests it is usually best to stop immediately when a failure occurs. 17 | continueAfterFailure = false 18 | 19 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 20 | XCUIApplication().launch() 21 | 22 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 23 | } 24 | 25 | override func tearDown() { 26 | // Put teardown code here. This method is called after the invocation of each test method in the class. 27 | } 28 | 29 | func testExample() { 30 | // Use recording to get started writing UI tests. 31 | // Use XCTAssert and related functions to verify your tests produce the correct results. 32 | } 33 | 34 | } 35 | --------------------------------------------------------------------------------