Skip to content

Commit

Permalink
[Added] ImageAttachment
Browse files Browse the repository at this point in the history
[Added] ImageAttachment test in sample project, as SPM does not support resource (test failed)
  • Loading branch information
ethanhuang13 committed Jul 11, 2019
1 parent 0b6bbc3 commit ab36947
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 12 deletions.
Expand Up @@ -61,6 +61,11 @@
BlueprintName = "NSAttributedStringBuilderTests"
ReferencedContainer = "container:">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "ImageAttachmentTests">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
</TestAction>
Expand Down
8 changes: 2 additions & 6 deletions Sources/NSAttributedStringBuilder/Components/Component.swift
@@ -1,9 +1,9 @@
#if canImport(UIKit)
import UIKit
typealias Size = CGSize
public typealias Size = CGSize
#elseif canImport(AppKit)
import AppKit
typealias Size = NSSize
public typealias Size = NSSize
#endif

public protocol Component {
Expand Down Expand Up @@ -46,10 +46,6 @@ extension Component {
// MARK: Basic Modifiers

extension Component {
public func attachment(_ attachment: NSTextAttachment) -> some Component {
attributes([.attachment: attachment])
}

public func backgroundColor(_ color: Color) -> some Component {
attributes([.backgroundColor: color])
}
Expand Down
30 changes: 30 additions & 0 deletions Sources/NSAttributedStringBuilder/Components/ImageAttachment.swift
@@ -0,0 +1,30 @@
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif

public typealias ImageAttachment = NSAttributedString.ImageAttchment

extension NSAttributedString {
public struct ImageAttchment: Component {
public let string: String = ""
public let attributes: Attributes = [:]
private let attachment: NSTextAttachment

public init(_ image: UIImage, size: Size? = nil) {
let attachment = NSTextAttachment()
attachment.image = image

if let size = size {
attachment.bounds.size = size
}

self.attachment = attachment
}

public var attributedString: NSAttributedString {
return NSAttributedString(attachment: attachment)
}
}
}
133 changes: 133 additions & 0 deletions SwiftUISampleApp/AttributedTextSample.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
6C4BCC9222D74C9700A4F8D7 /* ImageAttachmentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C4BCC9122D74C9700A4F8D7 /* ImageAttachmentTests.swift */; };
6C4BCC9322D74CAF00A4F8D7 /* Swift_logo_color_rgb.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 6C4BCC8122D74C0A00A4F8D7 /* Swift_logo_color_rgb.jpg */; };
6C4BCC9422D74D8200A4F8D7 /* Swift_logo_color_rgb.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 6C4BCC8122D74C0A00A4F8D7 /* Swift_logo_color_rgb.jpg */; };
6C65EA2822D489A300EF32FB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C65EA2722D489A300EF32FB /* AppDelegate.swift */; };
6C65EA2A22D489A300EF32FB /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C65EA2922D489A300EF32FB /* SceneDelegate.swift */; };
6C65EA2C22D489A300EF32FB /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C65EA2B22D489A300EF32FB /* ContentView.swift */; };
Expand All @@ -17,8 +20,22 @@
6CF300B522D4A36600D8C7DF /* AttributedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CF300B422D4A36600D8C7DF /* AttributedText.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
6C4BCC8C22D74C8D00A4F8D7 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 6C65EA1C22D489A200EF32FB /* Project object */;
proxyType = 1;
remoteGlobalIDString = 6C65EA2322D489A300EF32FB;
remoteInfo = AttributedTextSample;
};
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
6C456A1122D6EEAE00A02325 /* NSAttributedStringBuilder */ = {isa = PBXFileReference; lastKnownFileType = folder; name = NSAttributedStringBuilder; path = ..; sourceTree = "<group>"; };
6C4BCC8122D74C0A00A4F8D7 /* Swift_logo_color_rgb.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Swift_logo_color_rgb.jpg; sourceTree = "<group>"; };
6C4BCC8722D74C8D00A4F8D7 /* AttributedTextSampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AttributedTextSampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
6C4BCC8B22D74C8D00A4F8D7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6C4BCC9122D74C9700A4F8D7 /* ImageAttachmentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageAttachmentTests.swift; sourceTree = "<group>"; };
6C65EA2422D489A300EF32FB /* AttributedTextSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AttributedTextSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
6C65EA2722D489A300EF32FB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
6C65EA2922D489A300EF32FB /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -32,6 +49,13 @@
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
6C4BCC8422D74C8D00A4F8D7 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
6C65EA2122D489A300EF32FB /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
Expand All @@ -43,11 +67,22 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
6C4BCC8822D74C8D00A4F8D7 /* AttributedTextSampleTests */ = {
isa = PBXGroup;
children = (
6C4BCC9122D74C9700A4F8D7 /* ImageAttachmentTests.swift */,
6C4BCC8122D74C0A00A4F8D7 /* Swift_logo_color_rgb.jpg */,
6C4BCC8B22D74C8D00A4F8D7 /* Info.plist */,
);
path = AttributedTextSampleTests;
sourceTree = "<group>";
};
6C65EA1B22D489A200EF32FB = {
isa = PBXGroup;
children = (
6C456A1122D6EEAE00A02325 /* NSAttributedStringBuilder */,
6C65EA2622D489A300EF32FB /* AttributedTextSample */,
6C4BCC8822D74C8D00A4F8D7 /* AttributedTextSampleTests */,
6C65EA2522D489A300EF32FB /* Products */,
6CF300A822D48AC200D8C7DF /* Frameworks */,
);
Expand All @@ -57,6 +92,7 @@
isa = PBXGroup;
children = (
6C65EA2422D489A300EF32FB /* AttributedTextSample.app */,
6C4BCC8722D74C8D00A4F8D7 /* AttributedTextSampleTests.xctest */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -95,6 +131,24 @@
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
6C4BCC8622D74C8D00A4F8D7 /* AttributedTextSampleTests */ = {
isa = PBXNativeTarget;
buildConfigurationList = 6C4BCC8E22D74C8D00A4F8D7 /* Build configuration list for PBXNativeTarget "AttributedTextSampleTests" */;
buildPhases = (
6C4BCC8322D74C8D00A4F8D7 /* Sources */,
6C4BCC8422D74C8D00A4F8D7 /* Frameworks */,
6C4BCC8522D74C8D00A4F8D7 /* Resources */,
);
buildRules = (
);
dependencies = (
6C4BCC8D22D74C8D00A4F8D7 /* PBXTargetDependency */,
);
name = AttributedTextSampleTests;
productName = AttributedTextSampleTests;
productReference = 6C4BCC8722D74C8D00A4F8D7 /* AttributedTextSampleTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
6C65EA2322D489A300EF32FB /* AttributedTextSample */ = {
isa = PBXNativeTarget;
buildConfigurationList = 6C65EA3822D489A400EF32FB /* Build configuration list for PBXNativeTarget "AttributedTextSample" */;
Expand Down Expand Up @@ -125,6 +179,10 @@
LastUpgradeCheck = 1100;
ORGANIZATIONNAME = "Elaborapp Co., Ltd.";
TargetAttributes = {
6C4BCC8622D74C8D00A4F8D7 = {
CreatedOnToolsVersion = 11.0;
TestTargetID = 6C65EA2322D489A300EF32FB;
};
6C65EA2322D489A300EF32FB = {
CreatedOnToolsVersion = 11.0;
};
Expand All @@ -146,15 +204,25 @@
projectRoot = "";
targets = (
6C65EA2322D489A300EF32FB /* AttributedTextSample */,
6C4BCC8622D74C8D00A4F8D7 /* AttributedTextSampleTests */,
);
};
/* End PBXProject section */

/* Begin PBXResourcesBuildPhase section */
6C4BCC8522D74C8D00A4F8D7 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6C4BCC9322D74CAF00A4F8D7 /* Swift_logo_color_rgb.jpg in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
6C65EA2222D489A300EF32FB /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6C4BCC9422D74D8200A4F8D7 /* Swift_logo_color_rgb.jpg in Resources */,
6C65EA3422D489A400EF32FB /* LaunchScreen.storyboard in Resources */,
6C65EA3122D489A400EF32FB /* Preview Assets.xcassets in Resources */,
6C65EA2E22D489A400EF32FB /* Assets.xcassets in Resources */,
Expand All @@ -164,6 +232,14 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
6C4BCC8322D74C8D00A4F8D7 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6C4BCC9222D74C9700A4F8D7 /* ImageAttachmentTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
6C65EA2022D489A300EF32FB /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
Expand All @@ -177,6 +253,14 @@
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
6C4BCC8D22D74C8D00A4F8D7 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 6C65EA2322D489A300EF32FB /* AttributedTextSample */;
targetProxy = 6C4BCC8C22D74C8D00A4F8D7 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
6C65EA3222D489A400EF32FB /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
Expand All @@ -189,6 +273,46 @@
/* End PBXVariantGroup section */

/* Begin XCBuildConfiguration section */
6C4BCC8F22D74C8D00A4F8D7 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = SE9N6X5V7C;
INFOPLIST_FILE = AttributedTextSampleTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.elaborapp.NSAttributedStringBuilder.AttributedTextSampleTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AttributedTextSample.app/AttributedTextSample";
};
name = Debug;
};
6C4BCC9022D74C8D00A4F8D7 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = SE9N6X5V7C;
INFOPLIST_FILE = AttributedTextSampleTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.elaborapp.NSAttributedStringBuilder.AttributedTextSampleTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AttributedTextSample.app/AttributedTextSample";
};
name = Release;
};
6C65EA3622D489A400EF32FB /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
Expand Down Expand Up @@ -350,6 +474,15 @@
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
6C4BCC8E22D74C8D00A4F8D7 /* Build configuration list for PBXNativeTarget "AttributedTextSampleTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
6C4BCC8F22D74C8D00A4F8D7 /* Debug */,
6C4BCC9022D74C8D00A4F8D7 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
6C65EA1F22D489A200EF32FB /* Build configuration list for PBXProject "AttributedTextSample" */ = {
isa = XCConfigurationList;
buildConfigurations = (
Expand Down
Expand Up @@ -26,8 +26,29 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "NSAttributedStringBuilderTests"
BuildableName = "NSAttributedStringBuilderTests"
BlueprintName = "NSAttributedStringBuilderTests"
ReferencedContainer = "container:..">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6C4BCC8622D74C8D00A4F8D7"
BuildableName = "AttributedTextSampleTests.xctest"
BlueprintName = "AttributedTextSampleTests"
ReferencedContainer = "container:AttributedTextSample.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
3 changes: 3 additions & 0 deletions SwiftUISampleApp/AttributedTextSample/AttributedText.swift
Expand Up @@ -31,6 +31,9 @@ final public class AttributedText: UIViewRepresentable {
struct AttributedText_Previews : PreviewProvider {
static var previews: some View {
AttributedText {
ImageAttachment(UIImage(named: "Swift_logo_color_rgb.jpg")!, size: CGSize(width: 90, height: 90))
AttrText.linebreak
.lineSpacing(20)
AttrText("Hello SwiftUI")
.backgroundColor(.red)
.baselineOffset(10)
Expand Down
1 change: 1 addition & 0 deletions SwiftUISampleApp/AttributedTextSample/ContentView.swift
Expand Up @@ -17,6 +17,7 @@ struct ContentView : View {

// UILabel: UIViewRepresentable
AttributedText {
ImageAttachment(UIImage(named: "Swift_logo_color_rgb.jpg")!)
AttrText("AttributedText Title")
.font(.preferredFont(forTextStyle: .largeTitle))
AttrText.linebreak
Expand Down
@@ -0,0 +1,25 @@
@testable import NSAttributedStringBuilder
import XCTest

final class ImageAttachmentTests: XCTestCase {
func testSetImage() {
let testBundle = Bundle(for: ImageAttachmentTests.self)
let testImage = UIImage(contentsOfFile: testBundle.path(forResource: "Swift_logo_color_rgb", ofType: "jpg")!)!

let testData: NSAttributedString = {
let mas = NSMutableAttributedString(string: "Hello world")
let attachment = NSTextAttachment()
attachment.image = testImage
attachment.bounds = CGRect(x: 0, y: 0, width: 40, height: 40)
mas.append(NSAttributedString(attachment: attachment))
return mas
}()

let sut = NSAttributedString {
AttrText("Hello world")
ImageAttachment(testImage, size: CGSize(width: 40, height: 40))
}

XCTAssertTrue(sut.isEqual(testData))
}
}
22 changes: 22 additions & 0 deletions SwiftUISampleApp/AttributedTextSampleTests/Info.plist
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ab36947

Please sign in to comment.