Files
2022-11-17 15:42:32 +01:00

51 lines
1.5 KiB
Swift

//
// UIKit+Extensions.swift
// Earthquake Network
//
// Created by Andrea Busi on 05/04/21.
// Copyright © 2021 Earthquake Network. All rights reserved.
//
import UIKit
extension UIButton {
func setLocalizedTitle(key: String, uppercased: Bool = true, emoji: String? = nil) {
var title = NSLocalizedString(key, comment: "")
if uppercased {
title = title.uppercased()
}
if let emoji = emoji {
title = "\(title) \(emoji)"
}
setTitle(title, for: .normal)
}
}
extension UIImage {
class func circle(
diameter: CGFloat,
color: UIColor,
borderWidth: CGFloat = 0.0,
borderColor: UIColor = .black
) -> UIImage {
let size = CGSize(width: diameter, height: diameter)
let renderer = UIGraphicsImageRenderer(size: size)
let img = renderer.image { ctx in
ctx.cgContext.setFillColor(color.cgColor)
ctx.cgContext.setStrokeColor(borderColor.cgColor)
ctx.cgContext.setLineWidth(borderWidth)
// reduce circle size to keep space for the border
// without this, the image view is cropped
let circleDiameter = diameter - 2*borderWidth
let rectangle = CGRect(x: borderWidth, y: borderWidth, width: circleDiameter, height: circleDiameter)
ctx.cgContext.addEllipse(in: rectangle)
ctx.cgContext.drawPath(using: .fillStroke)
}
return img
}
}