From 70e2abef6d8ec12740ff71cb06ac540ede2e53d9 Mon Sep 17 00:00:00 2001 From: tobias <thinkdifferent055@gmail.com> Date: Sun, 4 Apr 2021 21:21:46 +0200 Subject: [PATCH] Add string size extension --- AppleLibs.xcodeproj/project.pbxproj | 20 ++++++++++++---- .../String+Html.swift} | 0 AppleLibs/String/String+Size.swift | 24 +++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) rename AppleLibs/{String+Extended.swift => String/String+Html.swift} (100%) create mode 100644 AppleLibs/String/String+Size.swift diff --git a/AppleLibs.xcodeproj/project.pbxproj b/AppleLibs.xcodeproj/project.pbxproj index 9bfc3b5..eb9467c 100644 --- a/AppleLibs.xcodeproj/project.pbxproj +++ b/AppleLibs.xcodeproj/project.pbxproj @@ -7,12 +7,13 @@ objects = { /* Begin PBXBuildFile section */ + F62B93A7261A492700D7F8E6 /* String+Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = F62B93A6261A492700D7F8E6 /* String+Size.swift */; }; F68C2D422616482A00042967 /* IsoDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F68C2D412616482A00042967 /* IsoDateFormatter.swift */; }; F6A251A0260B670000132DEC /* AppleLibs.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F6A25196260B66FF00132DEC /* AppleLibs.framework */; }; F6A251A5260B670000132DEC /* AppleLibsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6A251A4260B670000132DEC /* AppleLibsTests.swift */; }; F6A251A7260B670000132DEC /* AppleLibs.h in Headers */ = {isa = PBXBuildFile; fileRef = F6A25199260B66FF00132DEC /* AppleLibs.h */; settings = {ATTRIBUTES = (Public, ); }; }; F6A251BA260B697300132DEC /* Double+Rounded.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6A251B9260B697300132DEC /* Double+Rounded.swift */; }; - F6A251BE260B699100132DEC /* String+Extended.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6A251BD260B699100132DEC /* String+Extended.swift */; }; + F6A251BE260B699100132DEC /* String+Html.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6A251BD260B699100132DEC /* String+Html.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -26,6 +27,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + F62B93A6261A492700D7F8E6 /* String+Size.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Size.swift"; sourceTree = "<group>"; }; F68C2D412616482A00042967 /* IsoDateFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IsoDateFormatter.swift; sourceTree = "<group>"; }; F6A25196260B66FF00132DEC /* AppleLibs.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AppleLibs.framework; sourceTree = BUILT_PRODUCTS_DIR; }; F6A25199260B66FF00132DEC /* AppleLibs.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppleLibs.h; sourceTree = "<group>"; }; @@ -35,7 +37,7 @@ F6A251A6260B670000132DEC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; F6A251B2260B680F00132DEC /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; }; F6A251B9260B697300132DEC /* Double+Rounded.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Rounded.swift"; sourceTree = "<group>"; }; - F6A251BD260B699100132DEC /* String+Extended.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extended.swift"; sourceTree = "<group>"; }; + F6A251BD260B699100132DEC /* String+Html.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Html.swift"; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -57,6 +59,15 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + F62B93A5261A491100D7F8E6 /* String */ = { + isa = PBXGroup; + children = ( + F6A251BD260B699100132DEC /* String+Html.swift */, + F62B93A6261A492700D7F8E6 /* String+Size.swift */, + ); + path = String; + sourceTree = "<group>"; + }; F68C2D402616482000042967 /* Date */ = { isa = PBXGroup; children = ( @@ -87,9 +98,9 @@ F6A25198260B66FF00132DEC /* AppleLibs */ = { isa = PBXGroup; children = ( + F62B93A5261A491100D7F8E6 /* String */, F68C2D402616482000042967 /* Date */, F6A251B8260B696200132DEC /* Numbers */, - F6A251BD260B699100132DEC /* String+Extended.swift */, F6A25199260B66FF00132DEC /* AppleLibs.h */, F6A2519A260B66FF00132DEC /* Info.plist */, ); @@ -223,8 +234,9 @@ buildActionMask = 2147483647; files = ( F6A251BA260B697300132DEC /* Double+Rounded.swift in Sources */, + F62B93A7261A492700D7F8E6 /* String+Size.swift in Sources */, F68C2D422616482A00042967 /* IsoDateFormatter.swift in Sources */, - F6A251BE260B699100132DEC /* String+Extended.swift in Sources */, + F6A251BE260B699100132DEC /* String+Html.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/AppleLibs/String+Extended.swift b/AppleLibs/String/String+Html.swift similarity index 100% rename from AppleLibs/String+Extended.swift rename to AppleLibs/String/String+Html.swift diff --git a/AppleLibs/String/String+Size.swift b/AppleLibs/String/String+Size.swift new file mode 100644 index 0000000..9bd11f6 --- /dev/null +++ b/AppleLibs/String/String+Size.swift @@ -0,0 +1,24 @@ +// +// String+Size.swift +// AppleLibs +// +// Created by Tobias on 04.04.21. +// + +import UIKit + +public extension String { + func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat { + let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude) + let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil) + + return ceil(boundingBox.height) + } + + func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat { + let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height) + let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil) + + return ceil(boundingBox.width) + } +} -- GitLab