Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
flowbe committed Feb 28, 2021
0 parents commit d2df726
Show file tree
Hide file tree
Showing 10 changed files with 574 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Florentin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
28 changes: 28 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "MaterialOutlinedTextField",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MaterialOutlinedTextField",
targets: ["MaterialOutlinedTextField"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "MaterialOutlinedTextField",
dependencies: []),
.testTarget(
name: "MaterialOutlinedTextFieldTests",
dependencies: ["MaterialOutlinedTextField"]),
]
)
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# MaterialOutlinedTextField

A simple [Material Design outlined text field](https://material.io/components/text-fields) implementation in Swift.

## Installation

Installation can be done using Swift Package Manager. In Xcode, go to File > Swift Packages > Add Package Dependency… and paste the repository URL (https://github.com/flowbe/MaterialOutlinedTextField) to add it.

You can also add the dependency directly in your `Package.swift` file:
```swift
dependencies: [
.package(url: "https://github.com/flowbe/MaterialOutlinedTextField.git", .upToNextMajor(from: "0.1.0"))
]
```

## Usage

`MaterialOutlinedTextField` has the same interface as `UITextField` with a few extra properties and methods:

- `label`: The label appearing as floating or placeholder. You can use this property to set his text content.
- `labelBehavior`: Defines the behavior of the label when the text field is editing. The possible values are `floats` (default) or `disappears`.
- `containerRadius`: The corner radius of the text field.
- `colorModel`: The current color model based on the current state (get-only).
- `outlineLineWidth`: The current outline line width model based on the current state (get-only).
- `setColorModel(_ colorModel: ColorModel, for state: State)`: Set the color model for the specified state.
- `setOutlineLineWidth(_ outlineLineWidth: CGFloat, for state: State)`: Set the color model for the specified state.

## Example

```swift
let t = MaterialOutlinedTextField(frame: CGRect(x: 0, y: 0, width: 200, height: 56))
textField.label.text = "Label"
textField.placeholder = "Placeholder"
textField.clearButtonMode = .whileEditing
textField.setColorModel(ColorModel(textColor: .gray, floatingLabelColor: .gray, normalLabelColor: .gray, outlineColor: .gray), for: .normal)
textField.setColorModel(ColorModel(textColor: .systemBlue, floatingLabelColor: .systemBlue, normalLabelColor: .systemBlue, outlineColor: .systemBlue), for: .editing)
textField.setColorModel(ColorModel(with: .disabled), for: .disabled)
```
61 changes: 61 additions & 0 deletions Sources/MaterialOutlinedTextField/ColorModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// ColorModel.swift
// MaterialOutlinedTextField
//
// Created by Florentin on 28/02/2021.
// Copyright © 2021 Florentin. All rights reserved.
//

import UIKit

public struct ColorModel {
/// Text color.
public var textColor: UIColor
/// Floating label color.
public var floatingLabelColor: UIColor
/// Normal label color.
public var normalLabelColor: UIColor
/// Outline line color.
public var outlineColor: UIColor

public init(with state: MaterialOutlinedTextField.State) {
var textColor = UIColor.black
var floatingLabelColor = UIColor.black
var normalLabelColor = UIColor.darkGray
var outlineColor = UIColor.black

if #available(iOS 13.0, *) {
textColor = .label
floatingLabelColor = .label
normalLabelColor = .label
outlineColor = .label
}

let disabledAlpha: CGFloat = 0.6

if state == .disabled {
textColor = textColor.withAlphaComponent(disabledAlpha)
floatingLabelColor = floatingLabelColor.withAlphaComponent(disabledAlpha)
normalLabelColor = normalLabelColor.withAlphaComponent(disabledAlpha)
outlineColor = normalLabelColor.withAlphaComponent(disabledAlpha)
}

self.init(textColor: textColor,
floatingLabelColor: floatingLabelColor,
normalLabelColor: normalLabelColor,
outlineColor: outlineColor)
}

/// Creates a new color model.
/// - Parameters:
/// - textColor: Text color
/// - floatingLabelColor: Floating label color
/// - normalLabelColor: Normal label color
/// - outlineColor: Outline line color
public init(textColor: UIColor, floatingLabelColor: UIColor, normalLabelColor: UIColor, outlineColor: UIColor) {
self.textColor = textColor
self.floatingLabelColor = floatingLabelColor
self.normalLabelColor = normalLabelColor
self.outlineColor = outlineColor
}
}
Loading

0 comments on commit d2df726

Please sign in to comment.