Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring stuff #282

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions Spring/AsyncButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,25 @@
import UIKit

public class AsyncButton: UIButton {

private var imageURL = [UInt:NSURL]()
private var placeholderImage = [UInt:UIImage]()



private var imageURL : [UInt:NSURL] = [:]
private var placeholderImage :[UInt:UIImage] = [:]

public func setImageURL(url: NSURL?, placeholderImage placeholder:UIImage?, forState state:UIControlState) {

imageURL[state.rawValue] = url
placeholderImage[state.rawValue] = placeholder

if let urlString = url?.absoluteString {
ImageLoader.sharedLoader.imageForUrl(urlString: urlString) { [weak self] image, url in

if let strongSelf = self {

DispatchQueue.main.async(execute: { () -> Void in
if strongSelf.imageURL[state.rawValue]?.absoluteString == url {
strongSelf.setImage(image, for: state)
}
})

guard let urlString = url?.absoluteString else { return }
ImageLoader.sharedLoader.image(for: urlString) { [weak self] image, url in
guard let strongSelf = self else { return }
DispatchQueue.main.async(execute: {
if strongSelf.imageURL[state.rawValue]?.absoluteString == url {
strongSelf.setImage(image, for: state)
}
}
})
}
}

}

17 changes: 8 additions & 9 deletions Spring/AsyncImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ public class AsyncImageView: UIImageView {
public var url : NSURL? {
didSet {
self.image = placeholderImage
if let urlString = url?.absoluteString {
ImageLoader.sharedLoader.imageForUrl(urlString: urlString) { [weak self] image, url in
if let strongSelf = self {
DispatchQueue.main.async(execute: { () -> Void in
if strongSelf.url?.absoluteString == url {
strongSelf.image = image ?? strongSelf.placeholderImage
}
})
guard let urlString = url?.absoluteString else { return }
ImageLoader.sharedLoader.image(for: urlString) { [weak self] image, url in
guard let strongSelf = self else { return }
DispatchQueue.main.async(execute: {
if strongSelf.url?.absoluteString == url {
strongSelf.image = image ?? strongSelf.placeholderImage
}
}
})
}
}
}
Expand All @@ -49,3 +47,4 @@ public class AsyncImageView: UIImageView {
}

}

5 changes: 3 additions & 2 deletions Spring/AutoTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ public class AutoTextView: UITextView {
if text.length == 0 {
size.height = 0
}

contentInset = UIEdgeInsets(top: -4, left: -4, bottom: -4, right: -4)
layoutIfNeeded()

return size
}
}
}

5 changes: 3 additions & 2 deletions Spring/BlurView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
import UIKit

public func insertBlurView (view: UIView, style: UIBlurEffectStyle) -> UIVisualEffectView {
view.backgroundColor = UIColor.clear
view.backgroundColor = .clear

let blurEffect = UIBlurEffect(style: style)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
view.insertSubview(blurEffectView, at: 0)
return blurEffectView
}

17 changes: 9 additions & 8 deletions Spring/DesignableButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,47 @@ import UIKit

@IBDesignable public class DesignableButton: SpringButton {

@IBInspectable public var borderColor: UIColor = UIColor.clear {
@IBInspectable public var borderColor: UIColor = .clear {
didSet {
layer.borderColor = borderColor.cgColor
}
}

@IBInspectable public var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}

@IBInspectable public var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
@IBInspectable public var shadowColor: UIColor = UIColor.clear {

@IBInspectable public var shadowColor: UIColor = .clear {
didSet {
layer.shadowColor = shadowColor.cgColor
}
}

@IBInspectable public var shadowRadius: CGFloat = 0 {
didSet {
layer.shadowRadius = shadowRadius
}
}

@IBInspectable public var shadowOpacity: CGFloat = 0 {
didSet {
layer.shadowOpacity = Float(shadowOpacity)
}
}

@IBInspectable public var shadowOffsetY: CGFloat = 0 {
didSet {
layer.shadowOffset.height = shadowOffsetY
}
}

}

9 changes: 5 additions & 4 deletions Spring/DesignableImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@
import UIKit

@IBDesignable public class DesignableImageView: SpringImageView {
@IBInspectable public var borderColor: UIColor = UIColor.clear {

@IBInspectable public var borderColor: UIColor = .clear {
didSet {
layer.borderColor = borderColor.cgColor
}
}

@IBInspectable public var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}

@IBInspectable public var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}

}

12 changes: 7 additions & 5 deletions Spring/DesignableLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ import UIKit
didSet {
let font = UIFont(name: self.font.fontName, size: self.font.pointSize)
guard let text = self.text else { return }

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineHeight

let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSAttributedStringKey.font, value: font!, range: NSMakeRange(0, attributedString.length))


attributedString.add(attribute: .paragraphStyle, value: paragraphStyle)
attributedString.add(attribute: .font, value: font!)

self.attributedText = attributedString
}
}

}

55 changes: 28 additions & 27 deletions Spring/DesignableTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,78 +23,78 @@
import UIKit

@IBDesignable class DesignableTabBarController: UITabBarController {
@IBInspectable var normalTint: UIColor = UIColor.clear {

@IBInspectable var normalTint: UIColor = .clear {
didSet {
UITabBar.appearance().tintColor = normalTint
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: normalTint], for: UIControlState())
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: normalTint], for: [])
}
}
@IBInspectable var selectedTint: UIColor = UIColor.clear {

@IBInspectable var selectedTint: UIColor = .clear {
didSet {
UITabBar.appearance().tintColor = selectedTint
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: selectedTint], for:UIControlState.selected)
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: selectedTint], for: .selected)
}
}

@IBInspectable var fontName: String = "" {
didSet {
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: normalTint, NSAttributedStringKey.font: UIFont(name: fontName, size: 11)!], for: UIControlState())
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: normalTint, .font: UIFont(name: fontName, size: 11)!], for: [])
}
}

@IBInspectable var firstSelectedImage: UIImage? {
didSet {
if let image = firstSelectedImage {
var tabBarItems = self.tabBar.items as [UITabBarItem]!
tabBarItems?[0].selectedImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
tabBarItems?[0].selectedImage = image.withRenderingMode(.alwaysTemplate)
}
}
}

@IBInspectable var secondSelectedImage: UIImage? {
didSet {
if let image = secondSelectedImage {
var tabBarItems = self.tabBar.items as [UITabBarItem]!
tabBarItems?[1].selectedImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
tabBarItems?[1].selectedImage = image.withRenderingMode(.alwaysTemplate)
}
}
}

@IBInspectable var thirdSelectedImage: UIImage? {
didSet {
if let image = thirdSelectedImage {
var tabBarItems = self.tabBar.items as [UITabBarItem]!
tabBarItems?[2].selectedImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
tabBarItems?[2].selectedImage = image.withRenderingMode(.alwaysTemplate)
}
}
}

@IBInspectable var fourthSelectedImage: UIImage? {
didSet {
if let image = fourthSelectedImage {
var tabBarItems = self.tabBar.items as [UITabBarItem]!
tabBarItems?[3].selectedImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
tabBarItems?[3].selectedImage = image.withRenderingMode(.alwaysTemplate)
}
}
}

@IBInspectable var fifthSelectedImage: UIImage? {
didSet {
if let image = fifthSelectedImage {
var tabBarItems = self.tabBar.items as [UITabBarItem]!
tabBarItems?[4].selectedImage = image.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
tabBarItems?[4].selectedImage = image.withRenderingMode(.alwaysTemplate)
}
}
}

override func viewDidLoad() {
super.viewDidLoad()

for item in self.tabBar.items as [UITabBarItem]! {
if let image = item.image {
item.image = image.imageWithColor(tintColor: self.normalTint).withRenderingMode(UIImageRenderingMode.alwaysOriginal)
item.image = image.imageWithColor(tintColor: self.normalTint).withRenderingMode(.alwaysOriginal)
}
}
}
Expand All @@ -103,20 +103,21 @@ import UIKit
extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

let context = UIGraphicsGetCurrentContext()
context!.translateBy(x: 0, y: self.size.height)
context!.scaleBy(x: 1.0, y: -1.0);
context!.setBlendMode(CGBlendMode.normal)
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
context!.setBlendMode(.normal)

let rect = CGRect(origin: .zero, size: self.size)
context?.clip(to: rect, mask: self.cgImage!)
tintColor.setFill()
context!.fill(rect)

let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
UIGraphicsEndImageContext()

return newImage
}
}

Loading