123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785 |
- //
- // CAnnotStyle.swift
- // ComPDFKit_Tools
- //
- // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- import UIKit
- import ComPDFKit
- public class CAnnotStyle: NSObject {
-
- var isSelectAnnot:Bool = false
- var annotations:[CPDFAnnotation] = []
- var annotMode:CPDFViewAnnotationMode = .CPDFViewAnnotationModenone
- var headKeys:[String] = []
- var trialKeys:[String] = []
-
- init(annotionMode: CPDFViewAnnotationMode, annotations: [CPDFAnnotation]?) {
- super.init()
- if annotations?.count ?? 0 > 0 {
- isSelectAnnot = true
- self.annotations = annotations ?? []
- annotMode = convertAnnotationType()
- } else {
- isSelectAnnot = false
- annotMode = annotionMode
- }
-
- }
- var annotation: CPDFAnnotation? {
- return annotations.first
- }
-
- func convertAnnotationType() -> CPDFViewAnnotationMode {
- var annotationType: CPDFViewAnnotationMode = .CPDFViewAnnotationModenone
-
- if(annotation != nil) {
-
- if annotation is CPDFFreeTextAnnotation {
- annotationType = .freeText
- } else if annotation is CPDFTextAnnotation {
- annotationType = .note
- } else if annotation is CPDFCircleAnnotation {
- annotationType = .circle
- } else if annotation is CPDFSquareAnnotation {
- annotationType = .square
- } else if annotation is CPDFMarkupAnnotation {
- let markupAnnotation:CPDFMarkupAnnotation = annotation as? CPDFMarkupAnnotation ?? CPDFMarkupAnnotation()
- if markupAnnotation.markupType() == .highlight {
- annotationType = .highlight
- } else if markupAnnotation.markupType() == .strikeOut {
- annotationType = .strikeout
- } else if markupAnnotation.markupType() == .underline {
- annotationType = .underline
- } else if markupAnnotation.markupType() == .squiggly {
- annotationType = .squiggly
- }
- } else if annotation is CPDFLineAnnotation {
- let lineAnnotation:CPDFLineAnnotation = annotation as? CPDFLineAnnotation ?? CPDFLineAnnotation()
-
- if lineAnnotation.endLineStyle == .none && lineAnnotation.startLineStyle == .none {
- annotationType = .line
- } else {
- annotationType = .arrow
- }
- } else if annotation is CPDFInkAnnotation {
- annotationType = .ink
- } else if annotation is CPDFLinkAnnotation {
- annotationType = .link
- } else if annotation is CPDFSignatureAnnotation {
- annotationType = .signature
- } else if annotation is CPDFStampAnnotation {
- annotationType = .stamp
- } else if annotation is CPDFSoundAnnotation {
- annotationType = .sound
- } else if annotation is CPDFTextWidgetAnnotation {
- annotationType = .formModeText
- } else if annotation is CPDFButtonWidgetAnnotation {
- let buttonWidgetAnnotation:CPDFButtonWidgetAnnotation = annotation as? CPDFButtonWidgetAnnotation ?? CPDFButtonWidgetAnnotation()
-
- if buttonWidgetAnnotation.controlType() == .checkBoxControl {
- annotationType = .formModeCheckBox
- } else if buttonWidgetAnnotation.controlType() == .radioButtonControl {
- annotationType = .formModeRadioButton
- } else if buttonWidgetAnnotation.controlType() == .pushButtonControl {
- annotationType = .formModeButton
- }
- } else if annotation is CPDFChoiceWidgetAnnotation {
- let choiceWidgetAnnotation:CPDFChoiceWidgetAnnotation = annotation as? CPDFChoiceWidgetAnnotation ?? CPDFChoiceWidgetAnnotation()
-
- if choiceWidgetAnnotation.isListChoice {
- annotationType = .formModeList
- } else {
- annotationType = .formModeCombox
- }
- }
- }
-
- return annotationType
- }
-
-
- // MARK: - Common
- var color: UIColor? {
- if isSelectAnnot {
- return annotation?.color
- } else {
- let userDefaults = UserDefaults.standard
- switch annotMode {
- case .note:
- return userDefaults.PDFListViewColorForKey(CAnchoredNoteColorKey)
- case .circle:
- return userDefaults.PDFListViewColorForKey(CCircleNoteColorKey)
- case .square:
- return userDefaults.PDFListViewColorForKey(CSquareNoteColorKey)
- case .highlight:
- return userDefaults.PDFListViewColorForKey(CHighlightNoteColorKey)
- case .underline:
- return userDefaults.PDFListViewColorForKey(CUnderlineNoteColorKey)
- case .strikeout:
- return userDefaults.PDFListViewColorForKey(CStrikeOutNoteColorKey)
- case .squiggly:
- return userDefaults.PDFListViewColorForKey(CSquigglyNoteColorKey)
- case .line:
- return userDefaults.PDFListViewColorForKey(CLineNoteColorKey)
- case .arrow:
- return userDefaults.PDFListViewColorForKey(CArrowNoteColorKey)
- case .ink:
- return CPDFKitConfig.sharedInstance().freehandAnnotationColor()
- default:
- return nil
- }
- }
- }
- func setColor(_ color: UIColor?) {
- if isSelectAnnot {
- annotations.forEach { annotation in
- annotation.color = color
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch annotMode {
- case .note:
- userDefaults.setPDFListViewColor(color, forKey: CAnchoredNoteColorKey)
- case .circle:
- userDefaults.setPDFListViewColor(color, forKey: CCircleNoteColorKey)
- case .square:
- userDefaults.setPDFListViewColor(color, forKey: CSquareNoteColorKey)
- case .highlight:
- userDefaults.setPDFListViewColor(color, forKey: CHighlightNoteColorKey)
- case .underline:
- userDefaults.setPDFListViewColor(color, forKey: CUnderlineNoteColorKey)
- case .strikeout:
- userDefaults.setPDFListViewColor(color, forKey: CStrikeOutNoteColorKey)
- case .squiggly:
- userDefaults.setPDFListViewColor(color, forKey: CSquigglyNoteColorKey)
- case .line:
- userDefaults.setPDFListViewColor(color, forKey: CLineNoteColorKey)
- case .arrow:
- userDefaults.setPDFListViewColor(color, forKey: CArrowNoteColorKey)
- case .ink:
- CPDFKitConfig.sharedInstance().setFreehandAnnotationColor(color)
- userDefaults.setPDFListViewColor(color, forKey: CInkNoteColorKey)
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
-
- var opacity: CGFloat {
- var opacity: CGFloat = 0
- if isSelectAnnot {
- opacity = annotation?.opacity ?? 1.0
- } else {
- let userDefaults = UserDefaults.standard
- switch annotMode {
- case .freeText:
- opacity = CGFloat(userDefaults.float(forKey: CFreeTextNoteOpacityKey))
- case .note:
- opacity = CGFloat(userDefaults.float(forKey: CAnchoredNoteOpacityKey))
- case .circle:
- opacity = CGFloat(userDefaults.float(forKey: CCircleNoteOpacityKey))
- case .square:
- opacity = CGFloat(userDefaults.float(forKey: CSquareNoteOpacityKey))
- case .highlight:
- opacity = CGFloat(userDefaults.float(forKey: CHighlightNoteOpacityKey))
- case .underline:
- opacity = CGFloat(userDefaults.float(forKey: CUnderlineNoteOpacityKey))
- case .strikeout:
- opacity = CGFloat(userDefaults.float(forKey: CStrikeOutNoteOpacityKey))
- case .squiggly:
- opacity = CGFloat(userDefaults.float(forKey: CSquigglyNoteOpacityKey))
- case .line:
- opacity = CGFloat(userDefaults.float(forKey: CLineNoteOpacityKey))
- case .arrow:
- opacity = CGFloat(userDefaults.float(forKey: CArrowNoteOpacityKey))
- case .ink:
- opacity = CPDFKitConfig.sharedInstance().freehandAnnotationOpacity() / 100.0
- default:
- break
- }
- }
- return opacity
- }
- func setOpacity(_ opacity: CGFloat) {
- if isSelectAnnot {
- for annotation in annotations {
- annotation.opacity = opacity
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- userDefaults.set(opacity, forKey: CFreeTextNoteOpacityKey)
- case .note:
- userDefaults.set(opacity, forKey: CAnchoredNoteOpacityKey)
- case .circle:
- userDefaults.set(opacity, forKey: CCircleNoteOpacityKey)
- case .square:
- userDefaults.set(opacity, forKey: CSquareNoteOpacityKey)
- case .highlight:
- userDefaults.set(opacity, forKey: CHighlightNoteOpacityKey)
- case .underline:
- userDefaults.set(opacity, forKey: CUnderlineNoteOpacityKey)
- case .strikeout:
- userDefaults.set(opacity, forKey: CStrikeOutNoteOpacityKey)
- case .squiggly:
- userDefaults.set(opacity, forKey: CSquigglyNoteOpacityKey)
- case .line:
- userDefaults.set(opacity, forKey: CLineNoteOpacityKey)
- case .arrow:
- userDefaults.set(opacity, forKey: CArrowNoteOpacityKey)
- case .ink:
- CPDFKitConfig.sharedInstance().setFreehandAnnotationOpacity(opacity * 100)
- userDefaults.set(opacity, forKey: CInkNoteOpacityKey)
- default:
- break
- }
-
- userDefaults.synchronize()
- }
- }
-
- var style: CPDFBorderStyle {
- var style: CPDFBorderStyle = .solid
- if isSelectAnnot {
- style = annotation?.border.style ?? .solid
- } else {
- let userDefaults = UserDefaults.standard
- switch annotMode {
- case .freeText:
- style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CFreeTextNoteLineStyleKey)) ?? .solid
- case .circle:
- style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CCircleNoteLineStyleKey)) ?? .solid
- case .square:
- style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CSquareNoteLineStyleKey)) ?? .solid
- case .line:
- style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CLineNoteLineStyleKey)) ?? .solid
- case .arrow:
- style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CArrowNoteLineStyleKey)) ?? .solid
- case .ink:
- style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CInkNoteLineStyleyKey)) ?? .solid
- default:
- break
- }
- }
- return style
- }
-
- func setStyle(_ style: CPDFBorderStyle) {
- if isSelectAnnot {
- for annotation in annotations {
- let oldBorder = annotation.border
- var dashPattern: [CGFloat] = []
- if style == .dashed {
- dashPattern.append(5)
- }
- let border = CPDFBorder(style: style, lineWidth: oldBorder?.lineWidth ?? 0, dashPattern: dashPattern)
- annotation.border = border
-
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch annotMode {
- case .freeText:
- userDefaults.set(style.rawValue, forKey: CFreeTextNoteLineStyleKey)
- case .circle:
- userDefaults.set(style.rawValue, forKey: CCircleNoteLineStyleKey)
- case .square:
- userDefaults.set(style.rawValue, forKey: CSquareNoteLineStyleKey)
- case .line:
- userDefaults.set(style.rawValue, forKey: CLineNoteLineStyleKey)
- case .arrow:
- userDefaults.set(style.rawValue, forKey: CArrowNoteLineStyleKey)
- case .ink:
- userDefaults.set(style.rawValue, forKey: CInkNoteLineStyleyKey)
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
-
- var dashPattern: [NSNumber] {
- if isSelectAnnot {
- if annotation?.border.style == .dashed {
- return annotation?.border.dashPattern as? [NSNumber] ?? [0]
- } else {
- return [0]
- }
- } else {
- let userDefaults = UserDefaults.standard
- var dashPattern = 0
- switch annotMode {
- case .freeText:
- dashPattern = userDefaults.integer(forKey: CFreeTextNoteDashPatternKey)
- case .circle:
- dashPattern = UserDefaults.standard.integer(forKey: CCircleNoteDashPatternKey)
- case .square: dashPattern = UserDefaults.standard.integer(forKey: CSquareNoteDashPatternKey)
- case .line: dashPattern = UserDefaults.standard.integer(forKey: CLineNoteDashPatternKey)
- case .arrow: dashPattern = UserDefaults.standard.integer(forKey: CArrowNoteDashPatternKey)
- case .ink: dashPattern = UserDefaults.standard.integer(forKey: CInkNoteDashPatternKey)
- default: break
-
- }
- if self.style != .dashed {
- dashPattern = 0
- }
- return [dashPattern] as [NSNumber]
- }
- }
-
- func setDashPattern(_ dashPatterns: [NSNumber]) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- let oldBorder = annotation.border
- let border = CPDFBorder(style: oldBorder?.style ?? .solid, lineWidth: oldBorder?.lineWidth ?? 0, dashPattern: dashPatterns)
- annotation.border = border
- }
- } else {
- let dashPattern = dashPatterns.first?.intValue ?? 0
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- userDefaults.set(dashPattern, forKey: CFreeTextNoteDashPatternKey)
- case .circle:
- userDefaults.set(dashPattern, forKey: CCircleNoteDashPatternKey )
- case .square:
- userDefaults.set(dashPattern, forKey: CSquareNoteDashPatternKey )
- case .line:
- userDefaults.set(dashPattern, forKey: CLineNoteDashPatternKey )
- case .arrow:
- userDefaults.set(dashPattern, forKey: CArrowNoteDashPatternKey )
- case .ink:
- userDefaults.set(dashPattern, forKey: CInkNoteDashPatternKey )
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
-
- var lineWidth: CGFloat {
- var zLineWidth: CGFloat = 0
- if self.isSelectAnnot {
- zLineWidth = self.annotation?.lineWidth ?? 0
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .circle:
- zLineWidth = CGFloat(userDefaults.float(forKey: CCircleNoteLineWidthKey))
- case .square:
- zLineWidth = CGFloat(userDefaults.float(forKey: CSquareNoteLineWidthKey ))
- case .line:
- zLineWidth = CGFloat(userDefaults.float(forKey: CLineNoteLineWidthKey ))
- case .arrow:
- zLineWidth = CGFloat(userDefaults.float(forKey: CArrowNoteLineWidthKey ))
- case .ink:
- zLineWidth = CPDFKitConfig.sharedInstance().freehandAnnotationBorderWidth()
- default:
- break
- }
- }
- return zLineWidth
- }
-
- func setLineWidth(_ lineWidth: CGFloat) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- annotation.borderWidth = lineWidth
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .circle:
- userDefaults.set(Float(lineWidth), forKey: CCircleNoteLineWidthKey )
- case .square:
- userDefaults.set(Float(lineWidth), forKey: CSquareNoteLineWidthKey )
- case .line:
- userDefaults.set(Float(lineWidth), forKey: CLineNoteLineWidthKey )
- case .arrow:
- userDefaults.set(Float(lineWidth), forKey: CArrowNoteLineWidthKey )
- case .ink:
- CPDFKitConfig.sharedInstance().setFreehandAnnotationBorderWidth(lineWidth)
- userDefaults.set(Float(lineWidth), forKey: CInkNoteLineWidthKey )
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
- var startLineStyle: CPDFLineStyle {
- var zstartLineStyle: CPDFLineStyle = .none
- if self.isSelectAnnot {
- if let lineAnnotation = self.annotation as? CPDFLineAnnotation {
- zstartLineStyle = lineAnnotation.startLineStyle
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .arrow:
- zstartLineStyle = CPDFLineStyle(rawValue: userDefaults.integer(forKey: CArrowNoteStartStyleKey)) ?? .none
- case .line:
- zstartLineStyle = CPDFLineStyle(rawValue: userDefaults.integer(forKey: CLineNoteStartStyleKey)) ?? .none
- default:
- break
- }
- }
- return zstartLineStyle
- }
-
- func setStartLineStyle(_ startLineStyle: CPDFLineStyle) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- if let lineAnnotation = annotation as? CPDFLineAnnotation {
- lineAnnotation.startLineStyle = startLineStyle
- }
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .arrow:
- userDefaults.set(startLineStyle.rawValue, forKey: CArrowNoteStartStyleKey)
- case .line:
- userDefaults.set(startLineStyle.rawValue, forKey: CLineNoteStartStyleKey)
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
- var endLineStyle: CPDFLineStyle {
- var zendLineStyle: CPDFLineStyle = .none
- if self.isSelectAnnot {
- if let lineAnnotation = self.annotation as? CPDFLineAnnotation {
- zendLineStyle = lineAnnotation.endLineStyle
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .arrow:
- zendLineStyle = CPDFLineStyle(rawValue: userDefaults.integer(forKey: CArrowNoteEndStyleKey)) ?? .none
- case .line:
- zendLineStyle = CPDFLineStyle(rawValue: userDefaults.integer(forKey: CLineNoteEndStyleKey)) ?? .none
- default:
- break
- }
- }
- return zendLineStyle
- }
-
- func setEndLineStyle(_ endLineStyle: CPDFLineStyle) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- if let lineAnnotation = annotation as? CPDFLineAnnotation {
- lineAnnotation.endLineStyle = endLineStyle
- }
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .arrow:
- userDefaults.set(endLineStyle.rawValue, forKey: CArrowNoteEndStyleKey)
- case .line:
- userDefaults.set(endLineStyle.rawValue, forKey: CLineNoteEndStyleKey)
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
-
- // MARK: - FreeText
- var fontColor: UIColor? {
-
- var zfontColor: UIColor? = nil
- if self.isSelectAnnot {
- if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
- zfontColor = freeTextAnnotation.fontColor
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- zfontColor = userDefaults.PDFListViewColorForKey(CFreeTextNoteFontColorKey)
- default:
- break
- }
- }
- return zfontColor
- }
-
- func setFontColor(_ fontColor: UIColor?) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
- var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
- fontColor?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- freeTextAnnotation.fontColor = UIColor(red: red, green: green, blue: blue, alpha: self.opacity)
- }
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- userDefaults.setPDFListViewColor(fontColor, forKey: CFreeTextNoteFontColorKey)
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
- var fontSize: CGFloat {
- var zfontSize: CGFloat = 11
- if self.isSelectAnnot {
- if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
- zfontSize = freeTextAnnotation.fontSize
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- zfontSize = CGFloat(userDefaults.float(forKey: CFreeTextNoteFontSizeKey))
- default:
- break
- }
- }
- return zfontSize
- }
-
- func setFontSize(_ fontSize: CGFloat) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
- let cFont = freeTextAnnotation.cFont
- freeTextAnnotation.fontSize = fontSize
-
- let appleFont = UIFont.init(name: CPDFFont.convertAppleFont(cFont ?? CPDFFont(familyName: "Helvetica", fontStyle: "")) ?? "Helvetica", size: fontSize)
- let attributes: [NSAttributedString.Key: Any] = [.font: appleFont ?? UIFont.systemFont(ofSize: 11.0)]
- let bounds = annotation.bounds
-
- let rect = freeTextAnnotation.contents.boundingRect(with: CGSize(width: bounds.size.width, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
- var newBounds = bounds
- newBounds.origin.y = bounds.maxY - rect.size.height
- newBounds.size.height = rect.size.height + 6
-
- annotation.bounds = newBounds
- }
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- userDefaults.set(Float(fontSize), forKey: CFreeTextNoteFontSizeKey)
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
-
- var newCFont: CPDFFont {
- var cfont: CPDFFont = CPDFFont.init(familyName: "Helvetica", fontStyle: "")
- if self.isSelectAnnot {
- if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
- cfont = freeTextAnnotation.cFont
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- let zfontName = userDefaults.string(forKey: CFreeTextNoteFontFamilyNameKey)
- let zfontStyle = userDefaults.string(forKey: CFreeTextNoteFontNewStyleKey)
- cfont = CPDFFont.init(familyName: zfontName ?? "Helvetica", fontStyle: zfontStyle ?? "")
- break
- default:
- break
- }
- }
- return cfont
- }
-
- func setNewCFont(_ font: CPDFFont) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
- freeTextAnnotation.cFont = font
- }
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- let styleName = font.styleName
- let familyName = font.familyName
- if styleName?.count != 0 {
- userDefaults.set(styleName, forKey: CFreeTextNoteFontNewStyleKey)
- } else {
- userDefaults.set("", forKey: CFreeTextNoteFontNewStyleKey)
- }
-
- if familyName.count != 0 {
- userDefaults.set(familyName, forKey: CFreeTextNoteFontFamilyNameKey)
- } else {
- userDefaults.set("Helvetica", forKey: CFreeTextNoteFontNewStyleKey)
- }
- break
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
-
- var alignment: NSTextAlignment {
-
- var zalignment: NSTextAlignment = .left
- if self.isSelectAnnot {
- if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
- zalignment = freeTextAnnotation.alignment
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- zalignment = NSTextAlignment(rawValue: userDefaults.integer(forKey: CFreeTextNoteAlignmentKey)) ?? .left
- default:
- break
- }
- }
- return zalignment
- }
- func setAlignment(_ alignment: NSTextAlignment) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
- freeTextAnnotation.alignment = alignment
- }
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .freeText:
- userDefaults.set(alignment.rawValue, forKey: CFreeTextNoteAlignmentKey)
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
-
- // MARK: - Circle&Square
- var interiorColor: UIColor? {
-
- var zinteriorColor: UIColor? = nil
- if self.isSelectAnnot {
- if(self.annotation is CPDFCircleAnnotation ) {
- guard let circleAnnotation = self.annotation as? CPDFCircleAnnotation else {
- return UIColor.clear
- }
- zinteriorColor = circleAnnotation.interiorColor
- } else if (self.annotation is CPDFSquareAnnotation) {
- let squareAnnotation:CPDFSquareAnnotation = self.annotation as! CPDFSquareAnnotation
- zinteriorColor = squareAnnotation.interiorColor
- } else if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
- zinteriorColor = freeTextAnnotation.color
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .circle:
- zinteriorColor = userDefaults.PDFListViewColorForKey(CCircleNoteInteriorColorKey)
- case .square:
- zinteriorColor = userDefaults.PDFListViewColorForKey( CSquareNoteInteriorColorKey)
- default:
- break
- }
- }
- return zinteriorColor
- }
-
- func setInteriorColor(_ interiorColor: UIColor?) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- if let circleAnnotation = annotation as? CPDFCircleAnnotation {
- circleAnnotation.interiorColor = interiorColor
- } else if let squareAnnotation = annotation as? CPDFSquareAnnotation {
- squareAnnotation.interiorColor = interiorColor
- } else if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
- freeTextAnnotation.color = interiorColor
- }
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .circle:
- userDefaults.setPDFListViewColor(interiorColor, forKey: CCircleNoteInteriorColorKey)
- case .square:
- userDefaults.setPDFListViewColor(interiorColor, forKey: CSquareNoteInteriorColorKey)
- default:
- break
- }
- }
- }
- var interiorOpacity: CGFloat {
- var zinteriorOpacity: CGFloat = 0
- if self.isSelectAnnot {
- if let circleAnnotation = annotation as? CPDFCircleAnnotation {
- zinteriorOpacity = circleAnnotation.interiorOpacity
- } else if let squareAnnotation = annotation as? CPDFSquareAnnotation {
- zinteriorOpacity = squareAnnotation.interiorOpacity
- } else {
- zinteriorOpacity = self.annotation?.opacity ?? 1.0
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .circle:
- zinteriorOpacity = CGFloat(userDefaults.float(forKey: CCircleNoteInteriorOpacityKey))
- case .square:
- zinteriorOpacity = CGFloat(userDefaults.float(forKey: CSquareNoteInteriorOpacityKey))
- default:
- break
- }
- }
- return zinteriorOpacity
- }
-
- func setInteriorOpacity(_ interiorOpacity: CGFloat) {
- if self.isSelectAnnot {
- for annotation in self.annotations {
- if let circleAnnotation = annotation as? CPDFCircleAnnotation {
- circleAnnotation.interiorOpacity = interiorOpacity
- } else if let squareAnnotation = annotation as? CPDFSquareAnnotation {
- squareAnnotation.interiorOpacity = interiorOpacity
- } else {
- annotation.opacity = interiorOpacity
- }
- }
- } else {
- let userDefaults = UserDefaults.standard
- switch self.annotMode {
- case .circle:
- userDefaults.set(Float(interiorOpacity), forKey: CCircleNoteInteriorOpacityKey)
- case .square:
- userDefaults.set(Float(interiorOpacity), forKey: CSquareNoteInteriorOpacityKey)
- default:
- break
- }
- userDefaults.synchronize()
- }
- }
-
- }
|