Skip to content

Commit ada481b

Browse files
committed
mild chapter 10 tweaks
1 parent 9c6ee13 commit ada481b

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

bk2ch10p498fontDescriptor/ch23p670font1dynamicType/ViewController.swift

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ViewController : UIViewController {
3838
let emphasis = body.withSymbolicTraits(.traitItalic)!
3939
var fbody = UIFont(descriptor: body, size: 0)
4040
// work around lack of dynamism; this should not be necessary
41+
// bug still there in beta 6
4142
fbody = UIFontMetrics(forTextStyle: .body).scaledFont(for: fbody)
4243
var femphasis = UIFont(descriptor: emphasis, size: 0)
4344
// work around lack of dynamism; this should not be necessary

bk2ch10p508AttributedTextAsSecretMarking/AttributedTextAsSecretMarking/ViewController.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import UIKit
33

44
extension NSAttributedString.Key {
5-
static let myDate = NSAttributedString.Key("myDate")
5+
static let date = NSAttributedString.Key("date")
66
}
77

88
class ViewController: UIViewController {
@@ -18,12 +18,11 @@ class ViewController: UIViewController {
1818
let mas = NSMutableAttributedString(attributedString:self.lab.attributedText!)
1919
let r = (mas.string as NSString).range(of:"^0")
2020
if r.length > 0 {
21-
mas.addAttribute(.myDate, value: 1, range: r)
21+
mas.addAttribute(.date, value: 1, range: r)
2222
mas.replaceCharacters(in:r, with: Date().description)
2323
} else {
24-
mas.enumerateAttribute(.myDate, in: NSMakeRange(0, mas.length)) {
25-
value, r, stop in
26-
if let value = value as? Int, value == 1 {
24+
mas.enumerateAttribute(.date, in: NSMakeRange(0, mas.length)) { val, r, stop in
25+
if val as? Int == 1 {
2726
mas.replaceCharacters(in:r, with: Date().description)
2827
stop.pointee = true
2928
}

bk2ch10p519TextFieldLeftViewTest/TextFieldLeftViewTest/ViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ViewController: UIViewController {
3737

3838
tf.borderStyle = .none
3939
// tf.borderStyle = .bezel
40-
// tf.borderStyle = .line
40+
tf.borderStyle = .line
4141
// tf.borderStyle = .roundedRect
4242
tf.background = UIImage(named:"yellowsilk4")
4343

bk2ch10p532textViewAndKeyboard/ch23p811selfSizingTextField/ViewController.swift

+24-17
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ func lend<T> (closure:(T)->()) -> T where T:NSObject {
99

1010
class ViewController: UIViewController, UITextViewDelegate {
1111
@IBOutlet var tv : UITextView!
12-
var scrollView : UIScrollView! {
12+
var sv : UIScrollView! {
1313
return self.tv
1414
}
1515
var oldContentInset = UIEdgeInsets.zero
1616
var oldIndicatorInset = UIEdgeInsets.zero
1717
var oldOffset = CGPoint.zero
18+
var prevr = CGRect.zero
1819

1920
override func viewDidLoad() {
2021
super.viewDidLoad()
@@ -72,42 +73,48 @@ class ViewController: UIViewController, UITextViewDelegate {
7273
ks = .exiting
7374
}
7475
}
76+
print(ks == .entering ? "entering" : ks == .exiting ? "exiting" : "unknown")
7577
return (ks, newRect)
7678
}
77-
78-
#error("needs updating")
79+
7980
@objc func keyboardShow(_ n:Notification) {
81+
print("show")
82+
guard self.traitCollection.userInterfaceIdiom == .phone else {return}
8083
let d = n.userInfo!
81-
let (state, rnew) = keyboardState(for:d, in:self.scrollView)
84+
let (state, rnew) = keyboardState(for:d, in:self.sv)
8285
if state == .entering {
8386
print("really showing")
84-
self.oldContentInset = self.scrollView.contentInset
85-
self.oldIndicatorInset = self.scrollView.scrollIndicatorInsets
86-
self.oldOffset = self.scrollView.contentOffset
87+
self.oldContentInset = self.sv.contentInset
88+
self.oldIndicatorInset = self.sv.verticalScrollIndicatorInsets
89+
self.oldOffset = self.sv.contentOffset
8790
}
88-
print("show")
8991
// no need to scroll, as the scroll view will do it for us
9092
// so all we have to do is adjust the inset
91-
if let rnew = rnew {
92-
let h = rnew.intersection(self.scrollView.bounds).height
93-
self.scrollView.contentInset.bottom = h
94-
self.scrollView.scrollIndicatorInsets.bottom = h
93+
if let rnew = rnew, rnew != self.prevr {
94+
print("adjusting")
95+
self.prevr = rnew
96+
let h = rnew.intersection(self.sv.bounds).height + 6 // ?
97+
self.sv.contentInset.bottom = h
98+
self.sv.verticalScrollIndicatorInsets.bottom = h
9599
}
96100
}
97101

98102
@objc func keyboardHide(_ n:Notification) {
103+
print("hide")
104+
guard self.traitCollection.userInterfaceIdiom == .phone else {return}
99105
let d = n.userInfo!
100-
let (state, _) = keyboardState(for:d, in:self.scrollView)
106+
let (state, _) = keyboardState(for:d, in:self.sv)
101107
if state == .exiting {
102108
print("really hiding")
103109
// restore original setup
104-
// we _don't_ do this; let the text view position itself
105-
// self.scrollView.contentOffset = self.oldOffset
106-
self.scrollView.scrollIndicatorInsets = self.oldIndicatorInset
107-
self.scrollView.contentInset = self.oldContentInset
110+
self.sv.contentOffset = self.oldOffset
111+
self.sv.verticalScrollIndicatorInsets = self.oldIndicatorInset
112+
self.sv.contentInset = self.oldContentInset
113+
self.prevr = .zero
108114
}
109115
}
110116

117+
111118

112119

113120
@objc func doDone(_ sender: Any) {

0 commit comments

Comments
 (0)