-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SiteIconPicker: Add emoji group picker
- Loading branch information
Showing
10 changed files
with
979 additions
and
512 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
42 changes: 42 additions & 0 deletions
42
WordPress/Classes/ViewRelated/Blog/My Site/EmojiRenderer.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,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 | ||
} | ||
} |
Oops, something went wrong.