Skip to content

Commit 4b2529e

Browse files
committed
new attributed strings
1 parent c593493 commit 4b2529e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

bk2ch10p503attributedString/ch23p771attributedStringInLabel/ViewController.swift

+62
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,67 @@ class ViewController : UIViewController {
136136
}
137137
}
138138

139+
@available(iOS 15, *)
140+
func theNewWay() {
141+
// attributed strings now have properties
142+
// cleverly uses dynamic member lookup
143+
let s1 = """
144+
The Gettysburg Address, as delivered on a certain occasion \
145+
(namely Thursday, November 19, 1863) by A. Lincoln
146+
"""
147+
var att = AttributedString(s1)
148+
att.font = UIFont(name:"Arial-BoldMT", size:15)
149+
att.foregroundColor = UIColor(red:0.251, green:0.000, blue:0.502, alpha:1)
150+
// can apply attributes directly to range slice
151+
// good chance to demonstrate an AttributeContainer, expressing a dictionary...
152+
// without looking like one
153+
// this is all done by some combination of dynamic member lookup
154+
// and callAsFunction, perhaps using an intermediate builder object
155+
// but I don't yet understand the details
156+
if let r = att.range(of: "Gettysburg Address") {
157+
att[r].mergeAttributes(.init().strokeColor(.red).strokeWidth(-2.0))
158+
}
159+
// disappointingly, there is apparently no change in this
160+
let para = NSMutableParagraphStyle()
161+
para.headIndent = 10
162+
para.firstLineHeadIndent = 10
163+
para.tailIndent = -10
164+
para.lineBreakMode = .byWordWrapping
165+
para.alignment = .center
166+
para.paragraphSpacing = 15
167+
// underneath it still works as before; sufficient to apply para to first char
168+
let start = att.startIndex
169+
let firstCharacter = start..<att.index(start, offsetByCharacters: 1)
170+
att[firstCharacter].paragraphStyle = para
171+
172+
let s2 = """
173+
Fourscore and seven years ago, our fathers brought forth \
174+
upon this continent a new nation, conceived in liberty and \
175+
dedicated to the proposition that all men are created equal.
176+
"""
177+
var att2 = AttributedString(s2)
178+
att2.font = UIFont(name:"HoeflerText-Black", size:16)
179+
let start2 = att2.startIndex
180+
let firstCharacter2 = start2..<att2.index(start, offsetByCharacters: 1)
181+
att2[firstCharacter2].mergeAttributes(
182+
.init()
183+
.font(UIFont(name:"HoeflerText-Black", size:24)!)
184+
.expansion(0.3)
185+
.kern(-4))
186+
let para2 = NSMutableParagraphStyle()
187+
para2.headIndent = 10
188+
para2.firstLineHeadIndent = 10
189+
para2.tailIndent = -10
190+
para2.lineBreakMode = .byWordWrapping
191+
para2.alignment = .justified
192+
para2.lineHeightMultiple = 1.2
193+
para2.hyphenationFactor = 1.0
194+
att2[firstCharacter2].paragraphStyle = para2
195+
196+
// wow really easy to concatenate; they act like strings
197+
let both = NSAttributedString(att + "\n" + att2)
198+
_ = both
199+
}
200+
139201

140202
}

0 commit comments

Comments
 (0)