-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathViewController.swift
198 lines (162 loc) · 7.59 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
//
// ViewController.swift
// SwiftVersion
//
// Created by BlueDancer on 2018/1/28.
// Copyright © 2018年 畅三江. All rights reserved.
//
import UIKit
private let reuseIdentifier = "Cell"
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var tipsLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!
var demosM: [SJDemoInfo] = [SJDemoInfo]()
override func viewDidLoad() {
super.viewDidLoad()
label.layer.borderColor = UIColor.black.cgColor
label.layer.borderWidth = 0.6
label.numberOfLines = 0
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
self.createExamples()
tableView.reloadData()
// Do any additional setup after loading the view, typically from a nib.
}
func createExamples() -> Void {
for i in 0...3 {
let model = SJDemoInfo.init()
switch (i) {
case 0:
model.name = "\(i) 常用方法"
let text = NSAttributedString.sj.makeText({ (make) in
make.font(.boldSystemFont(ofSize: 20)).textColor(.black).lineSpacing(8)
make.append(":Image - ")
make.append({ (make) in
make.image = UIImage.init(named: "sample2")
make.bounds = CGRect.init(x: 0, y: 0, width: 30, height: 30)
})
make.append("\n")
make.append(":UnderLine").underLine({ (make) in
make.style = .single
make.color = .green
})
make.append("\n")
make.append(":Strikethrough").strikethrough({ (make) in
make.style = .single
make.color = .green
})
make.append("\n")
make.append(":BackgroundColor").backgroundColor(.green)
make.append("\n")
make.append(":Kern").kern(6)
make.append("\n")
let shadow = NSShadow.init()
shadow.shadowColor = UIColor.red
shadow.shadowOffset = .init(width: 0, height: 1)
shadow.shadowBlurRadius = 5
make.append(":Shadow").shadow(shadow)
make.append("\n")
make.append(":Stroke").stroke({ (make) in
make.color = .red
make.width = 1
})
make.append("\n")
make.append("oOo").font(.boldSystemFont(ofSize: 25)).alignment(.center)
make.append("\n")
make.append("Regular Expression")
make.regex("Regular").update({ (make) in
make.font(.boldSystemFont(ofSize: 25)).textColor(.purple)
})
make.regex("ss").replace("SS")
make.regex("on").replace({ (make) in
make.append("ON😆").textColor(.red).backgroundColor(.green).font(.boldSystemFont(ofSize: 30))
})
});
model.size = text.sj_textSize(forPreferredMaxLayoutWidth: self.view.bounds.size.width - 80)
model.task = { return text }
break;
case 1:
model.name = "\(i) 正则匹配"
let text = NSAttributedString.sj.makeText({ (make) in
make.append("@迷你世界联机 :@江叔 用小淘气耍赖野人#迷你世界#")
make.regex("[@][^\\s]+\\s").update({ (make) in
make.font(.boldSystemFont(ofSize: 25)).textColor(.purple)
})
make.regex("[#][^#]+#").update({ (make) in
make.font(.boldSystemFont(ofSize: 25)).textColor(.purple)
})
})
model.size = text.sj_textSize(forPreferredMaxLayoutWidth: self.view.bounds.size.width - 80)
model.task = { return text }
break;
case 2:
model.name = "\(i) 上下图文"
let text = NSAttributedString.sj.makeText({ (make) in
make.alignment(NSTextAlignment.center).lineSpacing(8)
make.append({ (make) in
make.image = UIImage.init(named: "sample2")
make.bounds = CGRect.init(x: 0, y: 0, width: 30, height: 30)
})
make.append("\n999")
})
model.size = text.sj_textSize(forPreferredMaxLayoutWidth: self.view.bounds.size.width - 80)
model.task = { return text }
break;
default:
model.name = "\(i) 图文对齐"
let text = NSAttributedString.sj.makeText { (make) in
make.font(UIFont.systemFont(ofSize: 14))
make.append("Top->")
make.append({ (make) in
make.image = UIImage.init(named: "sample2")
make.alignment = .top
})
make.append("Center->")
make.append({ (make) in
make.image = UIImage.init(named: "sample2")
make.alignment = .center
})
make.append("Bottom->")
make.append({ (make) in
make.image = UIImage.init(named: "sample2")
make.alignment = .bottom
})
}
model.size = text.sj_textSize(forPreferredMaxLayoutWidth: self.view.bounds.size.width - 80)
model.task = { return text }
break;
}
demosM.append(model)
}
}
func _updateHeightConstraint(_ size: CGSize) -> Void {
widthConstraint.constant = size.width
heightConstraint.constant = size.height
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if ( nil != demosM[indexPath.row].task ) {
self.tipsLabel.text = demosM[indexPath.row].name
label.attributedText = demosM[indexPath.row].task!()
self._updateHeightConstraint(demosM[indexPath.row].size!)
}
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return demosM.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
cell.textLabel?.text = demosM[indexPath.row].name
return cell;
}
}