-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from mzapatae/feature/required-extensions
Feature: Some extensions for UIKit
- Loading branch information
Showing
15 changed files
with
374 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Result+IsStatus.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 08-08-21. | ||
// | ||
|
||
import Combine | ||
|
||
extension Result { | ||
|
||
public var isSuccess: Bool { | ||
switch self { | ||
case .success: return true | ||
default: return false | ||
} | ||
} | ||
|
||
public var isFailure: Bool { | ||
switch self { | ||
case .failure: return true | ||
default: return false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// Range+NSRange.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 09-08-21. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Range where Bound == String.Index { | ||
|
||
public var nsRange:NSRange { | ||
return NSRange(location: self.lowerBound.encodedOffset, length: self.upperBound.encodedOffset - self.lowerBound.encodedOffset) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// SearchBar+Enabled.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 13-08-21. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UISearchBar { | ||
public func enable() { | ||
isUserInteractionEnabled = true | ||
alpha = 1.0 | ||
} | ||
|
||
public func disable() { | ||
isUserInteractionEnabled = false | ||
alpha = 0.5 | ||
} | ||
} | ||
|
39 changes: 39 additions & 0 deletions
39
Sources/Common/Extensions/UIKit/UITapGesture+TapAttributedText.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// UITapGesture+TapAttributedText.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 09-08-21. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UITapGestureRecognizer { | ||
|
||
public func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool { | ||
let layoutManager = NSLayoutManager() | ||
let textContainer = NSTextContainer(size: CGSize.zero) | ||
let textStorage = NSTextStorage(attributedString: label.attributedText!) | ||
|
||
layoutManager.addTextContainer(textContainer) | ||
textStorage.addLayoutManager(layoutManager) | ||
|
||
textContainer.lineFragmentPadding = 0.0 | ||
textContainer.lineBreakMode = label.lineBreakMode | ||
textContainer.maximumNumberOfLines = label.numberOfLines | ||
let labelSize = label.bounds.size | ||
textContainer.size = labelSize | ||
|
||
let locationOfTouchInLabel = self.location(in: label) | ||
let textBoundingBox = layoutManager.usedRect(for: textContainer) | ||
|
||
let textContainerOffset = CGPoint( | ||
x: (labelSize.width - textBoundingBox.size.width) * 0.3 - textBoundingBox.origin.x, | ||
y: (labelSize.height - textBoundingBox.size.height) * 0.3 - textBoundingBox.origin.y | ||
) | ||
|
||
let locationOfTouchInTextContainer = CGPoint(x: locationOfTouchInLabel.x - textContainerOffset.x, y: locationOfTouchInLabel.y - textContainerOffset.y) | ||
let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil) | ||
return NSLocationInRange(indexOfCharacter, targetRange) | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
Sources/Common/Extensions/UIKit/UITextField+SetupKeyboard.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// UITextField+SetupKeyboard.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 09-08-21. | ||
// | ||
|
||
import UIKit | ||
|
||
public enum KeyboardType { | ||
case email, password, numbers, phoneNumber, normal | ||
} | ||
|
||
extension UITextField { | ||
|
||
public func setupKeyboard(_ type: KeyboardType, returnKeyType: UIReturnKeyType) { | ||
self.autocapitalizationType = .none | ||
self.autocorrectionType = .no | ||
self.spellCheckingType = .no | ||
self.keyboardAppearance = .default | ||
self.returnKeyType = returnKeyType | ||
self.clearButtonMode = .always | ||
|
||
switch type { | ||
case .email: | ||
self.keyboardType = .emailAddress | ||
|
||
case .password: | ||
self.keyboardType = .default | ||
self.isSecureTextEntry = true | ||
|
||
case .numbers: | ||
self.keyboardType = .numbersAndPunctuation | ||
|
||
case .phoneNumber: | ||
self.keyboardType = .phonePad | ||
|
||
case .normal: | ||
self.keyboardType = .default | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
Sources/Common/Extensions/UIKit/UIView+SetCornerRadius.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// UIView+SetCornerRadius.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 03-08-21. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIView { | ||
|
||
public func set(cornerRadius: CGFloat) { | ||
self.clipsToBounds = true | ||
self.layer.cornerRadius = cornerRadius | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
Sources/Common/Extensions/UIKit/UIView+SetSubviewAutolayout.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// UIView+SetSubviewAutolayout.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 03-08-21. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIView { | ||
|
||
public func setSubviewForAutoLayout(_ subview: UIView) { | ||
subview.translatesAutoresizingMaskIntoConstraints = false | ||
self.addSubview(subview) | ||
} | ||
|
||
public func setSubviewsForAutoLayout(_ subviews: [UIView]) { | ||
subviews.forEach(setSubviewForAutoLayout(_:)) | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Localized.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 03-08-21. | ||
// | ||
|
||
import Foundation | ||
|
||
@propertyWrapper | ||
public struct Localized { | ||
private var key: String | ||
private var stringsFileName: String | ||
|
||
public init(_ key: String, stringsFileName: String = "Localizable") { | ||
self.key = key | ||
self.stringsFileName = stringsFileName | ||
} | ||
|
||
public var wrappedValue: String { | ||
NSLocalizedString(key, tableName: stringsFileName, comment: "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Coordinator.swift | ||
// | ||
// | ||
// Created by Miguel Angel on 03-08-21. | ||
// | ||
|
||
import UIKit | ||
|
||
public protocol Coordinator { | ||
func start() | ||
func coordinate(to coordinator: Coordinator) | ||
} | ||
|
||
public extension Coordinator { | ||
|
||
func coordinate(to coordinator: Coordinator) { | ||
coordinator.start() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.