Files
LCEssentials/Sources/LCEssentials/Extensions/LCEssentials+UITextField.swift

64 lines
2.6 KiB
Swift
Raw Normal View History

2025-03-23 00:27:49 -03:00
//
// Copyright (c) 2018 Loverde Co.
//
// 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.
import Foundation
#if os(iOS) || os(macOS)
import UIKit
public extension UITextField {
var placeholderColor: UIColor {
get {
return attributedPlaceholder?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor ?? .clear;
}
set {
guard let attributedPlaceholder = attributedPlaceholder else { return; }
let attributes: [NSAttributedString.Key: UIColor] = [.foregroundColor: newValue];
self.attributedPlaceholder = NSAttributedString(string: attributedPlaceholder.string, attributes: attributes);
}
}
/// Add padding to the left of the textfield rect.
///
/// - Parameter padding: amount of padding to apply to the left of the textfield rect.
func addPaddingLeft(_ padding: CGFloat) {
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: padding, height: frame.height))
leftView = paddingView
leftViewMode = .always
}
/// Add padding to the left of the textfield rect.
///
/// - Parameters:
/// - image: left image
/// - padding: amount of padding between icon and the left of textfield
func addPaddingLeftIcon(_ image: UIImage, padding: CGFloat) {
let imageView = UIImageView(image: image)
imageView.contentMode = .center
leftView = imageView
leftView?.frame.size = CGSize(width: image.size.width + padding, height: image.size.height)
leftViewMode = .always
}
}
#endif