|
@@ -7,13 +7,46 @@
|
|
|
|
|
|
import Foundation
|
|
import Foundation
|
|
|
|
|
|
|
|
+let TEXT_MARGIN_X = 2.0
|
|
|
|
+let TEXT_MARGIN_Y = 2.0
|
|
|
|
+
|
|
|
|
+let SKToolTipWidthKey = "SKToolTipWidth"
|
|
|
|
+let SKToolTipHeightKey = "SKToolTipHeight"
|
|
|
|
+
|
|
@objc protocol KMImageToolTipContext: NSObjectProtocol {
|
|
@objc protocol KMImageToolTipContext: NSObjectProtocol {
|
|
func toolTipImage() -> NSImage?
|
|
func toolTipImage() -> NSImage?
|
|
}
|
|
}
|
|
|
|
|
|
extension NSAttributedString: KMImageToolTipContext {
|
|
extension NSAttributedString: KMImageToolTipContext {
|
|
|
|
+// func toolTipImage() -> NSImage? {
|
|
|
|
+// return nil
|
|
|
|
+// }
|
|
|
|
+
|
|
func toolTipImage() -> NSImage? {
|
|
func toolTipImage() -> NSImage? {
|
|
- return nil
|
|
|
|
|
|
+ let backgroundColor: NSColor = {
|
|
|
|
+ if #available(OSX 10.9, *) {
|
|
|
|
+ return NSColor(calibratedRed: 0.95, green: 0.95, blue: 0.95, alpha: 1.0)
|
|
|
|
+ } else {
|
|
|
|
+ return NSColor(calibratedRed: 1.0, green: 1.0, blue: 0.75, alpha: 1.0)
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ let width = UserDefaults.standard.double(forKey: SKToolTipWidthKey) - 2.0 * TEXT_MARGIN_X
|
|
|
|
+ let height = UserDefaults.standard.double(forKey: SKToolTipHeightKey) - 2.0 * TEXT_MARGIN_Y
|
|
|
|
+ var textRect = self.boundingRect(with: NSSize(width: width, height: height), options: .usesLineFragmentOrigin)
|
|
|
|
+
|
|
|
|
+ textRect.origin = NSPoint(x: TEXT_MARGIN_X, y: TEXT_MARGIN_Y)
|
|
|
|
+ textRect.size.height = min(textRect.height, height)
|
|
|
|
+ textRect = NSInsetRect(NSIntegralRect(textRect), -TEXT_MARGIN_X, -TEXT_MARGIN_Y)
|
|
|
|
+
|
|
|
|
+ let image = NSImage(size: textRect.size, flipped: false) { rect in
|
|
|
|
+ backgroundColor.setFill()
|
|
|
|
+ rect.fill()
|
|
|
|
+ self.draw(with: NSRect(x: TEXT_MARGIN_X, y: TEXT_MARGIN_Y, width: rect.width - 2 * TEXT_MARGIN_X, height: rect.height - 2 * TEXT_MARGIN_Y), options: .usesLineFragmentOrigin)
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return image
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -165,24 +198,38 @@ extension CPDFPage: KMImageToolTipContext {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-extension NSParagraphStyle{
|
|
|
|
- static var defaultClippingParagraphStyle: NSParagraphStyle {
|
|
|
|
- var style: NSParagraphStyle? = nil
|
|
|
|
- if style == nil {
|
|
|
|
- let tmpParagraphStyle = (NSParagraphStyle.default as! NSMutableParagraphStyle).mutableCopy() as! NSMutableParagraphStyle
|
|
|
|
- tmpParagraphStyle.lineBreakMode = .byClipping
|
|
|
|
- style = tmpParagraphStyle.copy() as? NSParagraphStyle
|
|
|
|
- }
|
|
|
|
- return style!
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- static var defaultTruncatingTailParagraphStyle: NSParagraphStyle {
|
|
|
|
- var style: NSParagraphStyle? = nil
|
|
|
|
- if style == nil {
|
|
|
|
- let tmpParagraphStyle = (NSParagraphStyle.default as! NSMutableParagraphStyle).mutableCopy() as! NSMutableParagraphStyle
|
|
|
|
- tmpParagraphStyle.lineBreakMode = .byTruncatingTail
|
|
|
|
- style = tmpParagraphStyle.copy() as? NSParagraphStyle
|
|
|
|
- }
|
|
|
|
- return style!
|
|
|
|
- }
|
|
|
|
|
|
+extension NSParagraphStyle {
|
|
|
|
+ static var defaultClippingParagraphStyle: NSParagraphStyle = {
|
|
|
|
+ let tmpParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
|
|
|
|
+ tmpParagraphStyle.lineBreakMode = .byClipping
|
|
|
|
+ return tmpParagraphStyle.copy() as! NSParagraphStyle
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ static var defaultTruncatingTailParagraphStyle: NSParagraphStyle = {
|
|
|
|
+ let tmpParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
|
|
|
|
+ tmpParagraphStyle.lineBreakMode = .byTruncatingTail
|
|
|
|
+ return tmpParagraphStyle.copy() as! NSParagraphStyle
|
|
|
|
+ }()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+//extension NSParagraphStyle{
|
|
|
|
+// static var defaultClippingParagraphStyle: NSParagraphStyle {
|
|
|
|
+// var style: NSParagraphStyle? = nil
|
|
|
|
+// if style == nil {
|
|
|
|
+// let tmpParagraphStyle = (NSParagraphStyle.default as! NSMutableParagraphStyle).mutableCopy() as! NSMutableParagraphStyle
|
|
|
|
+// tmpParagraphStyle.lineBreakMode = .byClipping
|
|
|
|
+// style = tmpParagraphStyle.copy() as? NSParagraphStyle
|
|
|
|
+// }
|
|
|
|
+// return style!
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// static var defaultTruncatingTailParagraphStyle: NSParagraphStyle {
|
|
|
|
+// var style: NSParagraphStyle? = nil
|
|
|
|
+// if style == nil {
|
|
|
|
+// let tmpParagraphStyle = (NSParagraphStyle.default as! NSMutableParagraphStyle).mutableCopy() as! NSMutableParagraphStyle
|
|
|
|
+// tmpParagraphStyle.lineBreakMode = .byTruncatingTail
|
|
|
|
+// style = tmpParagraphStyle.copy() as? NSParagraphStyle
|
|
|
|
+// }
|
|
|
|
+// return style!
|
|
|
|
+// }
|
|
|
|
+//}
|