-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
34 lines (28 loc) · 1.7 KB
/
Contents.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
//: [< Previous](@previous) [Home](Introduction) [Next >](@next)
//: # Writing on the wall
//: If you have an `NSAttributedString`, you can render it directly to a Core Graphics context just by saying where it should be drawn. All the string's attributes will be used for rendering, including its font, size, color, and more.
//:
//: - Experiment: Someone has been putting up inspirational posters around your office, saying "The early bird catches the worm." Your designer wants your help to design a modified version that says "But the second mouse gets the cheese" in red text 100 pixels below. Can you write code to match their sketch?
import UIKit
let rect = CGRect(x: 0, y: 0, width: 1000, height: 1000)
let renderer = UIGraphicsImageRenderer(bounds: rect)
let rendered = renderer.image { ctx in
let firstPosition = rect.offsetBy(dx: 0, dy: 300)
let firstText = "The early bird catches the worm."
let firstAttrs: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 72),
.foregroundColor: UIColor.blue
]
let firstString = NSAttributedString(string: firstText, attributes: firstAttrs)
firstString.draw(in: firstPosition)
let secondPosition = rect.offsetBy(dx: 0, dy: 400)
let secondText = "But the second mouse gets the cheese"
let secondAttrs: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 72),
.foregroundColor: UIColor.red
]
let secondString = NSAttributedString(string: secondText, attributes: secondAttrs)
secondString.draw(in: secondPosition)
}
showOutput(rendered)
//: [< Previous](@previous) [Home](Introduction) [Next >](@next)