Skip to content

Extension function document

์ด์ •ํ›ˆ edited this page Feb 14, 2024 · 5 revisions

View

extension View {
    func dismissKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}
extension View {
    func modifier<ModifiedContent: View>(
        @ViewBuilder body: (_ content: Self) -> ModifiedContent
    ) -> ModifiedContent {
        body(self)
    }
}

Date

extension Date {
    func toYearMonthDay() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        
        return dateFormatter.string(from: self)
    }
}

String

extension String {
    func toDate() -> Date? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        dateFormatter.timeZone = TimeZone(identifier: "UTC")
        if let date = dateFormatter.date(from: self) {
            return date
        } else {
            return nil
        }
    }
}

SignUpView

extension SignUpView {
    //MARK: - keyboard dismiss method
    func dismissKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

CustomColor

extension CustomColor {
    static func getSignUpInputGray(_ colorMode: ColorScheme) -> Color {
        if colorMode == .light {
            return Color(red: 244 / 255, green: 244 / 255, blue: 244 / 255)
        } else {
            return Color(red: 28 / 255, green: 28 / 255, blue: 30 / 255)
        }
    }
}

Clone this wiki locally