-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathViewController.swift
144 lines (107 loc) · 5.16 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// ViewController.swift
// MemeGenerator
//
// Created by Julian Moorhouse on 26/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var imageView: UIImageView!
var topText: String?
var bottomText: String?
var photo: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNewMeme))
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(shareMeme))
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
photo = image
drawMeme()
dismiss(animated: true)
}
@objc func addNewMeme() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
picker.sourceType = UIImagePickerController.SourceType.camera
}
present(picker, animated: true)
}
@objc func shareMeme() {
guard let image = imageView.image else {
print("No image found")
return
}
let vc = UIActivityViewController(activityItems: [image], applicationActivities: [])
vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true)
}
func drawMeme() {
guard let source = photo else {
print("No image found")
return
}
let imageSize = source.size
let renderer = UIGraphicsImageRenderer(size: CGSize(width: imageSize.width, height: imageSize.height))
let image = renderer.image { _ in
photo?.draw(at: CGPoint(x: 0, y: 0))
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineBreakMode = .byWordWrapping
let attrs: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 70),
.foregroundColor: UIColor.red,
.paragraphStyle: paragraphStyle,
.strokeWidth: -4,
.strokeColor: UIColor.yellow,
]
let stringHeight: CGFloat = 200.0
let margin: CGFloat = 50.0
let topAttributedString = NSAttributedString(string: topText ?? "", attributes: attrs)
topAttributedString.draw(with: CGRect(x: 0, y: margin, width: imageSize.width, height: stringHeight), options: .usesLineFragmentOrigin, context: nil)
let bottomAttributedString = NSAttributedString(string: bottomText ?? "", attributes: attrs)
let boundingBox = bottomAttributedString.boundingRect(
with: CGSize(width: imageSize.width, height: imageSize.height),
options: [.usesLineFragmentOrigin],
context: nil
)
let bottomY = (imageSize.height - boundingBox.size.height) - margin
bottomAttributedString.draw(with: CGRect(x: 0, y: bottomY, width: imageSize.width, height: boundingBox.size.height), options: .usesLineFragmentOrigin, context: nil)
}
imageView.image = image
}
@IBAction func setTopTextTapped(_ sender: Any) {
let ac = UIAlertController(title: "Meme Generator", message: nil, preferredStyle: .alert)
let saveAction = UIAlertAction(title: "OK", style: .default) {
[weak self, weak ac] _ in
guard let text = ac?.textFields?[0].text else { return }
self?.topText = text
self?.drawMeme()
}
ac.addAction(saveAction)
ac.addTextField(configurationHandler: { textField in
textField.placeholder = "Top text"
})
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(ac, animated: true)
}
@IBAction func setBottomTextTapped(_ sender: Any) {
let ac = UIAlertController(title: "Meme Generator", message: nil, preferredStyle: .alert)
let saveAction = UIAlertAction(title: "OK", style: .default) {
[weak self, weak ac] _ in
guard let text = ac?.textFields?[0].text else { return }
self?.bottomText = text
self?.drawMeme()
}
ac.addAction(saveAction)
ac.addTextField(configurationHandler: { textField in
textField.placeholder = "Bottom text"
})
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(ac, animated: true)
}
}