-
Notifications
You must be signed in to change notification settings - Fork 24
Label ios14 #80
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
base: master
Are you sure you want to change the base?
Label ios14 #80
Changes from 8 commits
3922478
c003799
369cee4
679b9f3
3f7b303
05b440d
156cf1e
407e4db
be8a592
805fa89
3ec3ab7
73090c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // | ||
| // LebelExampleView.swift | ||
| // AltSwiftUIExample | ||
| // | ||
| // Created by Chan, Chengwei on 2021/05/24. | ||
| // Copyright © 2021 Rakuten Travel. All rights reserved. | ||
| // | ||
|
|
||
| import AltSwiftUI | ||
|
|
||
| struct LabelExampleView: View { | ||
| var viewStore = ViewValues() | ||
| var body: View { | ||
| VStack(spacing: 20) { | ||
| Label("Lamen", image: "icon") | ||
|
|
||
| Label { | ||
| Text("fullName") | ||
| .font(.body) | ||
| .foregroundColor(.primary) | ||
| Text("(nickName)") | ||
| .font(.body) | ||
| .foregroundColor(.primary) | ||
| } icon: { | ||
| Circle() | ||
| .fill(.blue) | ||
| .frame(width: 44, height: 44) | ||
| } | ||
|
|
||
| Label { | ||
| Text("fullName") | ||
| .font(.body) | ||
| .foregroundColor(.primary) | ||
| Text("(nickName)") | ||
| .font(.body) | ||
| .foregroundColor(.primary) | ||
| } icon: { | ||
| Circle() | ||
| .fill(.blue) | ||
| .frame(width: 44, height: 44) | ||
| } | ||
| .labelStyle(TitleOnlyLabelStyle()) | ||
|
|
||
| if #available(iOS 14.0, *) { | ||
| Label("Rain", systemImage: "cloud.rain") | ||
| } | ||
|
|
||
| if #available(iOS 14.0, *) { | ||
| VStack { | ||
| Label("Rain", systemImage: "cloud.rain") | ||
| Label("Snow", systemImage: "snow") | ||
| Label("Sun", systemImage: "sun.max") | ||
| } | ||
| .labelStyle(IconOnlyLabelStyle()) | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // | ||
| // Label.swift | ||
| // AltSwiftUI | ||
| // | ||
| // Created by Chan, Chengwei on 2021/05/24. | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
| @available(iOS 14.0, *) | ||
| public typealias LocalizedStringKey = String | ||
|
|
||
| /// A view that display icon with text. | ||
| public struct Label<Title: View, Icon: View>: View { | ||
| public var viewStore = ViewValues() | ||
| var title: Title | ||
| var icon: Icon | ||
|
|
||
| /// Creates an instance that disaly an `icon` with a `title`. | ||
| /// | ||
| /// - Parameters: | ||
| /// - title: The visual representation of right part text of the label | ||
| /// - icon: The visual representation of left part image of the label | ||
| public init(@ViewBuilder title: () -> Title, icon: () -> Icon) { | ||
| self.title = title() | ||
| self.icon = icon() | ||
| } | ||
|
|
||
| public var body: View { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| extension Label where Title == Text, Icon == Image { | ||
| /// Creates an instance that disaly an `icon` with a `title`. | ||
| /// | ||
| /// - Parameters: | ||
| /// - title: The string of right part text of the label | ||
| /// - icon: The visual representation of left part image of the label | ||
| public init<S: StringProtocol>(_ title: S, image: String) { | ||
| self.title = Text(title) | ||
| self.icon = Image(image) | ||
| } | ||
|
|
||
| /// Creates an instance that disaly an `icon` with a `title`. | ||
| /// | ||
| /// - Parameters: | ||
| /// - title: The LocalizedStringKey of right part text of the label | ||
| /// - icon: The visual representation of left part image of the label | ||
| @available(iOS 14.0, *) | ||
|
||
| public init(_ title: LocalizedStringKey, image: String) { | ||
| self.title = Text(NSLocalizedString(title, comment: "")) | ||
| self.icon = Image(image) | ||
| } | ||
|
|
||
| /// Creates an instance that disaly an `icon` with a `title`. | ||
| /// | ||
| /// - Parameters: | ||
| /// - title: The string of right part text of the label | ||
| /// - icon: The system icon name of left part image of the label | ||
| @available(iOS 14.0, *) | ||
| public init<S: StringProtocol>(_ title: S, systemImage: String) { | ||
| self.title = Text(title) | ||
| self.icon = Image(uiImage: UIImage(systemName: systemImage) ?? UIImage()) | ||
| } | ||
|
|
||
| /// Creates an instance that disaly an `icon` with a `title`. | ||
| /// | ||
| /// - Parameters: | ||
| /// - title: The LocalizedStringKey of right part text of the label | ||
| /// - icon: The system icon name of left part image of the label | ||
| @available(iOS 14.0, *) | ||
| public init(_ title: LocalizedStringKey, systemImage: String) { | ||
| self.title = Text(NSLocalizedString(title, comment: "")) | ||
| self.icon = Image(uiImage: UIImage(systemName: systemImage) ?? UIImage()) | ||
| } | ||
| } | ||
|
|
||
| extension Label: Renderable { | ||
| public func updateView(_ view: UIView, context: Context) { | ||
| guard let stackView = view as? SwiftUIStackView else { return } | ||
| setupView(stackView, context: context) | ||
| } | ||
|
|
||
| public func createView(context: Context) -> UIView { | ||
| let hstack = HStack { | ||
| Spacer() | ||
|
||
| .frame(height: 0) | ||
| self.icon | ||
| self.title | ||
| Spacer() | ||
| .frame(height: 0) | ||
| } | ||
| if let stackView = hstack.createView(context: context) as? SwiftUIStackView { | ||
| setupView(stackView, context: context) | ||
| return stackView | ||
| } | ||
|
|
||
| return UIView() | ||
| } | ||
|
|
||
| private func setupView(_ view: UIStackView, context: Context) { | ||
| guard let labelStyleType = context.viewValues?.labelStyle?.labelStyleType else { return } | ||
|
|
||
| context.viewOperationQueue.addOperation { | ||
|
||
| for i in 1..<(view.arrangedSubviews.count - 1) { | ||
| if i <= self.icon.subViews.count { | ||
| view.arrangedSubviews[i].isHidden = (labelStyleType == .TitleOnly) | ||
| } else { | ||
| view.arrangedSubviews[i].isHidden = (labelStyleType == .IconOnly) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can remove the constraint for this typealias