CNLiveShopAddGoodsSingleView.swift
23.5 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//
// CNLiveShopAddGoodsSingleView.swift
// metaCode
//
// Created by zx on 2023/11/8.
//
import Foundation
//商品标题最大长度
let TitleMaxLength = 50
/// 修改自身高度回调
typealias updateBlock = ()->Void
///修改添加通用参数回调
typealias changeFrameParamCallBack = (_ isDelect:Bool,_ model:CNLiveShopAddEditGoodsListModel,_ singleView:CNLiveShopAddGoodsSingleView)->Void
typealias tapClickBlock = (_ singleView:CNLiveShopAddGoodsSingleView)->Void
class CNLiveShopAddGoodsSingleView: UIView, UITextFieldDelegate, UITextViewDelegate {
var updateFrameBlock:updateBlock?
///
var changeFrameBlock:changeFrameParamCallBack?
//
var tapBlock:tapClickBlock?
//model
var _addEditGoodsListModel : CNLiveShopAddEditGoodsListModel?
var addEditGoodsListModel : CNLiveShopAddEditGoodsListModel? {
get{
_addEditGoodsListModel
}
set{
_addEditGoodsListModel = newValue!
self.setupNewUI(model: _addEditGoodsListModel!)
}
}
///标题
lazy var titleLabel: UILabel = {
let titleLabel = UILabel.init(frame: CGRectMake(15 , 15, 145, 20))
return titleLabel
}()
//副标题
var subButton = UIButton()
var contentTextField = UITextField()
var delectButton = UIButton()
var rightImageView = UIImageView()
var contentSwitch = UISwitch()
var uploadImageView = CNLiveAddGoodsSelectUploadImageView()
var contentTextBgView = CNLiveRoundedRectView()
var contentTextView = CNLiveShopTextView()
var addBtn = UIButton()
var subtractBtn = UIButton()
var countTF = UITextField()
var descLab = UILabel()
var line = UIImageView()
var starImageView = UIImageView()
//开始输入前的文本
var beginText:String?
init(frame: CGRect,model:CNLiveShopAddEditGoodsListModel) {
super.init(frame: frame)
self.addEditGoodsListModel = model
NotificationCenter.default.addObserver(self, selector: #selector(textFieldTextDidChangeNotification), name: Notification.Name("UITextFieldTextDidChangeNotification"), object:nil)
self.addObserver(self, forKeyPath: "tag",options: [.new,.old],context: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: Notification.Name("UITextFieldTextDidChangeNotification"), object: nil)
self.removeObserver(self, forKeyPath: "tag")
}
func setupNewUI(model:CNLiveShopAddEditGoodsListModel){
self.backgroundColor = model.style == "2" ? HexColor("#F6F6F6") : HexColor("#FFFFFF")
if (model.isHaveTo ?? false) {
self.starImageView = UIImageView()
self.starImageView.image = UIImage(named: "DS_shop_manageGoodsDetails_star")
self.starImageView.clipsToBounds = true
self.starImageView.contentMode = .scaleAspectFit
self.addSubview(self.starImageView)
}
self.titleLabel = UILabel()
if (self.addEditGoodsListModel?.style == "2") {
self.titleLabel.textColor = HexColor("#9D9D9D")
self.titleLabel.text = self.addEditGoodsListModel?.title
if (self.addEditGoodsListModel!.touchSubtitle?.count ?? 0 > 0) {
let subBtn = UIButton(type: .custom)
subBtn.frame = CGRect(x: self.width - 50, y: 0, width: 50, height: 50)
subBtn.setTitle("添加通用参数", for: .normal)
subBtn.setTitleColor(HexColor("#0091FF"), for: .normal)
subBtn.titleLabel?.font = Font_14
subBtn.titleLabel?.textAlignment = .right
subBtn.addTarget(self, action: #selector(selectAddParamAction), for: .touchUpInside)
self.subButton = subBtn
self.addSubview(self.subButton)
}
}else
{
self.titleLabel.textColor = HexColor("#333333")
if (self.addEditGoodsListModel?.subtitle?.count ?? 0 > 0) {
self.titleLabel.text = "\(self.addEditGoodsListModel?.title ?? "")\(self.addEditGoodsListModel?.subtitle ?? "")"
}else{
self.titleLabel.text = self.addEditGoodsListModel?.title ?? ""
}
}
self.titleLabel.font = Font_14
self.addSubview(self.titleLabel)
if (self.addEditGoodsListModel?.style == "1") {
let contentTextField = UITextField()
contentTextField.returnKeyType = .done
contentTextField.placeholder = "请输入"
contentTextField.font = Font_14
contentTextField.textColor = HexColor(model.contentTextColor ?? "#787878")
contentTextField.delegate = self
if (model.content?.count ?? 0 > 0){ contentTextField.text = model.content
}
if (model.contentTextFieldkeyboardType == "2") {
contentTextField.keyboardType = .decimalPad
}else {
contentTextField.keyboardType = .default
}
self.contentTextField = contentTextField
if (model.contentType == "1") {
self.contentTextField.isUserInteractionEnabled = true
if (model.isDelect ?? false) {
self.delectButton = UIButton(type: .custom)
self.delectButton.setTitle("删除", for: .normal)
self.delectButton.setTitleColor(HexColor("#0091FF"), for: .normal)
self.delectButton.titleLabel?.font = Font_14
self.delectButton.titleLabel?.textAlignment = .right
self.delectButton.addTarget(self, action: #selector(selectDelectParamAction), for: .touchUpInside)
self.addSubview(self.delectButton)
}
}else if (model.contentType == "2") {
self.contentTextField.isUserInteractionEnabled = false
self.rightImageView = UIImageView()
self.rightImageView.image = UIImage(named: "DS_shop_manageGoodsDetails_rightArrow")
self.rightImageView.contentMode = .scaleAspectFit
self.rightImageView.clipsToBounds = true
self.addSubview(self.rightImageView)
self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(selfTap)))
}else {
self.contentTextField.isUserInteractionEnabled = false
self.contentTextField.isHidden = true
self.contentSwitch = UISwitch()
self.contentSwitch.transform = CGAffineTransformMakeScale(0.7, 0.7)
//不写到layoutSubviews中是因为 会变位置
self.contentSwitch.top = (50-self.contentSwitch.height)*0.5
self.contentSwitch.onTintColor = HexColor("#61C464")
self.contentSwitch.tintColor = HexColor("#DBDEDB")
self.contentSwitch.isOn = true
self.contentSwitch.addTarget(self, action: #selector(switchValueChanged), for: .valueChanged)
self.addSubview(self.contentSwitch)
}
self.addSubview(self.contentTextField)
self.height = 50
}else if (model.style == "2") {
self.height = 50
}else if (model.style == "3") {
self.uploadImageView = CNLiveAddGoodsSelectUploadImageView(frame: CGRectZero)
self.uploadImageView.column = 3
self.uploadImageView.maxSelectImageCount = (model.maxSelectImageCount! as NSString).integerValue
self.uploadImageView.isAbleSelectVideo = model.isAbleSelectVideo ?? false
self.uploadImageView.isDisplySelectCover = model.isAbleSelectCover ?? false
if model.id?.isEmpty == true {
self.uploadImageView.deleteImage = UIImage(named: "DS_shop_manageGoodsDetails_delete")
}
self.uploadImageView.changeFrameCallBack = {[weak self] in
self?.height = (self?.uploadImageView.bottom)! + 15
self?.updateFrameBlock!()
}
self.addSubview(self.uploadImageView)
}else if (model.style == "4") {
self.contentTextBgView = CNLiveRoundedRectView()
self.contentTextBgView.fillColor = HexColor("F9F9F9")
self.contentTextBgView.radius = 8
self.addSubview(self.contentTextBgView)
self.contentTextView = CNLiveShopTextView()
self.contentTextView.placeHolder = model.placeholder ?? ""
self.contentTextView.placeHolderColor = HexColor("c6c6c6") ?? .gray
self.contentTextView.font = Font_14
self.contentTextView.textColor = HexColor("787878")
self.contentTextView.delegate = self
if (model.content?.count ?? 0 > 0){ self.contentTextView.text = model.content
}
self.contentTextBgView.addSubview(self.contentTextView)
self.height = 120
}else if (model.style == "5") {
//添加输入框
self.addBtn = UIButton(type: .custom)
self.addBtn.setImage(UIImage(named: "DS_main_add_count"), for: .normal)
self.addBtn.imageView?.contentMode = .scaleAspectFit
self.addBtn.addTarget(self, action: #selector(addBtnClick), for: .touchUpInside)
self.addSubview(self.addBtn)
self.subtractBtn = UIButton(type: .custom)
self.subtractBtn.setImage(UIImage(named: "DS_main_unadd_count_n"), for: .normal)
self.subtractBtn.imageView?.contentMode = .scaleAspectFit
self.subtractBtn.addTarget(self, action: #selector(subtractBtnClick), for: .touchUpInside)
self.addSubview(self.subtractBtn)
self.countTF = UITextField()
self.countTF.text = "1"
self.countTF.textAlignment = .center
self.countTF.font = Font_15
self.countTF.borderStyle = .none
self.countTF.delegate = self
self.countTF.layer.masksToBounds = true
self.countTF.layer.cornerRadius = 5
self.countTF.keyboardType = .numberPad
self.countTF.backgroundColor = HexColor("#F1F1F1")
self.countTF.addTarget(self, action: #selector(textDidChange(sender:)), for: .editingChanged)
self.addSubview(self.countTF)
self.descLab = UILabel()
self.descLab.textColor = HexColor("#C4C4C6)")
self.descLab.font = Font_10
self.descLab.text = model.desc
self.addSubview(self.descLab)
self.height = 60
}
if (model.isDisplayLine ?? false) {
self.line = UIImageView()
self.line.backgroundColor = HexColor("#e6e6e6)")
self.addSubview(self.line)
}
}
override func layoutSubviews() {
super.layoutSubviews()
self.titleLabel.frame = CGRect(x: 15, y: 15, width: 145, height: 20)
if (self.addEditGoodsListModel!.desc?.count ?? 0 > 0 && self.addEditGoodsListModel!.style == "5") {
self.descLab.frame = CGRect(x: 15, y: 35, width: 145, height: 20)
}
self.starImageView.frame = CGRect(x: self.titleLabel.left-3-5, y: 0, width: 5, height: 5)
self.starImageView.centerY = self.titleLabel.centerY
if (self.addEditGoodsListModel?.style == "2"){
self.subButton.frame = CGRect(x: KSreenWidth-130, y: 15, width: 120, height: 20)
}
if (self.addEditGoodsListModel!.style == "1") {
if (self.addEditGoodsListModel!.contentType == "1") {
if (self.addEditGoodsListModel!.isDelect ?? false) {
self.delectButton.frame = CGRect(x: KSreenWidth-50, y: 0, width: 40, height: 50)
self.contentTextField.frame = CGRect(x: self.titleLabel.right+15, y: 0, width: self.width-self.titleLabel.right-15*2-40, height: 50)
}else{
self.contentTextField.frame = CGRect(x: self.titleLabel.right+15, y: 0, width: self.width-self.titleLabel.right-15*2, height: 50)
}
}else if (self.addEditGoodsListModel!.contentType == "2") {
let contentTextFieldMaxW = self.width - self.titleLabel.right-15*3-2
self.contentTextField.sizeToFit()
if (self.contentTextField.width > contentTextFieldMaxW) {
self.contentTextField.width = contentTextFieldMaxW
}
self.contentTextField.left = self.titleLabel.right + 15
self.contentTextField.centerY = self.titleLabel.centerY
self.rightImageView.frame = CGRect(x: self.contentTextField.right+2, y: 0, width: 15, height: 15)
self.rightImageView.centerY = self.titleLabel.centerY
}else {
self.contentSwitch.left = self.titleLabel.right+15
}
self.addSubview(self.contentTextField)
}else if (self.addEditGoodsListModel!.style == "3") {
self.uploadImageView.frame = CGRect(x: 0, y: self.titleLabel.bottom+2, width: self.width, height: 50)
if (self.height == 0) {
self.height = self.uploadImageView.bottom+15
if ((self.updateFrameBlock) != nil) {
self.updateFrameBlock!()
}
}
}else if (self.addEditGoodsListModel!.style == "4") {
self.contentTextBgView.frame = CGRect(x: 15, y: self.titleLabel.bottom+10, width: self.width - 15*2, height: 60)
self.contentTextView.frame = CGRect(x: 10, y: 10, width: self.contentTextBgView.width-10*2, height: self.contentTextBgView.height-10*2)
}else if (self.addEditGoodsListModel!.style == "5") {
self.subtractBtn.frame = CGRect(x: 15+self.titleLabel.right, y: 15, width: 30, height: 30)
self.countTF.frame = CGRect(x: self.subtractBtn.right+2, y: self.subtractBtn.top+5, width: 55, height: 20)
self.addBtn.frame = CGRect(x: self.countTF.right+2, y: self.subtractBtn.top, width: 30, height: 30)
}
self.line.frame = CGRect(x: 15, y: self.height-1, width: self.width-15*2, height: 1)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - KVO
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let newTagValue = change?[NSKeyValueChangeKey.newKey] as! Int
if newTagValue == 5 {
self.contentTextField.placeholder = "请输入(最多输入50个字)"
}else if newTagValue == 6{
self.contentTextView.placeHolder = "请在此输入...(备注:最多输入30个字)"
}
}
// MARK: - BtnAction
@objc func selectAddParamAction(){
if ((self.changeFrameBlock) != nil) {
self.changeFrameBlock!(false, self.addEditGoodsListModel!, self)
}
}
@objc func selectDelectParamAction(){
AlertTool.showSystemAlert(title: "确认删除?", message: nil, actionTitles: ["取消","确定"], style: .alert, actionStyles:[.cancel,.default] ) { index in
if (index == 2) {
if ((self.changeFrameBlock) != nil) {
self.changeFrameBlock!(true, self.addEditGoodsListModel!, self)
}
}
}
}
@objc func selfTap(){
if ((self.tapBlock) != nil) {
self.tapBlock!(self)
}
}
@objc func switchValueChanged(){
}
@objc func addBtnClick(){
if (self.countTF.text?.count ?? 0 > 0 && self.countTF.text?.isPurnInt() == true){
var count = Int(self.countTF.text!)!
count = count + 1
self.countTF.text = "\(count)"
self.subtractBtn.setImage(UIImage(named: "DS_main_unadd_count"), for: .normal)
}
}
@objc func subtractBtnClick(){
if (self.countTF.text?.count ?? 0 > 0 && self.countTF.text?.isPurnInt() == true){
var count = Int(self.countTF.text!)!
if (count <= 2){
self.countTF.text = "1"
subtractBtn.setImage(UIImage(named: "DS_main_unadd_count_n"), for: .normal)
}else{
count = count - 1
self.countTF.text = "\(count)"
subtractBtn.setImage(UIImage(named: "DS_main_unadd_count"), for: .normal)
}
}
}
// MARK: - UITextFieldDelegate
@objc func textDidChange(sender:UITextField) {
if (sender == self.countTF) {
if (self.countTF.text?.count ?? 0 > 0 && (self.countTF.text?.isPurnInt() == true)) {
let count:Int = (self.countTF.text! as NSString).integerValue
if (count <= 1) {
self.subtractBtn.setImage(UIImage(named: "DS_main_unadd_count_n"), for: .normal)
}else{
self.subtractBtn.setImage(UIImage(named: "DS_main_unadd_count"), for: .normal)
}
}
}
}
func textViewDidEndEditing(_ textView: UITextView) {
if self.stringContainsEmoji(string: textView.text){
showMessage(message: "输入内容不能包含表情")
}else{
if textView.text.count > 0{
self.addEditGoodsListModel?.content = textView.text
}
}
}
// MARK: - UITextFieldDelegate
func textFieldDidBeginEditing(_ textField: UITextField) {
if self.addEditGoodsListModel?.style == "1"{
if self.contentTextField == textField && self.addEditGoodsListModel?.contentTextFieldkeyboardType == "2"{
self.beginText = textField.text ?? ""
}
}
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if textField.text?.count ?? 0 > 0{
self.addEditGoodsListModel?.content = textField.text
}
if self.stringContainsEmoji(string: textField.text ?? ""){
showMessage(message: "输入内容不能包含表情")
}
if textField == self.countTF && (self.countTF.text?.count == 0 || Int(self.countTF.text ?? "0") ?? 0 <= 0){
showMessage(message: "最低起售不能为空")
self.subtractBtn.setImage(UIImage(named: "DS_main_unadd_count_n"), for: .normal)
return false
}
if textField == self.countTF{
if (self.countTF.text?.count ?? 0 > 0 && self.countTF.text?.isPurnInt() == true){
let count = Int(self.countTF.text ?? "0")!
if count > 255{
showMessage(message: "最大起售数不能超过255")
return false
}
}
}
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == self.countTF{
if (self.countTF.text?.count ?? 0 > 0 && self.countTF.text?.isPurnInt() == true){
let count = Int(self.countTF.text ?? "0")!
if count > 255{
showMessage(message: "最大起售数不能超过255")
return false
}
}
}
keyWindow?.endEditing(true)
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.isEmpty{
//点击的删除键
return true
}
if (self.tag == 5 && textField.text?.count ?? 0 >= TitleMaxLength || (textField.text?.count ?? 0) + string.count > TitleMaxLength){
return false
}
if self.addEditGoodsListModel?.style == "1"{
if self.contentTextField == textField && self.addEditGoodsListModel?.contentTextFieldkeyboardType == "2"{
return self.allowWriteString(string: "0123456789.")
}
}
if self.stringContainsEmoji(string: textField.text ?? ""){
showMessage(message: "输入内容不能包含表情")
return false
}
return true
}
@objc func textFieldTextDidChangeNotification(notification:Notification){
let textField = notification.object as! UITextField
if self.addEditGoodsListModel?.style == "1"{
if (self.contentTextField == textField && self.addEditGoodsListModel?.contentTextFieldkeyboardType == "2"){
let oldStr = self.beginText
var newStr = textField.text
if newStr?.count ?? 0 > 0{
let newFirstStr = newStr?.subString(start: 0, length:1)
if newFirstStr == "."{
newStr = "0%\(newFirstStr ?? ".")"
textField.text = newStr
return
}
}
var isFindDecimalPoint = false
var behindDecimalPointLength = 0
if newStr?.count ?? 0 > oldStr!.count{
for i in 0..<(newStr?.count ?? 0) {
let newNextStr = newStr?.subString(start: i, length: 1)
if (!isFindDecimalPoint && newNextStr == "."){
isFindDecimalPoint = true
}else if isFindDecimalPoint{
if (behindDecimalPointLength >= Int((self.addEditGoodsListModel?.decimalLength!)!)! || newNextStr == "."){
textField.text = newStr?.subString(start: 0, length: i+1)
return
}
behindDecimalPointLength += 1
}
}
}
}
}
}
// MARK: - UITextViewDelegate
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text.isEmpty{
//点击的删除键
return true
}
if self.stringContainsEmoji(string: textView.text){
showMessage(message: "输入内容不能包含表情")
return false
}
return true
}
// MARK: - 是否包含表情
func stringContainsEmoji(string:String) -> Bool {
for scalar in string.unicodeScalars {
switch scalar.value {
case 0x1F600...0x1F64F, // Emoticons
0x1F300...0x1F5FF, // Misc Symbols and Pictographs
0x1F680...0x1F6FF, // Transport and Map
0x1F1E6...0x1F1FF, // Regional country flags
0x2600...0x26FF, // Misc symbols
0x2700...0x27BF, // Dingbats
0xFE00...0xFE0F, // Variation Selectors
0x1F900...0x1F9FF, // Supplemental Symbols and Pictographs
127000...127600, // Various asian characters
65024...65039, // Variation selector
9100...9300, // Misc items
8400...8447: // Combining Diacritical Marks for Symbols
return true
default:
continue
}
}
return false
}
/**
查看允不允许输入 例如 允许输入1、2时传入12
*/
func allowWriteString(string:String)->Bool{
//判断输入的是否是数字 否则无效
var cs = CharacterSet()
// 获取除以下数字所有的字符集合
cs = CharacterSet(charactersIn: string).inverted
let filterArr = string.components(separatedBy: cs)
// 对比一下是否为数字
return filterArr.first!.count == string.count ? true:false
}
}