Skip to content

Commit

Permalink
SiteIconPicker: Add emoji group picker
Browse files Browse the repository at this point in the history
  • Loading branch information
frosty committed Jun 11, 2021
1 parent cf9850d commit 8fd6a72
Show file tree
Hide file tree
Showing 10 changed files with 979 additions and 512 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import SwiftUI
extension BlogDetailsViewController {
@objc
func showEmojiPicker() {
guard #available(iOS 14.0, *) else {
return
}

var pickerView = SiteIconPickerView()

pickerView.onCompletion = { [weak self] image in
Expand Down
42 changes: 42 additions & 0 deletions WordPress/Classes/ViewRelated/Blog/My Site/EmojiRenderer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import UIKit

/// Renders the specified emoji character into an image with the specified background color.
/// The image size and insets for the character can be overridden if necessary.
///
struct EmojiRenderer {
let emoji: String
let backgroundColor: UIColor
let imageSize: CGSize
let insetSize: CGFloat

init(emoji: String, backgroundColor: UIColor, imageSize: CGSize = CGSize(width: 512.0, height: 512.0), insetSize: CGFloat = 16.0) {
self.emoji = emoji
self.backgroundColor = backgroundColor
self.imageSize = imageSize
self.insetSize = insetSize
}

func render() -> UIImage {
let rect = CGRect(origin: .zero, size: imageSize)
let insetRect = rect.insetBy(dx: insetSize, dy: insetSize)

// The size passed in here doesn't matter, we just need the descriptor
guard let font = UIFont.fontFittingText(emoji, in: insetRect.size, fontDescriptor: UIFont.systemFont(ofSize: 100).fontDescriptor) else {
return UIImage()
}

let renderer = UIGraphicsImageRenderer(size: rect.size)
let img = renderer.image { ctx in
backgroundColor.setFill()
ctx.fill(rect)

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center

let attrs: [NSAttributedString.Key : Any] = [.font: font, .paragraphStyle: paragraphStyle]
emoji.draw(with: insetRect, options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
}

return img
}
}
Loading

0 comments on commit 8fd6a72

Please sign in to comment.