Skip to content

Instantly share code, notes, and snippets.

@AOx0
Created March 23, 2020 08:46
Show Gist options
  • Save AOx0/9e49dca70c6e92eb3545b74618e58648 to your computer and use it in GitHub Desktop.
Save AOx0/9e49dca70c6e92eb3545b74618e58648 to your computer and use it in GitHub Desktop.
Proyect Code: ContentView.swift and EOConfig (Environment Object Configuration)
//
// ContentView.swift
//
//
// Created by Alejandro D on 22/03/20.
// Copyright © 2020 Alejandro D. All rights reserved.
//
import SwiftUI
import Combine
extension View {
func keyboardSensible(_ offsetValue: Binding<CGFloat>) -> some View {
return self
.padding(.bottom, offsetValue.wrappedValue)
.animation(.spring())
.onAppear {
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { notification in
let keyWindow = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
let bottom = keyWindow?.safeAreaInsets.bottom ?? 0
let value = notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
let height = value.height
offsetValue.wrappedValue = height - bottom
}
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { _ in
offsetValue.wrappedValue = 0
}
}
}
}
/// The TextView
struct TextView: UIViewRepresentable {
@Binding var text: String
@Binding var edit:Bool
@Binding var select:Bool
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> UITextView {
let myTextView = UITextView()
myTextView.delegate = context.coordinator
myTextView.font = UIFont(name: "RobotoMono-Regular", size: 9)
myTextView.isScrollEnabled = false
myTextView.isEditable = edit
myTextView.isUserInteractionEnabled = select
myTextView.isSelectable = select
myTextView.backgroundColor = UIColor(white: 0.0, alpha: 0.05)
return myTextView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
}
class Coordinator : NSObject, UITextViewDelegate {
var parent: TextView
init(_ uiTextView: TextView) {
self.parent = uiTextView
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return true
}
func textViewDidChange(_ textView: UITextView) {
print("text now: \(String(describing: textView.text!))")
self.parent.text = textView.text
}
}
}
/// The main View of the app
struct ContentView: View {
@EnvironmentObject var vars : Data
@State private var offsetValue: CGFloat = 0.0
@State var bTrue = true
@State var bFalse = false
var body: some View {
ZStack {
ScrollView (Axis.Set.horizontal) {
ScrollView {
HStack {
TextView(text: /*vars.*/$vars.numeroLinea, edit: $bFalse, select: $bFalse)
.frame(width: 20.0)
Divider()
TextView(text: /*vars.*/$vars.texto, edit: $bTrue, select: $bTrue)
}.padding()
}
}
VStack {
Spacer()
HStack {
Spacer()
//TextField(/*@START_MENU_TOKEN@*/"Placeholder"/*@END_MENU_TOKEN@*/, text: /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Value@*/.constant("")/*@END_MENU_TOKEN@*/)
Button(action: {
//self.texto = self.texto + "\n"
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}) {
ZStack {
Circle()
.frame(width: 50.0, height: 50.0)
.foregroundColor(/*@START_MENU_TOKEN@*/.white/*@END_MENU_TOKEN@*/)
.shadow(radius: 4.0)
Image("Rayo").resizable()
.frame(width: 15.0, height: 28.7)
.shadow(radius: 1.0)
.foregroundColor(.black)
}
}
}
}.padding([.bottom, .trailing], 40.0)
.keyboardSensible($offsetValue)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//
// EOConfig.swift
//
//
// Created by Alejandro D on 22/03/20.
// Copyright © 2020 Alejandro D. All rights reserved.
//
import Foundation
class Data : ObservableObject {
private var numeroLineaInt = 1
private var charDetected = false
@Published var numeroLinea : String = "1\n" //Number of Line
@Published var texto : String = "" {
willSet{
}
didSet{
let nsString = self.texto as NSString
self.numeroLineaInt = 1
nsString.enumerateLines{ (str,_) in
self.numeroLineaInt = self.numeroLineaInt + 1
}
self.numeroLinea = ""
for i in 1...numeroLineaInt {
self.numeroLinea = self.numeroLinea + String(i) + "\n"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment