diff --git a/MemojiViewDemo/Base.lproj/Main.storyboard b/MemojiViewDemo/Base.lproj/Main.storyboard index 637ed6c..8918914 100644 --- a/MemojiViewDemo/Base.lproj/Main.storyboard +++ b/MemojiViewDemo/Base.lproj/Main.storyboard @@ -1,28 +1,47 @@ - + - + - + - + + - + + + + + + + + + + + + + + + + + + + diff --git a/MemojiViewDemo/ViewController.swift b/MemojiViewDemo/ExampleViewController.swift similarity index 88% rename from MemojiViewDemo/ViewController.swift rename to MemojiViewDemo/ExampleViewController.swift index f4d4d7e..d17ee21 100644 --- a/MemojiViewDemo/ViewController.swift +++ b/MemojiViewDemo/ExampleViewController.swift @@ -1,5 +1,5 @@ // -// ViewController.swift +// ExampleViewController.swift // MemojiViewDemo // // Created by Emre Armagan on 10.04.22. @@ -8,22 +8,22 @@ import MemojiView import UIKit -class ViewController: UIViewController, MemojiViewDelegate { +class ExampleViewController: UIViewController, MemojiViewDelegate { let size = CGSize(width: 180, height: 180) static let color = UIColor(red: 156 / 255, green: 130 / 255, blue: 255 / 255, alpha: 1) static let defaultImage = UIImage(named: "user.image") private let memojiView: MemojiView = { let view = MemojiView(frame: .zero) - view.image = ViewController.defaultImage - view.backgroundColor = ViewController.color.withAlphaComponent(0.4) + view.image = ExampleViewController.defaultImage + view.backgroundColor = ExampleViewController.color.withAlphaComponent(0.4) return view }() private let miniMemojiView: MemojiView = { let view = MemojiView(frame: .zero) - view.image = ViewController.defaultImage - view.backgroundColor = ViewController.color + view.image = ExampleViewController.defaultImage + view.backgroundColor = ExampleViewController.color view.isEditable = false return view }() @@ -48,12 +48,19 @@ class ViewController: UIViewController, MemojiViewDelegate { let padding: CGFloat = (type == .memoji) ? 15 : 35 memojiView.layoutMargins = UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding) } + + @objc private func showSwiftUIExample() { + navigationController?.pushViewController(SwiftUIExampleViewController(), animated: true) + } } // MARK: - UI -extension ViewController { +extension ExampleViewController { private func setupView() { + title = "MemojiView" + navigationItem.rightBarButtonItem = UIBarButtonItem(title: "SwiftUI", style: .done, target: self, action: #selector(showSwiftUIExample)) + // Adding some content to view for demonstration purposes let rect = CGRect(origin: CGPoint(x: 0, y: 100 + size.height + 18), size: .init(width: view.frame.width, height: 30)) let nameLabel = UILabel(frame: rect) diff --git a/MemojiViewDemo/SwiftUI/MemojiContentView.swift b/MemojiViewDemo/SwiftUI/MemojiContentView.swift new file mode 100644 index 0000000..121e1a8 --- /dev/null +++ b/MemojiViewDemo/SwiftUI/MemojiContentView.swift @@ -0,0 +1,54 @@ +// +// MemojiContentView.swift +// MemojiView +// +// Created by Emre Armagan on 05.01.25. +// Copyright © 2025 Emre Armagan. All rights reserved. +// + +import MemojiView +import SwiftUI + +struct MemojiContentView: View { + @State private var displayedImage: UIImage? + @State private var displayedType: MemojiImageType? + @State private var isEditable: Bool = true + + var body: some View { + VStack(spacing: 20) { + Text("Memoji Editor").font(.headline) + + MemojiViewRepresentable( + image: $displayedImage, + memojiType: $displayedType, + isEditable: $isEditable, + maxLetters: 10, + textColor: .blue + ) { updatedImage, updatedType in + print("Updated image: \(updatedImage?.description ?? "nil")") + print("Updated type: \(updatedType)") + } + .frame(height: 200) + .border(Color.gray, width: 1) + + Toggle("Enable Editing", isOn: $isEditable) + .padding() + + if let image = displayedImage { + Image(uiImage: image) + .resizable() + .scaledToFit() + .frame(height: 100) + } + } + .padding() + } +} + +struct MemojiContentView_Previews: PreviewProvider { + static var previews: some View { + MemojiContentView() + .previewDevice("iPhone 14 Pro") + .previewDisplayName("ContentView Preview") + } +} diff --git a/MemojiViewDemo/SwiftUI/SwiftUIExampleViewController.swift b/MemojiViewDemo/SwiftUI/SwiftUIExampleViewController.swift new file mode 100644 index 0000000..1546665 --- /dev/null +++ b/MemojiViewDemo/SwiftUI/SwiftUIExampleViewController.swift @@ -0,0 +1,35 @@ +// +// SwiftUIExampleViewController.swift +// MemojiView +// +// Created by Emre Armagan on 05.01.25. +// Copyright © 2025 Emre Armagan. All rights reserved. +// + +import MemojiView +import SwiftUI +import UIKit + +class SwiftUIExampleViewController: UIViewController { + override func viewDidLoad() { + super.viewDidLoad() + setupSwiftUIContent() + } + + private func setupSwiftUIContent() { + let hostingController = UIHostingController(rootView: MemojiContentView()) + addChild(hostingController) + + hostingController.view.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(hostingController.view) + + NSLayoutConstraint.activate([ + hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor), + hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor), + hostingController.view.topAnchor.constraint(equalTo: view.topAnchor), + hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor) + ]) + + hostingController.didMove(toParent: self) + } +}