/
ViewController.swift
198 lines (164 loc) · 7.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import UIKit
import ImageIO
extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}
extension CGSize {
init(_ width:CGFloat, _ height:CGFloat) {
self.init(width:width, height:height)
}
}
extension CGPoint {
init(_ x:CGFloat, _ y:CGFloat) {
self.init(x:x, y:y)
}
}
extension CGVector {
init (_ dx:CGFloat, _ dy:CGFloat) {
self.init(dx:dx, dy:dy)
}
}
func lend<T> (closure:(T)->()) -> T where T:NSObject {
let orig = T()
closure(orig)
return orig
}
class ViewController : UIViewController {
@IBOutlet var tv : UITextView!
override func viewDidLoad() {
super.viewDidLoad()
print(self.tv.textContainerInset)
print(UITextView(frame:CGRect(0,0,100,100)).textContainerInset)
let s = "Onions\t$2.34\nPeppers\t$15.2\n"
let mas = NSMutableAttributedString(string:s, attributes:[
.font:UIFont(name:"GillSans", size:15)!,
.foregroundColor:UIColor.red,
//.foregroundColor:UIColor.label,
.paragraphStyle:lend {
(p:NSMutableParagraphStyle) in
let terms = NSTextTab.columnTerminators(for:Locale.current)
let tab = NSTextTab(textAlignment:.right, location:170, options:[.columnTerminators:terms])
var which : Int { return 1 }
switch which {
case 1:
p.tabStops = [tab]
case 2:
for oldTab in p.tabStops {
p.removeTabStop(oldTab)
}
p.addTabStop(tab)
default: break
}
p.firstLineHeadIndent = 20
}
])
self.tv.attributedText = mas
// return
let onions = self.thumbnailOfImage(name:"onion", extension:"jpg")
let peppers = self.thumbnailOfImage(name:"peppers", extension:"jpg")
let onionatt = NSTextAttachment()
onionatt.image = onions
onionatt.bounds = CGRect(0,-5,onions.size.width,onions.size.height)
let onionattchar = NSAttributedString(attachment:onionatt)
let pepperatt = NSTextAttachment()
pepperatt.image = peppers
pepperatt.bounds = CGRect(0,-1,peppers.size.width,peppers.size.height)
let pepperattchar = NSAttributedString(attachment:pepperatt)
let checkim = UIImage(systemName:"checkmark.circle")!.withRenderingMode(.alwaysOriginal)
// if you don't ask for always original, you don't get automatic tinting
// confirmed still true in iOS 14
let check = NSTextAttachment(image:checkim)
let checkchar = NSAttributedString(attachment:check)
let r = (mas.string as NSString).range(of:"Onions")
mas.insert(onionattchar, at:(r.location + r.length))
mas.insert(checkchar, at:(r.location + r.length))
let r2 = (mas.string as NSString).range(of:"Peppers")
mas.insert(pepperattchar, at:(r2.location + r2.length))
mas.insert(checkchar, at:(r2.location + r2.length))
mas.append(NSAttributedString(string: "\n\n", attributes:nil))
mas.append(NSAttributedString(string: "LINK", attributes: [
.link : URL(string: "https://www.apple.com")!,
.foregroundColor : UIColor.green,
.underlineStyle : NSUnderlineStyle.single.rawValue
]))
// aha! son of a gun, I finally figured this out!
// you have to set the overall `linkTextAttributes` to an empty dictionary
// note that self.tv.linkTextAttributes does NOT apply to these
mas.append(NSAttributedString(string: "\n\n", attributes:nil))
mas.append(NSAttributedString(string: "(805)-123-4567", attributes: nil))
mas.append(NSAttributedString(string: "\n\n", attributes:nil))
mas.append(NSAttributedString(string: "123 Main Street, Anytown, CA 91234", attributes: nil))
mas.append(NSAttributedString(string: "\n\n", attributes:nil))
mas.append(NSAttributedString(string: "tomorrow at 4 PM", attributes: nil))
self.tv.attributedText = mas
// this works but it applies to all links
print(self.tv.linkTextAttributes as Any)
self.tv.linkTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.orange]
// this allows individual link's attributes to work
self.tv.linkTextAttributes = [:]
// print(NSTextAttachment.character)
// print(0xFFFC)
self.tv.isSelectable = true
self.tv.isEditable = false
self.tv.delegate = self
do {
let mas = NSMutableAttributedString()
mas.append(NSAttributedString(string: "LINKNOT", attributes: [
.link : URL(string: "https://www.apple.com")!,
.foregroundColor : UIColor.green, // this is ignored????
.underlineStyle : NSUnderlineStyle.single.rawValue
]))
let lab = UILabel()
lab.attributedText = mas
lab.sizeToFit()
lab.isUserInteractionEnabled = true
self.view.addSubview(lab)
lab.frame.origin.y += 30
lab.frame.origin.x += 10
// just proving that (a) you can set this appearance, and (b) you can't tap it
}
}
func thumbnailOfImage(name:String, extension ext: String) -> UIImage {
let url = Bundle.main.url(forResource:name,
withExtension:ext)!
let src = CGImageSourceCreateWithURL(url as CFURL, nil)!
let scale = UIScreen.main.scale
let w : CGFloat = 20 * scale
let d : [CFString:Any] = [
kCGImageSourceShouldAllowFloat : true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: w
]
let imref =
CGImageSourceCreateThumbnailAtIndex(src, 0, d as NSDictionary)!
let im = UIImage(cgImage:imref, scale:scale, orientation:.up)
return im
}
}
extension UITextItemInteraction : CustomStringConvertible {
public var description: String {
switch self {
case .invokeDefaultAction: return "invokeDefaultAction"
case .presentActions: return "presentActions"
case .preview: return "preview"
@unknown default:
fatalError()
}
}
}
extension ViewController : UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction:UITextItemInteraction) -> Bool {
print("attachment", interaction)
if interaction == .preview {return false}
return true
}
func textView(_ textView: UITextView, shouldInteractWith url: Foundation.URL, in characterRange: NSRange, interaction:UITextItemInteraction) -> Bool {
print("URL", url)
print((textView.text as NSString).substring(with:characterRange))
print(interaction)
return true
}
}