diff --git a/Example Apps/SPIndicator.xcodeproj/project.xcworkspace/xcuserdata/ivanvorobei.xcuserdatad/UserInterfaceState.xcuserstate b/Example Apps/SPIndicator.xcodeproj/project.xcworkspace/xcuserdata/ivanvorobei.xcuserdatad/UserInterfaceState.xcuserstate index 09381e1..3eba3c6 100644 Binary files a/Example Apps/SPIndicator.xcodeproj/project.xcworkspace/xcuserdata/ivanvorobei.xcuserdatad/UserInterfaceState.xcuserstate and b/Example Apps/SPIndicator.xcodeproj/project.xcworkspace/xcuserdata/ivanvorobei.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Example Apps/SPIndicator.xcodeproj/xcuserdata/ivanvorobei.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Example Apps/SPIndicator.xcodeproj/xcuserdata/ivanvorobei.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..ccca103 --- /dev/null +++ b/Example Apps/SPIndicator.xcodeproj/xcuserdata/ivanvorobei.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,6 @@ + + + diff --git a/Example Apps/iOS Example/Scenes/PresetsController.swift b/Example Apps/iOS Example/Scenes/PresetsController.swift index f3435a4..61f19c9 100644 --- a/Example Apps/iOS Example/Scenes/PresetsController.swift +++ b/Example Apps/iOS Example/Scenes/PresetsController.swift @@ -61,10 +61,10 @@ class PresetsController: SPDiffableTableController { .init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), .init(systemItem: .play, primaryAction: .init(handler: { [weak self] (action) in guard let preset = self?.currentPreset else { return } - if preset.message == nil && preset.preset == nil { - SPIndicator.present(message: preset.title, haptic: .success) + if let iconPreset = preset.preset { + SPIndicator.present(title: preset.title, message: preset.message, preset: iconPreset, completion: nil) } else { - SPIndicator.present(title: preset.title, message: preset.message, preset: preset.preset!, completion: nil) + SPIndicator.present(title: preset.title, message: preset.message, haptic: .success) } }), menu: nil), @@ -106,6 +106,24 @@ class PresetsController: SPDiffableTableController { message: nil, preset: nil ), + IndicatorPresetModel( + name: "Message with Subtitle", + title: "Title", + message: "Subtitle", + preset: nil + ), + IndicatorPresetModel( + name: "Message Large Text with Icon", + title: "You can read it later when you have time. Saved to Bookmarks", + message: nil, + preset: .custom(UIImage.init(systemName: "envelope.open.fill")!) + ), + IndicatorPresetModel( + name: "Message Large Text without Icon", + title: "You can read it later when you have time. Saved to Bookmarks", + message: nil, + preset: nil + ) ] } diff --git a/README.md b/README.md index f42adef..b483433 100644 --- a/README.md +++ b/README.md @@ -77,9 +77,9 @@ SPIndicator.present(title: "Custom Image", message: "With tint color", preset: . For showing a simple text message: ```swift -SPIndicator.present(message: "Error", haptic: .error) +SPIndicator.present(title: "Error", haptic: .error) ``` -Here requerid haptic for better user expierense. +You can provide message optional too. ## Usage diff --git a/SPIndicator.podspec b/SPIndicator.podspec index 7a80f42..86b9484 100644 --- a/SPIndicator.podspec +++ b/SPIndicator.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'SPIndicator' - s.version = '1.0.2' + s.version = '1.0.4' s.summary = 'Floating indicator, mimicrate to indicator which appear when silent mode turn on / off. Support large texts.' s.homepage = 'https://github.com/ivanvorobei/SPIndicator' s.source = { :git => 'https://github.com/ivanvorobei/SPIndicator.git', :tag => s.version } diff --git a/Sources/SPIndicator/SPIndicator.swift b/Sources/SPIndicator/SPIndicator.swift index 6ff77d4..6f74e42 100644 --- a/Sources/SPIndicator/SPIndicator.swift +++ b/Sources/SPIndicator/SPIndicator.swift @@ -60,8 +60,8 @@ public enum SPIndicator { - parameter haptic: Haptic response with present. Default is `.success`. - parameter completion: Will call with dismiss alert. */ - public static func present(message: String, haptic: SPIndicatorHaptic, completion: (() -> Void)? = nil) { - let alertView = SPIndicatorView(message: message) + public static func present(title: String, message: String?, haptic: SPIndicatorHaptic, completion: (() -> Void)? = nil) { + let alertView = SPIndicatorView(title: title, message: message) alertView.present(haptic: haptic, completion: completion) } diff --git a/Sources/SPIndicator/SPIndicatorView.swift b/Sources/SPIndicator/SPIndicatorView.swift index 976d27d..b6d2de8 100644 --- a/Sources/SPIndicator/SPIndicatorView.swift +++ b/Sources/SPIndicator/SPIndicatorView.swift @@ -72,13 +72,16 @@ class SPIndicatorView: UIView { setIcon(for: preset) } - public init(message: String) { + public init(title: String, message: String?) { super.init(frame: CGRect.zero) titleAreaFactor = 1.8 minimumAreaWidth = 100 commonInit() layout = SPIndicatorLayout.message() - setTitle(message) + setTitle(title) + if let message = message { + setMessage(message) + } } public required init?(coder aDecoder: NSCoder) { @@ -293,7 +296,7 @@ class SPIndicatorView: UIView { private var titlesFullWidth: CGFloat { if let iconView = self.iconView { let space = iconView.frame.maxY + spaceBetweenTitlesAndImage - return frame.width - space - layoutMargins.right + return frame.width - space - layoutMargins.right - self.spaceBetweenTitlesAndImage } else { return frame.width - layoutMargins.left - layoutMargins.right } @@ -316,54 +319,148 @@ class SPIndicatorView: UIView { super.layoutSubviews() layoutMargins = layout.margins - layer.cornerRadius = frame.height / 2 backgroundView.frame = bounds backgroundView.layer.cornerRadius = layer.cornerRadius - if let iconView = self.iconView { - iconView.frame = .init( - origin: .init(x: layoutMargins.right, y: iconView.frame.origin.y), - size: layout.iconSize - ) - iconView.center.y = bounds.midY - } + // Flags - if let titleLabel = self.titleLabel { + let hasIcon = (self.iconView != nil) + let hasTitle = (self.titleLabel != nil) + let hasSubtite = (self.subtitleLabel != nil) + + let fitTitleToCompact: Bool = { + guard let titleLabel = self.titleLabel else { return true } + titleLabel.numberOfLines = 1 titleLabel.sizeToFit() - if titleLabel.frame.width > titlesCompactWidth { - titleLabel.textAlignment = UIApplication.shared.userInterfaceRightToLeft ? .right : .left - titleLabel.layoutDynamicHeight(width: titlesFullWidth) - titleLabel.frame.origin.x = layoutMargins.left + (iconView?.frame.width ?? 0) + spaceBetweenTitlesAndImage - } else { - titleLabel.textAlignment = .center - titleLabel.layoutDynamicHeight(width: titlesCompactWidth) - titleLabel.center.x = frame.width / 2 - } - } + return titleLabel.frame.width < titlesCompactWidth + }() - if let subtitleLabel = self.subtitleLabel { + let fitSubtitleToCompact: Bool = { + guard let subtitleLabel = self.subtitleLabel else { return true } + subtitleLabel.numberOfLines = 1 subtitleLabel.sizeToFit() - if subtitleLabel.frame.width > titlesCompactWidth { - subtitleLabel.textAlignment = UIApplication.shared.userInterfaceRightToLeft ? .right : .left - subtitleLabel.layoutDynamicHeight(width: titlesFullWidth) - subtitleLabel.frame.origin.x = layoutMargins.left + (iconView?.frame.width ?? 0) + spaceBetweenTitlesAndImage - } else { - subtitleLabel.textAlignment = .center - subtitleLabel.layoutDynamicHeight(width: titlesCompactWidth) - subtitleLabel.center.x = frame.width / 2 + return subtitleLabel.frame.width < titlesCompactWidth + }() + + let notFitAnyLabelToCompact: Bool = { + if !fitTitleToCompact { return true } + if !fitSubtitleToCompact { return true } + return false + }() + + var layout: LayoutGrid = .iconTitleCentered + + if (hasIcon && hasTitle && hasSubtite) && !notFitAnyLabelToCompact { + layout = .iconTitleMessageCentered + } + + if (hasIcon && hasTitle && hasSubtite) && notFitAnyLabelToCompact { + layout = .iconTitleMessageLeading + } + + if (hasIcon && hasTitle && !hasSubtite) && !notFitAnyLabelToCompact { + layout = .iconTitleCentered + } + + if (hasIcon && hasTitle && !hasSubtite) && notFitAnyLabelToCompact { + layout = .iconTitleLeading + } + + if (!hasIcon && hasTitle && !hasSubtite) { + layout = .title + } + + if (!hasIcon && hasTitle && hasSubtite) { + layout = .titleMessage + } + + // Actions + + let layoutIcon = { [weak self] in + guard let self = self else { return } + guard let iconView = self.iconView else { return } + iconView.frame = .init( + origin: .init(x: self.layoutMargins.left, y: iconView.frame.origin.y), + size: self.layout.iconSize + ) + iconView.center.y = self.bounds.midY + } + + let layoutTitleCenteredCompact = { [weak self] in + guard let self = self else { return } + guard let titleLabel = self.titleLabel else { return } + titleLabel.textAlignment = .center + titleLabel.layoutDynamicHeight(width: self.titlesCompactWidth) + titleLabel.center.x = self.frame.width / 2 + } + + let layoutTitleCenteredFullWidth = { [weak self] in + guard let self = self else { return } + guard let titleLabel = self.titleLabel else { return } + titleLabel.textAlignment = .center + titleLabel.layoutDynamicHeight(width: self.titlesFullWidth) + titleLabel.center.x = self.frame.width / 2 + } + + let layoutTitleLeadingFullWidth = { [weak self] in + guard let self = self else { return } + guard let titleLabel = self.titleLabel else { return } + guard let iconView = self.iconView else { return } + titleLabel.textAlignment = UIApplication.shared.userInterfaceRightToLeft ? .right : .left + titleLabel.layoutDynamicHeight(width: self.titlesFullWidth) + titleLabel.frame.origin.x = self.layoutMargins.left + iconView.frame.width + self.spaceBetweenTitlesAndImage + } + + let layoutSubtitle = { [weak self] in + guard let self = self else { return } + guard let titleLabel = self.titleLabel else { return } + guard let subtitleLabel = self.subtitleLabel else { return } + subtitleLabel.textAlignment = titleLabel.textAlignment + subtitleLabel.layoutDynamicHeight(width: titleLabel.frame.width) + subtitleLabel.frame.origin.x = titleLabel.frame.origin.x + } + + let layoutTitleSubtitleByVertical = { [weak self] in + guard let self = self else { return } + guard let titleLabel = self.titleLabel else { return } + guard let subtitleLabel = self.subtitleLabel else { + titleLabel.center.y = self.bounds.midY + return } + let allHeight = titleLabel.frame.height + subtitleLabel.frame.height + self.spaceBetweenTitles + titleLabel.frame.origin.y = (self.frame.height - allHeight) / 2 + subtitleLabel.frame.origin.y = titleLabel.frame.maxY + self.spaceBetweenTitles } - if (titleLabel != nil) && (subtitleLabel == nil) { - titleLabel?.center.y = bounds.midY + // Apply + + switch layout { + case .iconTitleMessageCentered: + layoutIcon() + layoutTitleCenteredCompact() + layoutSubtitle() + case .iconTitleMessageLeading: + layoutIcon() + layoutTitleLeadingFullWidth() + layoutSubtitle() + case .iconTitleCentered: + layoutIcon() + titleLabel?.numberOfLines = 2 + layoutTitleCenteredCompact() + case .iconTitleLeading: + layoutIcon() + titleLabel?.numberOfLines = 2 + layoutTitleLeadingFullWidth() + case .title: + titleLabel?.numberOfLines = 2 + layoutTitleCenteredFullWidth() + case .titleMessage: + layoutTitleCenteredFullWidth() + layoutSubtitle() } - if (titleLabel != nil) && (subtitleLabel != nil) { - let allHeight = (titleLabel?.frame.height ?? 0) + (subtitleLabel?.frame.height ?? 0) + spaceBetweenTitles - titleLabel?.frame.origin.y = (frame.height - allHeight) / 2 - subtitleLabel?.frame.origin.y = (titleLabel?.frame.maxY ?? 0) + spaceBetweenTitles - } + layoutTitleSubtitleByVertical() } // MARK: - Models @@ -374,6 +471,16 @@ class SPIndicatorView: UIView { case visible case fromVisible(_ translation: CGFloat) } + + enum LayoutGrid { + + case iconTitleMessageCentered + case iconTitleMessageLeading + case iconTitleCentered + case iconTitleLeading + case title + case titleMessage + } } #endif