From 041f5b226363a487f31d50701abb7df9cdfa5468 Mon Sep 17 00:00:00 2001 From: Guille Gonzalez Date: Sun, 20 Dec 2020 15:06:49 +0100 Subject: [PATCH] Fix watchOS builds --- .spi.yml | 5 +++ Makefile | 18 ++++++---- .../NSAttributedString+TextAttachment.swift | 34 +++++++++++-------- 3 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 .spi.yml diff --git a/.spi.yml b/.spi.yml new file mode 100644 index 0000000..8d77fb6 --- /dev/null +++ b/.spi.yml @@ -0,0 +1,5 @@ +version: 1 +builder: + configs: + - platform: watchos + scheme: AttributedText_watchOS diff --git a/Makefile b/Makefile index ced9d9f..fc5b542 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,23 @@ DESTINATION_MAC = platform=macOS -DESTINATION_IOS = platform=iOS Simulator,name=iPhone 8 -DESTINATION_TVOS = platform=tvOS Simulator,name=Apple TV +DESTINATION_IOS = generic/platform=ios +DESTINATION_TVOS = generic/platform=tvos +DESTINATION_WATCHOS = generic/platform=watchOS -default: test +default: build -test: - xcodebuild test \ +build: + xcodebuild \ -scheme AttributedText \ -destination '$(DESTINATION_MAC)' - xcodebuild test \ + xcodebuild \ -scheme AttributedText \ -destination '$(DESTINATION_IOS)' - xcodebuild test \ + xcodebuild \ -scheme AttributedText \ -destination '$(DESTINATION_TVOS)' + xcodebuild \ + -scheme AttributedText_watchOS \ + -destination '$(DESTINATION_WATCHOS)' format: swiftformat . diff --git a/Sources/AttributedText/NSAttributedString+TextAttachment.swift b/Sources/AttributedText/NSAttributedString+TextAttachment.swift index c2b4fef..5bd8b3c 100644 --- a/Sources/AttributedText/NSAttributedString+TextAttachment.swift +++ b/Sources/AttributedText/NSAttributedString+TextAttachment.swift @@ -1,20 +1,24 @@ -#if os(macOS) - import AppKit -#elseif canImport(UIKit) - import UIKit -#endif +#if !os(watchOS) + + #if os(macOS) + import AppKit + #elseif canImport(UIKit) + import UIKit + #endif -extension NSAttributedString { - func updateImageTextAttachments(maxWidth: CGFloat) { - enumerateAttribute(.attachment, in: NSRange(location: 0, length: length), options: []) { value, _, _ in - guard let attachment = value as? NSTextAttachment, - let image = attachment.image else { return } + extension NSAttributedString { + func updateImageTextAttachments(maxWidth: CGFloat) { + enumerateAttribute(.attachment, in: NSRange(location: 0, length: length), options: []) { value, _, _ in + guard let attachment = value as? NSTextAttachment, + let image = attachment.image else { return } - let aspectRatio = image.size.width / image.size.height - let width = min(maxWidth, image.size.width) - let height = width / aspectRatio + let aspectRatio = image.size.width / image.size.height + let width = min(maxWidth, image.size.width) + let height = width / aspectRatio - attachment.bounds = CGRect(x: 0, y: 0, width: width, height: height) + attachment.bounds = CGRect(x: 0, y: 0, width: width, height: height) + } } } -} + +#endif