-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Override square annotation image view
- Loading branch information
Showing
6 changed files
with
85 additions
and
0 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
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,20 @@ | ||
// | ||
// AnnotationManager.swift | ||
// Zotero | ||
// | ||
// Created by Miltiadis Vasilakis on 25/6/24. | ||
// Copyright © 2024 Corporation for Digital Scholarship. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import PSPDFKit | ||
|
||
class AnnotationManager: PSPDFKit.AnnotationManager { | ||
override func annotationViewClass(for annotation: Annotation) -> AnyClass? { | ||
if annotation is SquareAnnotation { | ||
// Use a custom annotation view subclass for square annotations. | ||
return SquareAnnotationView.self | ||
} | ||
return super.annotationViewClass(for: annotation) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// SquareAnnotationView.swift | ||
// Zotero | ||
// | ||
// Created by Miltiadis Vasilakis on 25/6/24. | ||
// Copyright © 2024 Corporation for Digital Scholarship. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import PSPDFKitUI | ||
|
||
class SquareAnnotationView: PSPDFKitUI.HostingAnnotationView { | ||
private var commentImageView: UIImageView? | ||
|
||
override var annotationImageView: UIImageView? { | ||
commentImageView?.removeFromSuperview() | ||
commentImageView = nil | ||
guard let annotationImageView = super.annotationImageView else { | ||
return nil | ||
} | ||
guard let annotation, let comment = annotation.contents, !comment.isEmpty, !annotation.flags.contains(.hidden) else { | ||
return annotationImageView | ||
} | ||
commentImageView = createCommentImageView(in: annotationImageView) | ||
return annotationImageView | ||
|
||
func createCommentImageView(in annotationImageView: UIImageView) -> UIImageView { | ||
let width: CGFloat = 12 | ||
let height: CGFloat = 12 | ||
let alpha: CGFloat = 0.75 | ||
let size = CGSize(width: width, height: height) | ||
let commentImage = UIGraphicsImageRenderer(size: size).image { context in | ||
CommentIconDrawingController.draw(context: context.cgContext, origin: .zero, size: size, color: annotation.color ?? .black, alpha: alpha) | ||
} | ||
let commentImageView = UIImageView() | ||
commentImageView.image = commentImage | ||
commentImageView.contentMode = .scaleAspectFit | ||
annotationImageView.addSubview(commentImageView) | ||
commentImageView.translatesAutoresizingMaskIntoConstraints = false | ||
let widthMultiplier = width / annotation.boundingBox.width | ||
let heightMultiplier = height / annotation.boundingBox.height | ||
NSLayoutConstraint.activate([ | ||
commentImageView.widthAnchor.constraint(equalTo: annotationImageView.widthAnchor, multiplier: widthMultiplier), | ||
commentImageView.heightAnchor.constraint(equalTo: annotationImageView.heightAnchor, multiplier: heightMultiplier), | ||
commentImageView.centerXAnchor.constraint(equalTo: annotationImageView.leadingAnchor), | ||
commentImageView.centerYAnchor.constraint(equalTo: annotationImageView.topAnchor) | ||
]) | ||
return commentImageView | ||
} | ||
} | ||
} |