Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeContainer extensions #58

Merged
merged 5 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions SwiftyAttributes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
9E0DA88E229B34D800E29BFD /* UIKit+SwiftyAttributes_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E0DA88D229B34D800E29BFD /* UIKit+SwiftyAttributes_Tests.swift */; };
9E28077022A424320097CE8A /* NSString+macOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E28076F22A424320097CE8A /* NSString+macOS.swift */; };
9E351CDB22A3DDAC000FA7FC /* NSString_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E351CDA22A3DDAC000FA7FC /* NSString_Tests.swift */; };
9E54879329FD2E2A009EB1D6 /* AttributeContainer+SwiftyAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E54879229FD2E2A009EB1D6 /* AttributeContainer+SwiftyAttributes.swift */; };
9E54879529FD2E47009EB1D6 /* AttributeContainer+SwiftyAttributes_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E54879429FD2E47009EB1D6 /* AttributeContainer+SwiftyAttributes_Tests.swift */; };
9EB6D1A322A1CAC300D00CFC /* NSString+SwiftyAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB6D1A222A1CAC200D00CFC /* NSString+SwiftyAttributes.swift */; };
C027C0C91DEA0A0100953C09 /* SpellingState_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C027C0C81DEA0A0100953C09 /* SpellingState_Tests.swift */; };
C027C0CB1DEA452500953C09 /* Attribute+Sequence_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C027C0CA1DEA452500953C09 /* Attribute+Sequence_Tests.swift */; };
Expand Down Expand Up @@ -80,6 +82,8 @@
9E0DA88D229B34D800E29BFD /* UIKit+SwiftyAttributes_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIKit+SwiftyAttributes_Tests.swift"; sourceTree = "<group>"; };
9E28076F22A424320097CE8A /* NSString+macOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSString+macOS.swift"; sourceTree = "<group>"; };
9E351CDA22A3DDAC000FA7FC /* NSString_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSString_Tests.swift; sourceTree = "<group>"; };
9E54879229FD2E2A009EB1D6 /* AttributeContainer+SwiftyAttributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "AttributeContainer+SwiftyAttributes.swift"; path = "common/AttributeContainer+SwiftyAttributes.swift"; sourceTree = "<group>"; };
9E54879429FD2E47009EB1D6 /* AttributeContainer+SwiftyAttributes_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AttributeContainer+SwiftyAttributes_Tests.swift"; sourceTree = "<group>"; };
9EB6D1A222A1CAC200D00CFC /* NSString+SwiftyAttributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSString+SwiftyAttributes.swift"; path = "common/NSString+SwiftyAttributes.swift"; sourceTree = "<group>"; };
C027C0C81DEA0A0100953C09 /* SpellingState_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpellingState_Tests.swift; sourceTree = "<group>"; };
C027C0CA1DEA452500953C09 /* Attribute+Sequence_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Attribute+Sequence_Tests.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -225,6 +229,7 @@
isa = PBXGroup;
children = (
C027C0CA1DEA452500953C09 /* Attribute+Sequence_Tests.swift */,
9E54879429FD2E47009EB1D6 /* AttributeContainer+SwiftyAttributes_Tests.swift */,
C08D04141DC3B16A00575F98 /* NSAttributedString_Tests.swift */,
C08D04121DC39B5500575F98 /* NSMutableAttributedString_Tests.swift */,
9E351CDA22A3DDAC000FA7FC /* NSString_Tests.swift */,
Expand All @@ -245,6 +250,7 @@
children = (
C050821F1DFA72F700D39B3B /* Attribute.swift */,
C050821E1DFA72F700D39B3B /* Attribute+Sequence.swift */,
9E54879229FD2E2A009EB1D6 /* AttributeContainer+SwiftyAttributes.swift */,
C05082211DFA72F700D39B3B /* Ligatures.swift */,
C05082221DFA72F700D39B3B /* NSAttributedString+SwiftyAttributes.swift */,
C05082231DFA72F700D39B3B /* NSMutableAttributedString+SwiftyAttributes.swift */,
Expand Down Expand Up @@ -483,6 +489,7 @@
buildActionMask = 2147483647;
files = (
9E04C4D126EA09A5009BA429 /* SwiftyAttributedStringBuilder.swift in Sources */,
9E54879329FD2E2A009EB1D6 /* AttributeContainer+SwiftyAttributes.swift in Sources */,
C050822A1DFA72F700D39B3B /* Attribute.swift in Sources */,
C050822F1DFA72F700D39B3B /* Operators.swift in Sources */,
C050822E1DFA72F700D39B3B /* NSMutableAttributedString+SwiftyAttributes.swift in Sources */,
Expand Down Expand Up @@ -513,6 +520,7 @@
C027C0C91DEA0A0100953C09 /* SpellingState_Tests.swift in Sources */,
C0F96A031DEDE1D300D039A4 /* VerticalGlyphForm_Tests.swift in Sources */,
C027C0CB1DEA452500953C09 /* Attribute+Sequence_Tests.swift in Sources */,
9E54879529FD2E47009EB1D6 /* AttributeContainer+SwiftyAttributes_Tests.swift in Sources */,
9E351CDB22A3DDAC000FA7FC /* NSString_Tests.swift in Sources */,
9E0DA88E229B34D800E29BFD /* UIKit+SwiftyAttributes_Tests.swift in Sources */,
C0E1C96F1DD31A0D0068E85C /* Operators_Tests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// AttributeContainer+SwiftyAttributes.swift
// SwiftyAttributes
//
// Created by Roman Podymov on 29/04/23.
// Copyright © 2023 Roman Podymov. All rights reserved.
//

import Foundation

#if canImport(UIKit)
@available(iOS 15.0, tvOS 15.0, watchOS 8.0, *)
public extension AttributeContainer {
mutating func set(attribute: Attribute) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary in this PR, but would be neat to also add a custom subscript to set and get the attributes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @eddiekaiger
Do you mean something like

public extension AttributeContainer {
    subscript(attribute: Attribute) -> Any? {
        get { /* */ }
        set { /* */ }
    }
}

?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!

switch attribute {
#if !os(watchOS)
case .attachment(let value):
self[AttributeScopes.UIKitAttributes.AttachmentAttribute.self] = value
case .shadow(let value):
self[AttributeScopes.UIKitAttributes.ShadowAttribute.self] = value
#endif
case .baselineOffset(let value):
self[AttributeScopes.UIKitAttributes.BaselineOffsetAttribute.self] = value
case .backgroundColor(let value):
self[AttributeScopes.UIKitAttributes.BackgroundColorAttribute.self] = value
case .expansion(let value):
self[AttributeScopes.UIKitAttributes.ExpansionAttribute.self] = value
case .font(let value):
self[AttributeScopes.UIKitAttributes.FontAttribute.self] = value
case .kern(let value):
self[AttributeScopes.UIKitAttributes.KernAttribute.self] = value
case .ligatures(let value):
self[AttributeScopes.UIKitAttributes.LigatureAttribute.self] = value.rawValue
case .link(let value):
self[AttributeScopes.FoundationAttributes.LinkAttribute.self] = value
case .obliqueness(let value):
self[AttributeScopes.UIKitAttributes.ObliquenessAttribute.self] = value
case .paragraphStyle(let value):
self[AttributeScopes.UIKitAttributes.ParagraphStyleAttribute.self] = value
case .strokeColor(let value):
self[AttributeScopes.UIKitAttributes.StrokeColorAttribute.self] = value
case .strokeWidth(let value):
self[AttributeScopes.UIKitAttributes.StrokeWidthAttribute.self] = value
case .strikethroughColor(let value):
self[AttributeScopes.UIKitAttributes.StrikethroughColorAttribute.self] = value
case .strikethroughStyle(let value):
self[AttributeScopes.UIKitAttributes.StrikethroughStyleAttribute.self] = value
case .textColor(let value):
self[AttributeScopes.UIKitAttributes.ForegroundColorAttribute.self] = value
case .textEffect(let value):
self[AttributeScopes.UIKitAttributes.TextEffectAttribute.self] = value.textEffectStyle
case .underlineColor(let value):
self[AttributeScopes.UIKitAttributes.UnderlineColorAttribute.self] = value
case .underlineStyle(let value):
self[AttributeScopes.UIKitAttributes.UnderlineStyleAttribute.self] = value
default:
break
}
}
}
#endif
9 changes: 8 additions & 1 deletion SwiftyAttributes/Sources/common/TextEffect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ public enum TextEffect: RawRepresentable {
public var rawValue: String {
switch self {
case .letterPressStyle:
return NSAttributedString.TextEffectStyle.letterpressStyle.rawValue
return textEffectStyle.rawValue
}
}

public var textEffectStyle: NSAttributedString.TextEffectStyle {
switch self {
case .letterPressStyle:
return NSAttributedString.TextEffectStyle.letterpressStyle
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// AttributeContainer+SwiftyAttributes_Tests.swift
// SwiftyAttributes
//
// Created by Roman Podymov on 29/04/23.
// Copyright © 2023 Roman Podymov. All rights reserved.
//

import XCTest
import SwiftyAttributes

class AttributeContainer_SwiftyAttributes_Tests: XCTestCase {
#if canImport(UIKit)
@available(iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func testAttributeContainerSetAttribute() {
var container = AttributeContainer()
container.set(attribute: .textColor(.blue))
container.set(attribute: .backgroundColor(.yellow))
XCTAssertEqual(container.foregroundColor, UIColor.blue)
XCTAssertEqual(container.backgroundColor, UIColor.yellow)
}
#endif
}
4 changes: 4 additions & 0 deletions SwiftyAttributesTests/NSAttributedString_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class NSAttributedString_Tests: XCTestCase {
// MARK: - Attribute At Location

func testAttributeAtLocation_attachment() {
#if !os(watchOS)
let attachment = TextAttachment()
#if os(macOS)
attachment.image = NSImage()
Expand All @@ -75,6 +76,7 @@ class NSAttributedString_Tests: XCTestCase {
let expected = str.attribute(.attachment, at: 0, effectiveRange: nil) as! TextAttachment
XCTAssertEqual(subject, expected)
XCTAssertEqual(subject, attachment)
#endif
}

func testAttributeAtLocation_baselineOffet() {
Expand Down Expand Up @@ -154,13 +156,15 @@ class NSAttributedString_Tests: XCTestCase {
}

func testAttributeAtLocation_shadow() {
#if !os(watchOS)
let shadow = Shadow()
shadow.shadowOffset = CGSize(width: 3, height: 5)
let str = "Hello".withShadow(shadow)
let subject = str.swiftyAttribute(.shadow, at: 0, effectiveRange: nil)!.value as! Shadow
let expected = str.attribute(.shadow, at: 0, effectiveRange: nil) as! Shadow
XCTAssertEqual(subject, expected)
XCTAssertEqual(subject, shadow)
#endif
}

func testAttributeAtLocation_strokeColor() {
Expand Down
4 changes: 4 additions & 0 deletions SwiftyAttributesTests/SwiftyAttributes_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class SwiftyAttributesTests: XCTestCase {
}

func testAttribute_shadow() {
#if !os(watchOS)
let shadow = Shadow()
shadow.shadowBlurRadius = 4
shadow.shadowColor = Color.brown
Expand All @@ -130,6 +131,7 @@ class SwiftyAttributesTests: XCTestCase {
let expected = NSAttributedString(string: "Hello", attributes: [.shadow: shadow])
XCTAssertEqual(subject, expected)
XCTAssertEqual(subject2, expected)
#endif
}

func testAttribute_textEffect() {
Expand All @@ -141,12 +143,14 @@ class SwiftyAttributesTests: XCTestCase {
}

func testAttribute_attachment() {
#if !os(watchOS)
let attachment = TextAttachment(data: nil, ofType: nil)
let subject = "Hello".withAttachment(attachment)
let subject2 = "Hello".attributedString.withAttachment(attachment)
let expected = NSAttributedString(string: "Hello", attributes: [.attachment: attachment])
XCTAssertEqual(subject, expected)
XCTAssertEqual(subject2, expected)
#endif
}

func testAttribute_link() {
Expand Down