Skip to content

Commit

Permalink
Merge pull request #8 from TrianglZ/feature/add-focused-color
Browse files Browse the repository at this point in the history
Add focused color to style
  • Loading branch information
NourGweda1 authored Jul 4, 2024
2 parents de68092 + 17186a9 commit c6c89cf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ You can customize the appearance of the OTP view by adjusting the Style struct p
shadowRadius: CGFloat,
shadowXOffset: CGFloat,
shadowYOffset: CGFloat,
isCursorHidden: Bool)
isCursorHidden: Bool,
focusedStateColor: Color? = nil)
```
#### Callbacks
TrianglzOTPView provides two callbacks:
Expand Down
16 changes: 14 additions & 2 deletions Sources/TrianglzOTPView/Helpers/Extensions/View+TFModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,29 @@ import Foundation
import SwiftUI

extension View {

func customTextFieldModifier(customStyle: TrianglzOTPView.Style,
index: Int,
focusedTextField: FocusState<Int?>.Binding) -> some View {
self

var borderColor: Color {
guard let focusedStateColor = customStyle.focusedStateColor else {
return customStyle.borderColor
}
guard focusedTextField.wrappedValue == index else {
return customStyle.borderColor
}
return focusedStateColor
}

return self
.frame(width: customStyle.width, height: customStyle.height)
.background(
RoundedRectangle(cornerRadius: customStyle.cornerRadius)
.foregroundColor(customStyle.backgroundColor)
.overlay(
RoundedRectangle(cornerRadius: customStyle.cornerRadius)
.stroke(customStyle.borderColor, lineWidth: customStyle.borderWidth)
.stroke(borderColor, lineWidth: customStyle.borderWidth)
)
)
.foregroundColor(customStyle.foregroundColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct EnhancedTextField: UIViewRepresentable {
let font: UIFont
let fontColor: UIColor
let keyboardType: UIKeyboardType
var focusedStateFontColor: UIColor? = nil

let onBackspace: (Bool) -> Void
let onChange: (String) -> Void

Expand All @@ -33,6 +35,8 @@ struct EnhancedTextField: UIViewRepresentable {
view.keyboardType = keyboardType
view.font = font
view.textColor = fontColor
view.fontColor = fontColor
view.focusedStateFontColor = focusedStateFontColor
return view
}

Expand All @@ -42,6 +46,8 @@ struct EnhancedTextField: UIViewRepresentable {
}

class EnhancedUITextField: UITextField {
var fontColor: UIColor?
var focusedStateFontColor: UIColor? = nil
var onBackspace: ((Bool) -> Void)?

override init(frame: CGRect) {
Expand All @@ -57,5 +63,19 @@ struct EnhancedTextField: UIViewRepresentable {
onBackspace?(text?.isEmpty == true)
super.deleteBackward()
}

override func becomeFirstResponder() -> Bool {
let didBecomeFirstResponder = super.becomeFirstResponder()
textColor = focusedStateFontColor
tintColor = focusedStateFontColor
return didBecomeFirstResponder
}

override func resignFirstResponder() -> Bool {
let didResignFirstResponder = super.resignFirstResponder()
textColor = fontColor
tintColor = fontColor
return didResignFirstResponder
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension TrianglzOTPView {
var shadowXOffset: CGFloat = 0
var shadowYOffset: CGFloat = 0
var isCursorHidden: Bool = false
var focusedStateColor: Color? = nil

public init() {
foregroundColor = .black
Expand All @@ -46,7 +47,8 @@ extension TrianglzOTPView {
shadowRadius: CGFloat = 0,
shadowXOffset: CGFloat = 0,
shadowYOffset: CGFloat = 0,
isCursorHidden: Bool = false) {
isCursorHidden: Bool = false,
focusedStateColor: Color? = nil) {
self.foregroundColor = foregroundColor
self.fontStyle = fontStyle
self.hstackSpacing = hstackSpacing
Expand All @@ -62,6 +64,7 @@ extension TrianglzOTPView {
self.shadowXOffset = shadowXOffset
self.shadowYOffset = shadowYOffset
self.isCursorHidden = isCursorHidden
self.focusedStateColor = focusedStateColor
}
}
}
9 changes: 9 additions & 0 deletions Sources/TrianglzOTPView/TrianglzOTPView/TrianglzOTPView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public struct TrianglzOTPView: View {
return .alphabet
}
}

private var focusedStateFontColor: UIColor? {
guard let focusedStateColor = customStyle.focusedStateColor else {
return nil
}
return UIColor(focusedStateColor)
}

public var body: some View {
HStack(alignment: customStyle.hstackAlignment,
spacing: customStyle.hstackSpacing) {
Expand All @@ -62,6 +70,7 @@ public struct TrianglzOTPView: View {
font: customStyle.fontStyle,
fontColor: UIColor(customStyle.foregroundColor),
keyboardType: keyboardType,
focusedStateFontColor: focusedStateFontColor,
onBackspace: { isEmpty in
handleOnBackAction(isEmpty: isEmpty, index: index)
},onChange: { newValue in
Expand Down

0 comments on commit c6c89cf

Please sign in to comment.