-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd232b6
commit 591faa7
Showing
13 changed files
with
187 additions
and
108 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
12 changes: 12 additions & 0 deletions
12
Sources/ZNSTextAttachment/ZNSTextAttachmentDataSource.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,12 @@ | ||
// | ||
// ZNSTextAttachmentDataSource.swift | ||
// | ||
// | ||
// Created by zhgchgli on 2023/3/5. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol ZNSTextAttachmentDataSource: AnyObject { | ||
func zNSTextAttachment(_ textAttachment: ZNSTextAttachment, loadImageURL imageURL: URL, completion: @escaping (Data) -> Void) | ||
} |
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,12 @@ | ||
// | ||
// ZNSTextAttachmentDelegate.swift | ||
// | ||
// | ||
// Created by zhgchgli on 2023/3/5. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol ZNSTextAttachmentDelegate: AnyObject { | ||
func zNSTextAttachment(didLoad textAttachment: ZNSTextAttachment) | ||
} |
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,10 @@ | ||
// | ||
// ZNSTextAttachmentHandler.swift | ||
// | ||
// | ||
// Created by zhgchgli on 2023/3/5. | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias ZNSTextAttachmentHandler = (ZNSTextAttachmentDataSource & ZNSTextAttachmentDelegate) |
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
57 changes: 57 additions & 0 deletions
57
Sources/ZNSTextAttachment/iOS/ZResizableNSTextAttachment.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,57 @@ | ||
// | ||
// ZResizableNSTextAttachment.swift | ||
// | ||
// | ||
// Created by zhgchgli on 2023/3/5. | ||
// | ||
|
||
import Foundation | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
|
||
public class ZResizableNSTextAttachment: NSTextAttachment { | ||
|
||
public let imageSize: CGSize? | ||
public let fixedWidth: CGFloat? | ||
public let fixedHeight: CGFloat? | ||
|
||
public init(imageSize: CGSize?, fixedWidth: CGFloat?, fixedHeight: CGFloat?, data: Data, type: String) { | ||
self.imageSize = imageSize | ||
self.fixedWidth = fixedWidth | ||
self.fixedHeight = fixedHeight | ||
|
||
super.init(data: data, ofType: type) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
public override func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect { | ||
|
||
if let fixedWidth = self.fixedWidth, | ||
let fixedHeight = self.fixedHeight { | ||
return CGRect(origin: .zero, size: CGSize(width: fixedWidth, height: fixedHeight)) | ||
} | ||
|
||
guard let imageWidth = imageSize?.width, | ||
let imageHeight = imageSize?.height else { | ||
return .zero | ||
} | ||
|
||
if let fixedWidth = self.fixedWidth { | ||
let factor = fixedWidth / imageWidth | ||
return CGRect(origin: .zero, size:CGSize(width: Int(fixedWidth), height: Int(imageHeight * factor))) | ||
} else if let fixedHeight = self.fixedHeight { | ||
let factor = fixedHeight / imageHeight | ||
return CGRect(origin: .zero, size:CGSize(width: Int(imageWidth * factor), height: Int(fixedHeight))) | ||
} else { | ||
let maxWidth = lineFrag.size.width - ((textContainer?.lineFragmentPadding ?? 0) * 2) | ||
let factor = maxWidth / imageWidth | ||
|
||
return CGRect(origin: .zero, size:CGSize(width: Int(imageWidth * factor), height: Int(imageHeight * factor))) | ||
} | ||
} | ||
} | ||
#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
Oops, something went wrong.