diff --git a/Documentation/Changelog.md b/Documentation/Changelog.md index 83d5990..1908896 100644 --- a/Documentation/Changelog.md +++ b/Documentation/Changelog.md @@ -1,5 +1,11 @@ ## CHANGELOG +### 0.7.0 +- added text orientation to `TextPreferences`; +- `preferedFontSize` removed from `TextPreferences`; +- `flipUpsideDown` fixed; + + ### v0.6.2 - fixed bug during init process; - in `TextPreferences`, `flipUpsideDown` now default value is `true`; diff --git a/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS.xcodeproj/project.pbxproj b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS.xcodeproj/project.pbxproj index 3a83e27..c571940 100644 --- a/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS.xcodeproj/project.pbxproj +++ b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS.xcodeproj/project.pbxproj @@ -21,6 +21,8 @@ 2710568D248D53EF006C0181 /* Prize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2710568B248D53EF006C0181 /* Prize.swift */; }; 2710568F248D541B006C0181 /* Prize+SliceContentType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2710568E248D541B006C0181 /* Prize+SliceContentType.swift */; }; 2720252124A36FB700240E4E /* SwiftFortuneWheelConfiguration+RainbowColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2720252024A36FB700240E4E /* SwiftFortuneWheelConfiguration+RainbowColors.swift */; }; + 27F7D5B924A51C5D004BEE19 /* Example2ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F7D5B824A51C5D004BEE19 /* Example2ViewController.swift */; }; + 27F7D5BB24A51D01004BEE19 /* SwiftFortuneWheelConfiguration+Example2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F7D5BA24A51D01004BEE19 /* SwiftFortuneWheelConfiguration+Example2.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -53,6 +55,8 @@ 2710568B248D53EF006C0181 /* Prize.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Prize.swift; sourceTree = ""; }; 2710568E248D541B006C0181 /* Prize+SliceContentType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Prize+SliceContentType.swift"; sourceTree = ""; }; 2720252024A36FB700240E4E /* SwiftFortuneWheelConfiguration+RainbowColors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SwiftFortuneWheelConfiguration+RainbowColors.swift"; sourceTree = ""; }; + 27F7D5B824A51C5D004BEE19 /* Example2ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Example2ViewController.swift; sourceTree = ""; }; + 27F7D5BA24A51D01004BEE19 /* SwiftFortuneWheelConfiguration+Example2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SwiftFortuneWheelConfiguration+Example2.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -124,6 +128,8 @@ 2720251F24A36F8900240E4E /* Example2ViewController */ = { isa = PBXGroup; children = ( + 27F7D5B824A51C5D004BEE19 /* Example2ViewController.swift */, + 27F7D5BA24A51D01004BEE19 /* SwiftFortuneWheelConfiguration+Example2.swift */, ); path = Example2ViewController; sourceTree = ""; @@ -202,8 +208,10 @@ files = ( 2710568D248D53EF006C0181 /* Prize.swift in Sources */, 2703B4F6248E2A9500FB50F9 /* UIView+CornerRadius.swift in Sources */, + 27F7D5B924A51C5D004BEE19 /* Example2ViewController.swift in Sources */, 2710568F248D541B006C0181 /* Prize+SliceContentType.swift in Sources */, 27105687248D4E49006C0181 /* ExamplesViewController.swift in Sources */, + 27F7D5BB24A51D01004BEE19 /* SwiftFortuneWheelConfiguration+Example2.swift in Sources */, 2710562E248D3C9D006C0181 /* AppDelegate.swift in Sources */, 27105630248D3C9D006C0181 /* SceneDelegate.swift in Sources */, 2720252124A36FB700240E4E /* SwiftFortuneWheelConfiguration+RainbowColors.swift in Sources */, diff --git a/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Base.lproj/Main.storyboard b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Base.lproj/Main.storyboard index 7bc7f03..f84954d 100644 --- a/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Base.lproj/Main.storyboard +++ b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Base.lproj/Main.storyboard @@ -43,6 +43,31 @@ + + + + + + + + + + + + + + + + + + + + @@ -267,6 +292,22 @@ + + + + + + + + + + + + + + + + diff --git a/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Example2ViewController/Example2ViewController.swift b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Example2ViewController/Example2ViewController.swift new file mode 100644 index 0000000..7982e09 --- /dev/null +++ b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Example2ViewController/Example2ViewController.swift @@ -0,0 +1,52 @@ +// +// Example2ViewController.swift +// SwiftFortuneWheelDemoiOS +// +// Created by Sherzod Khashimov on 6/25/20. +// Copyright © 2020 Sherzod Khashimov. All rights reserved. +// + +import UIKit +import SwiftFortuneWheel + +class Example2ViewController: UIViewController { + + lazy var fortuneWheel: SwiftFortuneWheel = { + var slices: [Slice] = [] + + for index in 1...4 { + let headerContent = Slice.ContentType.text(text: "\(index)", preferenes: .example2AmountTextPreferences) + let descriptionContent = Slice.ContentType.text(text: "DESCRIPTION", preferenes: .example2DescriptionTextPreferences) + let slice = Slice(contents: [headerContent, descriptionContent]) + slices.append(slice) + } + + let frame = CGRect(x: 35, y: 100, width: 300, height: 300) + + let fortuneWheel = SwiftFortuneWheel(frame: frame, slices: slices, configuration: .example2Configuration) + + fortuneWheel.isPinHidden = true + fortuneWheel.isSpinHidden = true + return fortuneWheel + }() + + override func viewDidLoad() { + super.viewDidLoad() + + self.title = "Example2" + + view.addSubview(fortuneWheel) + layoutWheel() + + } + + func layoutWheel() { + guard let superview = fortuneWheel.superview else { return } + fortuneWheel.translatesAutoresizingMaskIntoConstraints = false + fortuneWheel.widthAnchor.constraint(equalToConstant: 300).isActive = true + fortuneWheel.heightAnchor.constraint(equalToConstant: 300).isActive = true + fortuneWheel.centerYAnchor.constraint(equalTo: superview.centerYAnchor).isActive = true + fortuneWheel.centerXAnchor.constraint(equalTo: superview.centerXAnchor).isActive = true + } + +} diff --git a/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Example2ViewController/SwiftFortuneWheelConfiguration+Example2.swift b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Example2ViewController/SwiftFortuneWheelConfiguration+Example2.swift new file mode 100644 index 0000000..1de314b --- /dev/null +++ b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/Example2ViewController/SwiftFortuneWheelConfiguration+Example2.swift @@ -0,0 +1,95 @@ +// +// SwiftFortuneWheelConfiguration+Example2.swift +// SwiftFortuneWheelDemoiOS +// +// Created by Sherzod Khashimov on 6/25/20. +// Copyright © 2020 Sherzod Khashimov. All rights reserved. +// + +import Foundation +import UIKit +import SwiftFortuneWheel + +private let circleStrokeWidth: CGFloat = 2 +private let blackColor = UIColor(white: 51.0 / 255.0, alpha: 1.0) +private let redColor = UIColor.red + +public extension SwiftFortuneWheelConfiguration { + static var example2Configuration: SwiftFortuneWheelConfiguration { + let configuration = SwiftFortuneWheelConfiguration(spinButtonPreferences: .example2SpinButtonPreferences, + pinPreferences: .example2PinPreferences, + wheelPreferences: .example2WheelPreferences) + + return configuration + } +} + +public extension SwiftFortuneWheelConfiguration.SpinButtonPreferences { + static var example2SpinButtonPreferences: SwiftFortuneWheelConfiguration.SpinButtonPreferences { + let preferences = SwiftFortuneWheelConfiguration.SpinButtonPreferences(size: CGSize(width: 80, height: 80), + cornerRadius: 40, + textColor: .white, + font: .systemFont(ofSize: 20, weight: .bold), + backgroundColor: blackColor) + return preferences + } +} + +public extension SwiftFortuneWheelConfiguration.PinImageViewPreferences { + static var example2PinPreferences: SwiftFortuneWheelConfiguration.PinImageViewPreferences { + let preferences = SwiftFortuneWheelConfiguration.PinImageViewPreferences(size: CGSize(width: 12, height: 24), + position: .top, + verticalOffset: 30) + return preferences + } +} + +public extension SwiftFortuneWheelConfiguration.WheelPreferences { + static var example2WheelPreferences: SwiftFortuneWheelConfiguration.WheelPreferences { + let preferences = SwiftFortuneWheelConfiguration.WheelPreferences(circlePreferences: .example2CirclePreferences, + slicePreferences: .example2SlicePreferenes) + return preferences + } +} + +public extension SwiftFortuneWheelConfiguration.CirclePreferences { + static var example2CirclePreferences: SwiftFortuneWheelConfiguration.CirclePreferences { + let preferences = SwiftFortuneWheelConfiguration.CirclePreferences(strokeWidth: circleStrokeWidth, + strokeColor: blackColor) + return preferences + } +} + +public extension SwiftFortuneWheelConfiguration.SlicePreferences { + static var example2SlicePreferenes: SwiftFortuneWheelConfiguration.SlicePreferences { + let backgroundColorType = SwiftFortuneWheelConfiguration.ColorType.evenOddColors(evenColor: blackColor, oddColor: redColor) + let preferences = SwiftFortuneWheelConfiguration.SlicePreferences(backgroundColorType: backgroundColorType, + strokeWidth: 1, + strokeColor: blackColor) + return preferences + } +} + +public extension TextPreferences { + static var example2AmountTextPreferences: TextPreferences { + let textColorType = SwiftFortuneWheelConfiguration.ColorType.customPatternColors(colors: nil, defaultColor: .white) + let font = UIFont.systemFont(ofSize: 22, weight: .bold) + let prefenreces = TextPreferences(textColorType: textColorType, + font: font, + verticalOffset: 10, + isCurved: true) + return prefenreces + } + + static var example2DescriptionTextPreferences: TextPreferences { + let textColorType = SwiftFortuneWheelConfiguration.ColorType.customPatternColors(colors: nil, defaultColor: .white) + let font = UIFont.systemFont(ofSize: 12, weight: .bold) + let prefenreces = TextPreferences(textColorType: textColorType, + font: font, + verticalOffset: 10, + orientation: .vertical, + flipUpsideDown: false, + isCurved: false) + return prefenreces + } +} diff --git a/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/ExampleViewController/SwiftFortuneWheelConfiguration+Example.swift b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/ExampleViewController/SwiftFortuneWheelConfiguration+Example.swift index 4f88453..40d8d1c 100644 --- a/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/ExampleViewController/SwiftFortuneWheelConfiguration+Example.swift +++ b/Examples/SwiftFortuneWheelDemoiOS/SwiftFortuneWheelDemoiOS/ExampleViewController/SwiftFortuneWheelConfiguration+Example.swift @@ -91,7 +91,6 @@ extension TextPreferences { let font = UIFont.systemFont(ofSize: 20, weight: .bold) let prefenreces = TextPreferences(textColorType: textColorType, font: font, - preferedFontSize: 20, verticalOffset: 25, flipUpsideDown: true, isCurved: true) @@ -103,7 +102,6 @@ extension TextPreferences { let font = UIFont.systemFont(ofSize: 10, weight: .bold) let prefenreces = TextPreferences(textColorType: textColorType, font: font, - preferedFontSize: 10, verticalOffset: 10, orientation: .horizontal, flipUpsideDown: true, @@ -117,7 +115,6 @@ extension TextPreferences { let font = UIFont.systemFont(ofSize: 20, weight: .bold) let prefenreces = TextPreferences(textColorType: textColorType, font: font, - preferedFontSize: 20, verticalOffset: 25, flipUpsideDown: true, isCurved: true) @@ -129,7 +126,6 @@ extension TextPreferences { let font = UIFont.systemFont(ofSize: 10, weight: .bold) let prefenreces = TextPreferences(textColorType: textColorType, font: font, - preferedFontSize: 10, verticalOffset: 10, orientation: .horizontal, flipUpsideDown: true, diff --git a/Playgrounds/MyPlayground.playground/Contents.swift b/Playgrounds/MyPlayground.playground/Contents.swift index f1eca31..111c016 100644 --- a/Playgrounds/MyPlayground.playground/Contents.swift +++ b/Playgrounds/MyPlayground.playground/Contents.swift @@ -13,7 +13,7 @@ class MyViewController : UIViewController { for index in 1...4 { let headerContent = Slice.ContentType.text(text: "\(index)", preferenes: .amountTextPreferences) - let descriptionContent = Slice.ContentType.text(text: "Description", preferenes: .descriptionTextPreferences) + let descriptionContent = Slice.ContentType.text(text: "DESCRIPTION", preferenes: .descriptionTextPreferences) let slice = Slice(contents: [headerContent, descriptionContent]) slices.append(slice) } diff --git a/Playgrounds/MyPlayground.playground/Sources/SwiftFortuneWheelConfiguration+Default.swift b/Playgrounds/MyPlayground.playground/Sources/SwiftFortuneWheelConfiguration+Default.swift index 1c5ab17..0d7b99f 100644 --- a/Playgrounds/MyPlayground.playground/Sources/SwiftFortuneWheelConfiguration+Default.swift +++ b/Playgrounds/MyPlayground.playground/Sources/SwiftFortuneWheelConfiguration+Default.swift @@ -78,9 +78,7 @@ public extension TextPreferences { let font = UIFont.systemFont(ofSize: 22, weight: .bold) let prefenreces = TextPreferences(textColorType: textColorType, font: font, - preferedFontSize: 20, verticalOffset: 10, - flipUpsideDown: true, isCurved: true) return prefenreces } @@ -90,10 +88,9 @@ public extension TextPreferences { let font = UIFont.systemFont(ofSize: 12, weight: .bold) let prefenreces = TextPreferences(textColorType: textColorType, font: font, - preferedFontSize: 10, - verticalOffset: 3, - flipUpsideDown: true, - isCurved: true) + verticalOffset: 10, + orientation: .vertical, + isCurved: false) return prefenreces } } diff --git a/README.md b/README.md index 6764e50..566373a 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,20 @@ Fortune spinning wheel that supports dynamic content and rich customization. - - | | Main Features | |---|---| -| 🏵 | Dynamic content, support image, and text | +| 🏵 | Dynamic content, supports texts, images, and lines | +| 🎛 | Supports vertical and horizontal text orientation | | 🌈 | Appearance customization | | 🎨 | Drawn and animated using CoreGraphics | | 🧮 | Dynamic layout | | 🚀 | Written in Swift | +## Preview + + + + ## Getting Started - [**Getting started guide**](/Documentation/GettingStarted.md) @@ -33,7 +37,7 @@ You can find UIColor { + var strokeColor = UIColor.clear + + switch self.colorType { + case .evenOddColors(let evenColor, let oddColor): + strokeColor = index % 2 == 0 ? evenColor : oddColor + case .customPatternColors(let colors, let defaultColor): + strokeColor = colors?[index, default: defaultColor] ?? defaultColor + } + return strokeColor + } +} diff --git a/Sources/SwiftFortuneWheel/Configuration/TextPreferences.swift b/Sources/SwiftFortuneWheel/Configuration/TextPreferences.swift index 7b968b4..e15fdf0 100644 --- a/Sources/SwiftFortuneWheel/Configuration/TextPreferences.swift +++ b/Sources/SwiftFortuneWheel/Configuration/TextPreferences.swift @@ -24,9 +24,6 @@ public struct TextPreferences { /// Text font public var font: UIFont - /// Prefered font size, requared to calculate a size - public var preferedFontSize: CGFloat - /// Text color type public var textColorType: SwiftFortuneWheelConfiguration.ColorType @@ -47,7 +44,6 @@ public struct TextPreferences { /// - isCurved: Is text curved or not, works only with orientation equal to horizontal, default value is `true` public init(textColorType: SwiftFortuneWheelConfiguration.ColorType, font: UIFont, - preferedFontSize: CGFloat, verticalOffset: CGFloat = 0, horizontalOffset: CGFloat = 0, orientation: Orientation = .horizontal, @@ -58,7 +54,6 @@ public struct TextPreferences { self.verticalOffset = verticalOffset self.flipUpsideDown = flipUpsideDown self.font = font - self.preferedFontSize = preferedFontSize self.isCurved = isCurved self.orientation = orientation } @@ -71,3 +66,29 @@ public extension TextPreferences { case vertical } } + +extension TextPreferences { + func color(for index: Int) -> UIColor { + var color: UIColor = .clear + + switch self.textColorType { + case .evenOddColors(let evenColor, let oddColor): + color = index % 2 == 0 ? evenColor : oddColor + case .customPatternColors(let colors, let defaultColor): + color = colors?[index, default: defaultColor] ?? defaultColor + } + return color + } + + func textFontAttributes(for index: Int) -> [NSAttributedString.Key:Any] { + let textColor = self.color(for: index) + + let textStyle = NSMutableParagraphStyle() + textStyle.alignment = .center + let deafultAttributes:[NSAttributedString.Key: Any] = + [.font: self.font, + .foregroundColor: textColor, + .paragraphStyle: textStyle ] + return deafultAttributes + } +} diff --git a/Sources/SwiftFortuneWheel/Utils/Drawing/SliceDrawing.swift b/Sources/SwiftFortuneWheel/Utils/Drawing/SliceDrawing.swift index 7ee1796..936a8a7 100644 --- a/Sources/SwiftFortuneWheel/Utils/Drawing/SliceDrawing.swift +++ b/Sources/SwiftFortuneWheel/Utils/Drawing/SliceDrawing.swift @@ -189,16 +189,13 @@ extension SliceDrawing { switch preferences.orientation { case .horizontal: if preferences.isCurved { - self.drawCurved(text: text, in: context, preferences: preferences, rotation: rotation, index: index, topOffset: topOffset) + return self.drawCurved(text: text, in: context, preferences: preferences, rotation: rotation, index: index, topOffset: topOffset) } else { - self.drawHorizontal(text: text, in: context, preferences: preferences, rotation: rotation, index: index, topOffset: topOffset) + return self.drawHorizontal(text: text, in: context, preferences: preferences, rotation: rotation, index: index, topOffset: topOffset) } case .vertical: - // Not finished yet -// self.drawVertical(text: text, in: context, preferences: preferences, rotation: rotation, index: index, topOffset: topOffset) - self.drawHorizontal(text: text, in: context, preferences: preferences, rotation: rotation, index: index, topOffset: topOffset) + return self.drawVertical(text: text, in: context, preferences: preferences, rotation: rotation, index: index, topOffset: topOffset) } - return preferences.preferedFontSize + preferences.verticalOffset } /// Draws curved text @@ -209,20 +206,13 @@ extension SliceDrawing { /// - rotation: rotation degree /// - index: index /// - topOffset: top offset - private func drawCurved(text: String, in context:CGContext, preferences: TextPreferences, rotation: CGFloat, index: Int, topOffset: CGFloat) { + private func drawCurved(text: String, in context:CGContext, preferences: TextPreferences, rotation: CGFloat, index: Int, topOffset: CGFloat) -> CGFloat { - var textColor: UIColor = .black + let textColor = preferences.color(for: index) - switch preferences.textColorType { - case .evenOddColors(let evenColor, let oddColor): - textColor = index % 2 == 0 ? evenColor : oddColor - case .customPatternColors(let colors, let defaultColor): - textColor = colors?[index, default: defaultColor] ?? defaultColor - } - - let bottomYPosition = -(radius - preferences.verticalOffset - topOffset - preferences.preferedFontSize) + let bottomYPosition = -(radius - preferences.verticalOffset - topOffset - preferences.font.pointSize) let width = self.width(forYPosition: bottomYPosition) - let textRect = CGRect(x: 0, y: 0, width: width, height: preferences.preferedFontSize) + let textRect = CGRect(x: 0, y: 0, width: width, height: preferences.font.pointSize) let yPosition = -(radius - preferences.verticalOffset - topOffset - topMargin) + textRect.height / 2 context.saveGState() @@ -233,6 +223,8 @@ extension SliceDrawing { context.restoreGState() + return preferences.font.pointSize + preferences.verticalOffset + } /// Draws text @@ -243,43 +235,32 @@ extension SliceDrawing { /// - rotation: rotation degree /// - index: index /// - topOffset: top offset - private func drawHorizontal(text: String, in context:CGContext, preferences: TextPreferences, rotation: CGFloat, index: Int, topOffset: CGFloat) { + private func drawHorizontal(text: String, in context:CGContext, preferences: TextPreferences, rotation: CGFloat, index: Int, topOffset: CGFloat) -> CGFloat { - var textColor: UIColor = .black + let textFontAttributes = preferences.textFontAttributes(for: index) - switch preferences.textColorType { - case .evenOddColors(let evenColor, let oddColor): - textColor = index % 2 == 0 ? evenColor : oddColor - case .customPatternColors(let colors, let defaultColor): - textColor = colors?[index, default: defaultColor] ?? defaultColor - } - - let textFontAttributes: [NSAttributedString.Key:Any] = { - let textStyle = NSMutableParagraphStyle() - textStyle.alignment = .center - let deafultAttributes:[NSAttributedString.Key: Any] = - [.font: preferences.font, - .foregroundColor: textColor, - .paragraphStyle: textStyle ] - return deafultAttributes - }() - - let bottomYPosition = -(radius - preferences.verticalOffset - topOffset - preferences.preferedFontSize) + let bottomYPosition = -(radius - preferences.verticalOffset - topOffset - preferences.font.pointSize) let width = self.width(forYPosition: bottomYPosition) - let textRect = CGRect(x: 0, y: 0, width: width, height: preferences.preferedFontSize) + let textRect = CGRect(x: 0, y: 0, width: width, height: preferences.font.pointSize) let yPosition = -(radius - preferences.verticalOffset - topOffset - topMargin) + let xPos = -(textRect.width / 2) - preferences.horizontalOffset context.saveGState() context.rotate(by: rotation * CGFloat.pi/180) - if preferences.flipUpsideDown { + context.translateBy(x: xPos, y: yPosition) + + if !preferences.flipUpsideDown { context.rotate(by: flipRotation) + context.translateBy(x: -textRect.width, y: -textRect.height) } - text.draw(in: CGRect(x: -(textRect.width / 2) - preferences.horizontalOffset, y: yPosition, width: textRect.width, height: textRect.height), withAttributes: textFontAttributes) + text.draw(in: CGRect(x: 0, y: 0, width: textRect.width, height: textRect.height), withAttributes: textFontAttributes) context.restoreGState() + return preferences.font.pointSize + preferences.verticalOffset + } /// Draws text @@ -290,62 +271,47 @@ extension SliceDrawing { /// - rotation: rotation degree /// - index: index /// - topOffset: top offset - private func drawVertical(text: String, in context: CGContext, preferences: TextPreferences, rotation: CGFloat, index: Int, topOffset: CGFloat) { - - var textColor: UIColor = .black - - switch preferences.textColorType { - case .evenOddColors(let evenColor, let oddColor): - textColor = index % 2 == 0 ? evenColor : oddColor - case .customPatternColors(let colors, let defaultColor): - textColor = colors?[index, default: defaultColor] ?? defaultColor - } + private func drawVertical(text: String, in context: CGContext, preferences: TextPreferences, rotation: CGFloat, index: Int, topOffset: CGFloat) -> CGFloat { - let textFontAttributes: [NSAttributedString.Key:Any] = { - let textStyle = NSMutableParagraphStyle() - textStyle.alignment = .center - let deafultAttributes:[NSAttributedString.Key: Any] = - [.font: preferences.font, - .foregroundColor: textColor, - .paragraphStyle: textStyle ] - return deafultAttributes - }() + let textFontAttributes = preferences.textFontAttributes(for: index) let textWidth: CGFloat = text.width(by: preferences.font) - let maxAvailableBottomRadiusOffsetCircularSegmentHeight = self.leftMargin + self.rightMargin + preferences.preferedFontSize + let maxAvailableBottomRadiusOffsetCircularSegmentHeight = self.leftMargin + self.rightMargin + preferences.font.pointSize let bottomRadiusOffset = max(self.bottomMargin, radius(circularSegmentHeight: maxAvailableBottomRadiusOffsetCircularSegmentHeight, from: sliceDegree)) let availableHeightInSlice: CGFloat = radius - preferences.verticalOffset - topOffset - bottomRadiusOffset let textRectWidth = min(textWidth, availableHeightInSlice) let croppedText = text.crop(by: textRectWidth, font: preferences.font) -// let bottomYPosition = -(radius - preferences.verticalOffset - topOffset - preferences.preferedFontSize) - let textRect = CGRect(x: 0, y: 0, width: textRectWidth, height: preferences.preferedFontSize) + let textRect = CGRect(x: 0, y: 0, width: textRectWidth, height: preferences.font.pointSize) let yPosition = -(radius - preferences.verticalOffset - topOffset - topMargin) - let xPos = -(textRect.width / 2) - preferences.horizontalOffset + let xPos = -(textRect.height / 2) - preferences.horizontalOffset context.saveGState() context.rotate(by: rotation * CGFloat.pi/180) - context.translateBy(x: xPos + textRect.height / 2, y: yPosition + textRect.width / 2) - context.rotate(by: 90 * CGFloat.pi/180) + context.translateBy(x: -xPos, y: yPosition) + context.rotate(by: 90 * CGFloat.pi/180) -// context.rotate(by: 90 * CGFloat.pi/180) if preferences.flipUpsideDown { context.rotate(by: flipRotation) + context.translateBy(x: -textRectWidth, y: -textRect.height) } -// croppedText.draw(in: CGRect(x: 0, y: 0, width: textRect.width, height: textRect.height), withAttributes: textFontAttributes) -// croppedText.draw(in: CGRect(x: -(textRect.width / 2) - preferences.horizontalOffset - rotationOffset / 2, y: yPosition - rotationOffset / 2 , width: textRect.width, height: textRect.height), withAttributes: textFontAttributes) -// context.addRect(textRect) -// UIColor.red.setStroke() -// context.drawPath(using: .fillStroke) - croppedText.draw(in: CGRect(x: -textRect.width / 2, y: -textRect.height / 2, width: textRect.width, height: textRect.height), withAttributes: textFontAttributes) + + // For Debugging purposes + // context.addRect(textRect) + // UIColor.red.setStroke() + // context.drawPath(using: .fillStroke) + + croppedText.draw(in: CGRect(x: 0, y: 0, width: textRect.width, height: textRect.height), withAttributes: textFontAttributes) context.restoreGState() + return textRectWidth + preferences.verticalOffset + } } @@ -377,18 +343,11 @@ extension SliceDrawing { /// - topOffset: top offset func drawLine(in context: CGContext, preferences: LinePreferences, start: CGFloat, and end: CGFloat, rotation: CGFloat, index: Int, topOffset: CGFloat) { + let strokeColor = preferences.strokeColor(for: index) + context.saveGState() context.rotate(by: (rotation - contextPositionCorrectionOffsetDegree) * CGFloat.pi/180) - var strokeColor = UIColor.clear - - switch preferences.colorType { - case .evenOddColors(let evenColor, let oddColor): - strokeColor = index % 2 == 0 ? evenColor : oddColor - case .customPatternColors(let colors, let defaultColor): - strokeColor = colors?[index, default: defaultColor] ?? defaultColor - } - let strokeWidth = preferences.height let yPosition = radius - preferences.verticalOffset - topOffset diff --git a/SwiftFortuneWheel.podspec b/SwiftFortuneWheel.podspec index 61dd3c1..34c1d9a 100644 --- a/SwiftFortuneWheel.podspec +++ b/SwiftFortuneWheel.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "SwiftFortuneWheel" - s.version = "0.6.2" + s.version = "0.7.0" s.summary = "Fortune spinning wheel that supports dynamic content and rich customization." s.description = <<-DESC Fortune spinning wheel that supports dynamic content and rich customization. Main Features: Dynamic content, support image, and text; Appearance customization; Drawn and animated using CoreGraphics; Dynamic layout.