This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'deploy/1.3.0' into productive
- Loading branch information
Showing
7 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// StringExtension.swift | ||
// HandyUIKit | ||
// | ||
// Created by Cihat Gündüz on 19.02.17. | ||
// Copyright © 2017 Flinesoft. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension String { | ||
/// Calculates and returns the height needed to fit the text into a width-constrained rect. | ||
/// | ||
/// - Parameters: | ||
/// - fixedWidth: The fixed width of the rect. | ||
/// - font: The font of the text to calculate for. | ||
/// - Returns: The height needed to fit the text into a width-constrained rect. | ||
public func height(forFixedWidth fixedWidth: CGFloat, font: UIFont) -> CGFloat { | ||
let constraintSize = CGSize(width: fixedWidth, height: .greatestFiniteMagnitude) | ||
return rect(for: constraintSize, font: font).height | ||
} | ||
|
||
/// Calculates and returns the width needed to fit the text into a height-constrained rect. | ||
/// | ||
/// - Parameters: | ||
/// - fixedHeight: The fixed height of the rect. | ||
/// - font: The font of the text to calculate for. | ||
/// - Returns: The width needed to fit the text into a height-constrained rect. | ||
public func width(forFixedHeight fixedHeight: CGFloat, font: UIFont) -> CGFloat { | ||
let constraintSize = CGSize(width: .greatestFiniteMagnitude, height: fixedHeight) | ||
return rect(for: constraintSize, font: font).width | ||
} | ||
|
||
private func rect(for constraintSize: CGSize, font: UIFont) -> CGRect { | ||
let attributes = [NSFontAttributeName: font] | ||
return (self as NSString).boundingRect(with: constraintSize, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// StringExtensionTests.swift | ||
// HandyUIKit | ||
// | ||
// Created by Cihat Gündüz on 19.02.17. | ||
// Copyright © 2017 Flinesoft. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import HandyUIKit | ||
|
||
class StringExtensionTests: XCTestCase { | ||
let loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." // swiftlint:disable:this line_length | ||
|
||
func testHeight() { | ||
let font = UIFont(name: "Helvetica Neue", size: 16)! | ||
let calculatedHeight = loremIpsum.height(forFixedWidth: 300, font: font) | ||
|
||
XCTAssertEqualWithAccuracy(calculatedHeight, 205.04, accuracy: 0.001) | ||
} | ||
|
||
func testWidth() { | ||
let font = UIFont(name: "Helvetica Neue", size: 16)! | ||
let calculatedHeight = loremIpsum.width(forFixedHeight: 21, font: font) | ||
|
||
XCTAssertEqualWithAccuracy(calculatedHeight, 3014.592, accuracy: 0.001) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters