-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* extension NSString for macOS * UIKit + SwiftyAttributes * tests for public var swiftyLargeTitleTextAttributes * fixed build error on tvOS * fixed for tvOS again * moved NSString+SwiftyAttributes * fixed macOS build error * less functions * removed unused typealiases. less #if os. added documentation for new properties. * add functions again * typealiases * added NSString extension to project * Update NSString+SwiftyAttributes.swift * tests * Update NSString+macOS.swift * removed functions without tests * swifty_
- Loading branch information
1 parent
ee0f062
commit f599af5
Showing
5 changed files
with
158 additions
and
5 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
47 changes: 47 additions & 0 deletions
47
SwiftyAttributes/Sources/common/NSString+SwiftyAttributes.swift
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,47 @@ | ||
// | ||
// NSString+SwiftyAttributes.swift | ||
// SwiftyAttributes | ||
// | ||
// Created by Roman Podymov on 26/05/19. | ||
// Copyright © 2019 Roman Podymov. All rights reserved. | ||
// | ||
|
||
#if os(macOS) | ||
import AppKit | ||
public typealias Point = NSPoint | ||
public typealias Size = NSSize | ||
public typealias Rect = NSRect | ||
public typealias DrawingOptions = NSString.DrawingOptions | ||
#else | ||
import UIKit | ||
public typealias Point = CGPoint | ||
public typealias Size = CGSize | ||
public typealias Rect = CGRect | ||
public typealias DrawingOptions = NSStringDrawingOptions | ||
#endif | ||
|
||
public typealias DrawingContext = NSStringDrawingContext | ||
|
||
extension NSString { | ||
|
||
/** | ||
Get string size with the specified attributes. | ||
|
||
- parameter attrs: The attributes to use. | ||
*/ | ||
public func swifty_size(withSwiftyAttributes attrs: [Attribute]? = nil) -> Size { | ||
return size(withAttributes: attrs?.foundationAttributes) | ||
} | ||
|
||
/** | ||
Get string bounding rectangle with the specified attributes. | ||
|
||
- parameter size: Size of bounding rectangle. | ||
- parameter options: Aditional options. | ||
- parameter swiftyAttributes: The attributes to use. | ||
- parameter context: Drawing context. | ||
*/ | ||
public func swifty_boundingRect(with size: Size, options: DrawingOptions = [], swiftyAttributes: [Attribute]? = nil, context: DrawingContext?) -> Rect { | ||
return boundingRect(with: size, options: options, attributes: swiftyAttributes?.foundationAttributes, context: context) | ||
} | ||
} |
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,26 @@ | ||
// | ||
// NSString+macOS.swift | ||
// SwiftyAttributes | ||
// | ||
// Created by Roman Podymov on 02/06/2019. | ||
// Copyright © 2019 Roman Podymov. All rights reserved. | ||
// | ||
|
||
#if os(macOS) | ||
import AppKit | ||
|
||
extension NSString { | ||
|
||
/** | ||
Get string bounding rectangle with the specified attributes. | ||
|
||
- parameter size: Size of bounding rectangle. | ||
- parameter options: Aditional options. | ||
- parameter swiftyAttributes: The attributes to use. | ||
*/ | ||
@available(macOS, deprecated: 10.12) | ||
public func swifty_boundingRect(with size: Size, options: DrawingOptions = [], swiftyAttributes: [Attribute]? = nil) -> Rect { | ||
return boundingRect(with: size, options: options, attributes: swiftyAttributes?.foundationAttributes) | ||
} | ||
} | ||
#endif |
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,68 @@ | ||
// | ||
// NSString_Tests.swift | ||
// SwiftyAttributes | ||
// | ||
// Created by Roman Podymov on 02/06/2019. | ||
// Copyright © 2019 Roman Podymov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import SwiftyAttributes | ||
|
||
class NSString_Tests: XCTestCase { | ||
|
||
private let subjectAttributes: [Attribute] = [ | ||
.baselineOffset(10.0), | ||
.font(.systemFont(ofSize: 19)), | ||
.textColor(.magenta) | ||
] | ||
|
||
private let expectedAttributes: [NSAttributedString.Key : Any] = [ | ||
.baselineOffset : 10.0, | ||
.font : Font.systemFont(ofSize: 19), | ||
.foregroundColor : Color.magenta | ||
] | ||
|
||
func testSizeWithSwiftyAttributes() { | ||
let testString = "Hello World" as NSString | ||
let subject = testString.swifty_size(withSwiftyAttributes: subjectAttributes) | ||
let expected = testString.size(withAttributes: expectedAttributes) | ||
XCTAssertEqual(subject, expected) | ||
} | ||
|
||
func testBoundingRectWithSizeOptionsSwiftyAttributesContext() { | ||
let testString = "Hello World" as NSString | ||
let testSize = CGSize(width: 50, height: 100) | ||
let subject = testString.swifty_boundingRect( | ||
with: testSize, | ||
options: [], | ||
swiftyAttributes: subjectAttributes, | ||
context: nil | ||
) | ||
let expected = testString.boundingRect( | ||
with: testSize, | ||
options: [], | ||
attributes: expectedAttributes, | ||
context: nil | ||
) | ||
XCTAssertEqual(subject, expected) | ||
} | ||
|
||
#if os(macOS) | ||
func testBoundingRectWithSizeOptionsSwiftyAttributes() { | ||
let testString = "Hello World" as NSString | ||
let testSize = CGSize(width: 50, height: 100) | ||
let subject = testString.swifty_boundingRect( | ||
with: testSize, | ||
options: [], | ||
swiftyAttributes: subjectAttributes | ||
) | ||
let expected = testString.boundingRect( | ||
with: testSize, | ||
options: [], | ||
attributes: expectedAttributes | ||
) | ||
XCTAssertEqual(subject, expected) | ||
} | ||
#endif | ||
} |
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