Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 2 additions & 54 deletions Source/ArcTextLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,7 @@ public class ArcTextLayer: CALayer {
private func draw() {
var radAngle = angle.radians

#if os(OSX)
var textSize = CGSize.zero
if #available(OSX 10.11, *) {
textSize = text.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
.integral
.size
} else {
textSize = text.string.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: text.attributes(
at: 0,
effectiveRange: nil))
.integral
.size
}
#elseif os(iOS) || os(tvOS)
let textSize = text.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
.integral
.size
#endif
let textSize = text.boundingRect.integral.size

let perimeter: CGFloat = 2 * .pi * radius
let textAngle: CGFloat = textSize.width / perimeter * 2 * .pi
Expand Down Expand Up @@ -129,33 +103,7 @@ public class ArcTextLayer: CALayer {
for textLayer in textLayers {
let letter = textLayer.string as! NSAttributedString

#if os(OSX)
var charSize = CGSize.zero
if #available(OSX 10.11, *) {
charSize = letter.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
.integral
.size
} else {
charSize = letter.string.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: letter.attributes(
at: 0,
effectiveRange: nil))
.integral
.size
}
#elseif os(iOS) || os(tvOS)
let charSize = letter.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
.integral
.size
#endif
let charSize = letter.boundingRect.integral.size

let letterAngle = (charSize.width / perimeter) * textDirection
let x = radius * cos(radAngle + (letterAngle / 2))
Expand Down
39 changes: 12 additions & 27 deletions Source/CircleOfFifths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,18 @@ internal enum CircleChordType {
public class CircleOfFifths: CRView {
public var scale = Scale(type: .minor, key: .c) { didSet { redraw() }}

#if os(OSX)
@IBInspectable public var defaultColor: NSColor = .white { didSet { redraw() }}
@IBInspectable public var highlightedColor: NSColor = .red { didSet { redraw() }}
@IBInspectable public var disabledColor: NSColor = .lightGray { didSet { redraw() }}
@IBInspectable public var textColor: NSColor = .black { didSet { redraw() }}
@IBInspectable public var circlePieLineColor: NSColor = .black { didSet { redraw() }}
@IBInspectable public var majorColor: NSColor = .red { didSet { redraw() }}
@IBInspectable public var minorColor: NSColor = .blue { didSet { redraw() }}
@IBInspectable public var diminishedColor: NSColor = .green { didSet { redraw() }}
@IBInspectable public var majorTextColor: NSColor = .black { didSet { redraw() }}
@IBInspectable public var minorTextColor: NSColor = .black { didSet { redraw() }}
@IBInspectable public var diminishedTextColor: NSColor = .black { didSet { redraw() }}
@IBInspectable public var chordPieLineColor: NSColor = .black { didSet { redraw() }}
#elseif os(iOS) || os(tvOS)
@IBInspectable public var defaultColor: UIColor = .white { didSet { redraw() }}
@IBInspectable public var highlightedColor: UIColor = .red { didSet { redraw() }}
@IBInspectable public var disabledColor: UIColor = .lightGray { didSet { redraw() }}
@IBInspectable public var textColor: UIColor = .black { didSet { redraw() }}
@IBInspectable public var circlePieLineColor: UIColor = .black { didSet { redraw() }}
@IBInspectable public var majorColor: UIColor = .red { didSet { redraw() }}
@IBInspectable public var minorColor: UIColor = .blue { didSet { redraw() }}
@IBInspectable public var diminishedColor: UIColor = .green { didSet { redraw() }}
@IBInspectable public var majorTextColor: UIColor = .black { didSet { redraw() }}
@IBInspectable public var minorTextColor: UIColor = .black { didSet { redraw() }}
@IBInspectable public var diminishedTextColor: UIColor = .black { didSet { redraw() }}
@IBInspectable public var chordPieLineColor: UIColor = .black { didSet { redraw() }}
#endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ugly part of the code is actually necessary because of an interface builder bug. We need to do this in order to see these properties in the interface builder for both iOS and macOS.

@IBInspectable public var defaultColor: CRColor = .white { didSet { redraw() }}
@IBInspectable public var highlightedColor: CRColor = .red { didSet { redraw() }}
@IBInspectable public var disabledColor: CRColor = .lightGray { didSet { redraw() }}
@IBInspectable public var textColor: CRColor = .black { didSet { redraw() }}
@IBInspectable public var circlePieLineColor: CRColor = .black { didSet { redraw() }}
@IBInspectable public var majorColor: CRColor = .red { didSet { redraw() }}
@IBInspectable public var minorColor: CRColor = .blue { didSet { redraw() }}
@IBInspectable public var diminishedColor: CRColor = .green { didSet { redraw() }}
@IBInspectable public var majorTextColor: CRColor = .black { didSet { redraw() }}
@IBInspectable public var minorTextColor: CRColor = .black { didSet { redraw() }}
@IBInspectable public var diminishedTextColor: CRColor = .black { didSet { redraw() }}
@IBInspectable public var chordPieLineColor: CRColor = .black { didSet { redraw() }}

@IBInspectable public var chordPieLineWidth: CGFloat = 1 { didSet { redraw() }}
@IBInspectable public var circlePieLineWidth: CGFloat = 1 { didSet { redraw() }}
Expand Down
57 changes: 40 additions & 17 deletions Source/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,7 @@ import MusicTheorySwift
public typealias CRColor = NSColor
public typealias CRFont = NSFont
public typealias CRBezierPath = NSBezierPath
#elseif os(iOS) || os(tvOS)
public typealias CRView = UIView
public typealias CRColor = UIColor
public typealias CRFont = UIFont
public typealias CRBezierPath = UIBezierPath
#endif

// MARK: - CGFloat Extension

internal extension CGFloat {
var radians: CGFloat {
return self * .pi / 180.0
}
}

// MARK: - NSBezierPath Extension

#if os(OSX)
public extension NSBezierPath {
public var cgPath: CGPath {
let path = CGMutablePath()
Expand All @@ -60,8 +43,48 @@ internal extension CGFloat {
return path
}
}
#elseif os(iOS) || os(tvOS)
public typealias CRView = UIView
public typealias CRColor = UIColor
public typealias CRFont = UIFont
public typealias CRBezierPath = UIBezierPath
#endif

extension NSAttributedString {
var boundingRect : CGRect {
#if os(OSX)
if #available(OSX 10.11, *) {
return boundingRect(with: .max,
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)

} else {
return string.boundingRect(with: .max,
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: attributes(at: 0, effectiveRange: nil))

}
#elseif os(iOS) || os(tvOS)
return boundingRect(with: .max,
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
#endif
}
}

// MARK: - CGFloat Extension

internal extension CGFloat {
var radians: CGFloat {
return self * .pi / 180.0
}
}

extension CGSize {
static let max = CGSize(width: .max, height: .max)
}


// MARK: - ScaleType Extension

internal extension ScaleType {
Expand Down
32 changes: 1 addition & 31 deletions Source/PieChartLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,37 +161,7 @@ public class PieChartLayer: CAShapeLayer {
textLayer.position = CGPoint(x: x, y: y)

// text frame
if let att = slice.attributedString {
var textSize = CGSize.zero

#if os(OSX)
if #available(OSX 10.11, *) {
textSize = att.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
.size
} else {
textSize = att.string.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: att.attributes(
at: 0,
effectiveRange: nil))
.size
}
#elseif os(iOS) || os(tvOS)
textSize = att.boundingRect(
with: CGSize(width: .max, height: .max),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil)
.size
#endif

textLayer.frame.size = textSize
} else {
textLayer.frame.size = .zero
}
textLayer.frame.size = slice.attributedString?.boundingRect.size ?? .zero
}
}
}