Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Added] ImageAttachment test in sample project, as SPM does not support resource (test failed)
- Loading branch information
1 parent
0b6bbc3
commit ab36947
Showing
11 changed files
with
256 additions
and
12 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
30 changes: 30 additions & 0 deletions
30
Sources/NSAttributedStringBuilder/Components/ImageAttachment.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,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) | ||
} | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
SwiftUISampleApp/AttributedTextSampleTests/ImageAttachmentTests.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,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)) | ||
} | ||
} |
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,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.
Oops, something went wrong.