Skip to content

Commit 9e3761b

Browse files
committed
Text stickers support display background color
1 parent b0fe011 commit 9e3761b

18 files changed

+612
-287
lines changed

Sources/Extensions/UIColor+ZLImageEditor.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,14 @@ extension ZLImageEditorWrapper where Base: UIColor {
6363
ZLImageEditorUIConfiguration.default().toolIconHighlightedColor
6464
}
6565
}
66+
67+
extension ZLImageEditorWrapper where Base: UIColor {
68+
/// - Parameters:
69+
/// - r: 0~255
70+
/// - g: 0~255
71+
/// - b: 0~255
72+
/// - a: 0~1
73+
static func rgba(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat = 1) -> UIColor {
74+
return UIColor(red: r / 255, green: g / 255, blue: b / 255, alpha: a)
75+
}
76+
}

Sources/Extensions/UIImage+ZLImageEditor.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,18 @@ extension ZLImageEditorWrapper where Base: UIImage {
388388
}
389389
}
390390

391+
extension ZLImageEditorWrapper where Base: UIImage {
392+
static func getImage(_ named: String) -> UIImage? {
393+
if ZLCustomImageDeploy.imageNames.contains(named), let image = UIImage(named: named) {
394+
return image
395+
}
396+
if let image = ZLCustomImageDeploy.imageForKey[named] {
397+
return image
398+
}
399+
return UIImage(named: named, in: Bundle.zlImageEditorBundle, compatibleWith: nil)
400+
}
401+
}
402+
391403
extension ZLImageEditorWrapper where Base: CIImage {
392404
func toUIImage() -> UIImage? {
393405
let context = CIContext()

Sources/General/ZLAdjustSlider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ZLAdjustSlider: UIView {
4646

4747
lazy var separator: UIView = {
4848
let view = UIView()
49-
view.backgroundColor = zlRGB(230, 230, 230)
49+
view.backgroundColor = .zl.rgba(230, 230, 230)
5050
return view
5151
}()
5252

Sources/General/ZLBaseStickerView.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ protocol ZLStickerViewAdditional: NSObject {
5252
func addScale(_ scale: CGFloat)
5353
}
5454

55-
enum ZLStickerLayout {
56-
static let borderWidth = 1 / UIScreen.main.scale
57-
static let edgeInset: CGFloat = 20
58-
}
59-
6055
class ZLBaseStickerView<T>: UIView, UIGestureRecognizerDelegate {
6156
private enum Direction: Int {
6257
case up = 0
@@ -65,6 +60,8 @@ class ZLBaseStickerView<T>: UIView, UIGestureRecognizerDelegate {
6560
case left = 270
6661
}
6762

63+
var borderWidth = 1 / UIScreen.main.scale
64+
6865
var firstLayout = true
6966

7067
let originScale: CGFloat
@@ -137,7 +134,7 @@ class ZLBaseStickerView<T>: UIView, UIGestureRecognizerDelegate {
137134
self.gesRotation = gesRotation
138135
self.totalTranslationPoint = totalTranslationPoint
139136

140-
borderView.layer.borderWidth = ZLStickerLayout.borderWidth
137+
borderView.layer.borderWidth = borderWidth
141138
hideBorder()
142139
if showBorder {
143140
startTimer()

Sources/General/ZLClipImageViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,10 @@ class ZLClipImageViewController: UIViewController {
309309
bottomToolView.layer.addSublayer(bottomShadowLayer)
310310

311311
bottomToolLineView = UIView()
312-
bottomToolLineView.backgroundColor = zlRGB(240, 240, 240)
312+
bottomToolLineView.backgroundColor = .zl.rgba(240, 240, 240)
313313
bottomToolView.addSubview(bottomToolLineView)
314314

315-
cancelBtn.setImage(getImage("zl_close"), for: .normal)
315+
cancelBtn.setImage(.zl.getImage("zl_close"), for: .normal)
316316
cancelBtn.adjustsImageWhenHighlighted = false
317317
cancelBtn.enlargeInset = 20
318318
cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
@@ -325,13 +325,13 @@ class ZLClipImageViewController: UIViewController {
325325
revertBtn.addTarget(self, action: #selector(revertBtnClick), for: .touchUpInside)
326326
bottomToolView.addSubview(revertBtn)
327327

328-
doneBtn.setImage(getImage("zl_right"), for: .normal)
328+
doneBtn.setImage(.zl.getImage("zl_right"), for: .normal)
329329
doneBtn.adjustsImageWhenHighlighted = false
330330
doneBtn.enlargeInset = 20
331331
doneBtn.addTarget(self, action: #selector(doneBtnClick), for: .touchUpInside)
332332
bottomToolView.addSubview(doneBtn)
333333

334-
rotateBtn.setImage(getImage("zl_rotateimage"), for: .normal)
334+
rotateBtn.setImage(.zl.getImage("zl_rotateimage"), for: .normal)
335335
rotateBtn.adjustsImageWhenHighlighted = false
336336
rotateBtn.enlargeInset = 20
337337
rotateBtn.addTarget(self, action: #selector(rotateBtnClick), for: .touchUpInside)

Sources/General/ZLEditImageViewController.swift

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ open class ZLEditImageViewController: UIViewController {
8686

8787
public var drawColViewH: CGFloat = 50
8888

89-
public var filterColViewH: CGFloat = 80
89+
public var filterColViewH: CGFloat = 90
9090

9191
public var adjustColViewH: CGFloat = 60
9292

@@ -136,7 +136,7 @@ open class ZLEditImageViewController: UIViewController {
136136

137137
open lazy var cancelBtn: ZLEnlargeButton = {
138138
let btn = ZLEnlargeButton(type: .custom)
139-
btn.setImage(getImage("zl_retake"), for: .normal)
139+
btn.setImage(.zl.getImage("zl_retake"), for: .normal)
140140
btn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
141141
btn.adjustsImageWhenHighlighted = false
142142
btn.enlargeInset = 30
@@ -157,8 +157,8 @@ open class ZLEditImageViewController: UIViewController {
157157

158158
open lazy var revokeBtn: UIButton = {
159159
let btn = UIButton(type: .custom)
160-
btn.setImage(getImage("zl_revoke_disable"), for: .disabled)
161-
btn.setImage(getImage("zl_revoke"), for: .normal)
160+
btn.setImage(.zl.getImage("zl_revoke_disable"), for: .disabled)
161+
btn.setImage(.zl.getImage("zl_revoke"), for: .normal)
162162
btn.adjustsImageWhenHighlighted = false
163163
btn.isEnabled = false
164164
btn.isHidden = true
@@ -200,7 +200,7 @@ open class ZLEditImageViewController: UIViewController {
200200
return view
201201
}()
202202

203-
open lazy var ashbinImgView = UIImageView(image: getImage("zl_ashbin"), highlightedImage: getImage("zl_ashbin_open"))
203+
open lazy var ashbinImgView = UIImageView(image: .zl.getImage("zl_ashbin"), highlightedImage: .zl.getImage("zl_ashbin_open"))
204204

205205
var adjustSlider: ZLAdjustSlider?
206206

@@ -392,11 +392,11 @@ open class ZLEditImageViewController: UIViewController {
392392

393393
var stickers: [UIView?] = Array(repeating: nil, count: teStic.count + imStic.count)
394394
teStic.forEach { cache in
395-
let v = ZLTextStickerView(from: cache.state)
395+
let v = ZLTextStickerView(state: cache.state)
396396
stickers[cache.index] = v
397397
}
398398
imStic.forEach { cache in
399-
let v = ZLImageStickerView(from: cache.state)
399+
let v = ZLImageStickerView(state: cache.state)
400400
stickers[cache.index] = v
401401
}
402402

@@ -439,18 +439,18 @@ open class ZLEditImageViewController: UIViewController {
439439
topShadowLayer.frame = topShadowView.bounds
440440
cancelBtn.frame = CGRect(x: 30, y: insets.top + 10, width: 28, height: 28)
441441

442-
bottomShadowView.frame = CGRect(x: 0, y: view.zl.height - 140 - insets.bottom, width: view.zl.width, height: 140 + insets.bottom)
442+
bottomShadowView.frame = CGRect(x: 0, y: view.zl.height - 150 - insets.bottom, width: view.zl.width, height: 150 + insets.bottom)
443443
bottomShadowLayer.frame = bottomShadowView.bounds
444444

445445
if canRedo, let redoBtn = redoBtn {
446-
redoBtn.frame = CGRect(x: view.zl.width - 15 - 35, y: 30, width: 35, height: 30)
447-
revokeBtn.frame = CGRect(x: redoBtn.zl.left - 10 - 35, y: 30, width: 35, height: 30)
446+
redoBtn.frame = CGRect(x: view.zl.width - 15 - 35, y: 40, width: 35, height: 30)
447+
revokeBtn.frame = CGRect(x: redoBtn.zl.left - 10 - 35, y: 40, width: 35, height: 30)
448448
} else {
449-
revokeBtn.frame = CGRect(x: view.zl.width - 15 - 35, y: 30, width: 35, height: 30)
449+
revokeBtn.frame = CGRect(x: view.zl.width - 15 - 35, y: 40, width: 35, height: 30)
450450
}
451-
drawColorCollectionView?.frame = CGRect(x: 20, y: 20, width: revokeBtn.zl.left - 20 - 10, height: drawColViewH)
451+
drawColorCollectionView?.frame = CGRect(x: 20, y: 30, width: revokeBtn.zl.left - 20 - 10, height: drawColViewH)
452452

453-
adjustCollectionView?.frame = CGRect(x: 20, y: 10, width: view.zl.width - 40, height: adjustColViewH)
453+
adjustCollectionView?.frame = CGRect(x: 20, y: 20, width: view.zl.width - 40, height: adjustColViewH)
454454
if ZLImageEditorUIConfiguration.default().adjustSliderType == .vertical {
455455
adjustSlider?.frame = CGRect(x: view.zl.width - 60, y: view.zl.height / 2 - 100, width: 60, height: 200)
456456
} else {
@@ -479,7 +479,7 @@ open class ZLEditImageViewController: UIViewController {
479479
height: 25
480480
)
481481

482-
let toolY: CGFloat = 85
482+
let toolY: CGFloat = 95
483483

484484
let doneBtnH = ZLImageEditorLayout.bottomToolBtnH
485485
let doneBtnW = localLanguageTextValue(.editFinish).zl.boundingRect(font: ZLImageEditorLayout.bottomToolTitleFont, limitSize: CGSize(width: CGFloat.greatestFiniteMagnitude, height: doneBtnH)).width + 20
@@ -592,10 +592,10 @@ open class ZLEditImageViewController: UIViewController {
592592

593593
if tools.contains(.draw) {
594594
let drawColorLayout = UICollectionViewFlowLayout()
595-
let drawColorItemWidth: CGFloat = 30
595+
let drawColorItemWidth: CGFloat = 36
596596
drawColorLayout.itemSize = CGSize(width: drawColorItemWidth, height: drawColorItemWidth)
597-
drawColorLayout.minimumLineSpacing = 15
598-
drawColorLayout.minimumInteritemSpacing = 15
597+
drawColorLayout.minimumLineSpacing = 0
598+
drawColorLayout.minimumInteritemSpacing = 0
599599
drawColorLayout.scrollDirection = .horizontal
600600
let drawColorTopBottomInset = (drawColViewH - drawColorItemWidth) / 2
601601
drawColorLayout.sectionInset = UIEdgeInsets(top: drawColorTopBottomInset, left: 0, bottom: drawColorTopBottomInset, right: 0)
@@ -605,7 +605,6 @@ open class ZLEditImageViewController: UIViewController {
605605
drawCV.delegate = self
606606
drawCV.dataSource = self
607607
drawCV.isHidden = true
608-
drawCV.showsHorizontalScrollIndicator = false
609608
bottomShadowView.addSubview(drawCV)
610609

611610
ZLDrawColorCell.zl.register(drawCV)
@@ -621,17 +620,17 @@ open class ZLEditImageViewController: UIViewController {
621620
}
622621

623622
let filterLayout = UICollectionViewFlowLayout()
624-
filterLayout.itemSize = CGSize(width: filterColViewH - 20, height: filterColViewH)
623+
filterLayout.itemSize = CGSize(width: filterColViewH - 30, height: filterColViewH - 10)
625624
filterLayout.minimumLineSpacing = 15
626625
filterLayout.minimumInteritemSpacing = 15
627626
filterLayout.scrollDirection = .horizontal
627+
filterLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
628628

629629
let filterCV = UICollectionView(frame: .zero, collectionViewLayout: filterLayout)
630630
filterCV.backgroundColor = .clear
631631
filterCV.delegate = self
632632
filterCV.dataSource = self
633633
filterCV.isHidden = true
634-
filterCV.showsHorizontalScrollIndicator = false
635634
bottomShadowView.addSubview(filterCV)
636635

637636
ZLFilterImageCell.zl.register(filterCV)
@@ -677,8 +676,8 @@ open class ZLEditImageViewController: UIViewController {
677676
bottomShadowView.addSubview(revokeBtn)
678677
if canRedo {
679678
let btn = UIButton(type: .custom)
680-
btn.setImage(getImage("zl_redo_disable"), for: .disabled)
681-
btn.setImage(getImage("zl_redo"), for: .normal)
679+
btn.setImage(.zl.getImage("zl_redo_disable"), for: .disabled)
680+
btn.setImage(.zl.getImage("zl_redo"), for: .normal)
682681
btn.adjustsImageWhenHighlighted = false
683682
btn.isEnabled = false
684683
btn.isHidden = true
@@ -734,9 +733,9 @@ open class ZLEditImageViewController: UIViewController {
734733
}
735734

736735
ZLImageEditorConfiguration.default().fontChooserContainerView?.selectFontBlock = { [weak self] font in
737-
self?.showInputTextVC(font: font) { [weak self] text, font, textColor, bgColor in
738-
self?.addTextStickersView(text, textColor: textColor, font: font, bgColor: bgColor)
739-
}
736+
self?.showInputTextVC(font: font, completion: { [weak self] text, textColor, font, image, style in
737+
self?.addTextStickersView(text, textColor: textColor, font: font, image: image, style: style)
738+
})
740739
}
741740
}
742741

@@ -840,8 +839,8 @@ open class ZLEditImageViewController: UIViewController {
840839
setToolView(show: false)
841840
fontChooserContainerIsHidden = false
842841
} else {
843-
showInputTextVC { [weak self] text, _, textColor, bgColor in
844-
self?.addTextStickersView(text, textColor: textColor, bgColor: bgColor)
842+
showInputTextVC { [weak self] text, textColor, font, image, style in
843+
self?.addTextStickersView(text, textColor: textColor, font: font, image: image, style: style)
845844
}
846845
}
847846
}
@@ -919,7 +918,7 @@ open class ZLEditImageViewController: UIViewController {
919918
var textStickers: [(ZLTextStickerState, Int)] = []
920919
var imageStickers: [(ZLImageStickerState, Int)] = []
921920
for (index, view) in stickersContainer.subviews.enumerated() {
922-
if let ts = view as? ZLTextStickerView, let _ = ts.label.text {
921+
if let ts = view as? ZLTextStickerView, !ts.text.isEmpty {
923922
textStickers.append((ts.state, index))
924923
} else if let ts = view as? ZLImageStickerView {
925924
imageStickers.append((ts.state, index))
@@ -1177,7 +1176,7 @@ open class ZLEditImageViewController: UIViewController {
11771176
toolViewStateTimer = nil
11781177
}
11791178

1180-
func showInputTextVC(_ text: String? = nil, textColor: UIColor? = nil, font: UIFont? = nil, bgColor: UIColor? = nil, completion: @escaping (String, UIFont, UIColor, UIColor) -> Void) {
1179+
func showInputTextVC(_ text: String? = nil, textColor: UIColor? = nil, font: UIFont? = nil, style: ZLInputTextStyle = .normal, completion: @escaping (String, UIColor, UIFont, UIImage?, ZLInputTextStyle) -> Void) {
11811180
var bgImage: UIImage?
11821181
autoreleasepool {
11831182
// Calculate image displayed frame on the screen.
@@ -1196,10 +1195,10 @@ open class ZLEditImageViewController: UIViewController {
11961195
.zl.clipImage(angle: 0, editRect: r, isCircle: isCircle)
11971196
}
11981197

1199-
let vc = ZLInputTextViewController(image: bgImage, text: text, font: font, textColor: textColor, bgColor: bgColor)
1198+
let vc = ZLInputTextViewController(image: bgImage, text: text, font: font, textColor: textColor, style: style)
12001199

1201-
vc.endInput = { text, font, textColor, bgColor in
1202-
completion(text, font, textColor, bgColor)
1200+
vc.endInput = { text, textColor, font, image, style in
1201+
completion(text, textColor, font, image, style)
12031202
}
12041203

12051204
vc.modalPresentationStyle = .fullScreen
@@ -1234,13 +1233,14 @@ open class ZLEditImageViewController: UIViewController {
12341233
}
12351234

12361235
/// Add text sticker
1237-
func addTextStickersView(_ text: String, textColor: UIColor, font: UIFont? = nil, bgColor: UIColor) {
1238-
guard !text.isEmpty else { return }
1236+
func addTextStickersView(_ text: String, textColor: UIColor, font: UIFont, image: UIImage?, style: ZLInputTextStyle) {
1237+
guard !text.isEmpty, let image = image else { return }
1238+
12391239
let scale = mainScrollView.zoomScale
1240-
let size = ZLTextStickerView.calculateSize(text: text, width: view.frame.width, font: font)
1240+
let size = ZLTextStickerView.calculateSize(image: image)
12411241
let originFrame = getStickerOriginFrame(size)
12421242

1243-
let textSticker = ZLTextStickerView(text: text, textColor: textColor, font: font, bgColor: bgColor, originScale: 1 / scale, originAngle: -angle, originFrame: originFrame)
1243+
let textSticker = ZLTextStickerView(text: text, textColor: textColor, font: font, style: style, image: image, originScale: 1 / scale, originAngle: -angle, originFrame: originFrame)
12441244
stickersContainer.addSubview(textSticker)
12451245
textSticker.frame = originFrame
12461246

@@ -1692,22 +1692,23 @@ extension ZLEditImageViewController: ZLStickerViewDelegate {
16921692
}
16931693

16941694
func sticker(_ textSticker: ZLTextStickerView, editText text: String) {
1695-
showInputTextVC(text, textColor: textSticker.textColor, font: textSticker.textFont, bgColor: textSticker.bgColor) { [weak self] text, font, textColor, bgColor in
1696-
guard let `self` = self else { return }
1697-
if text.isEmpty {
1695+
showInputTextVC(text, textColor: textSticker.textColor, font: textSticker.font, style: textSticker.style) { [weak self] text, textColor, font, image, style in
1696+
guard let image = image, !text.isEmpty else {
16981697
textSticker.moveToAshbin()
1699-
} else {
1700-
textSticker.startTimer()
1701-
guard textSticker.text != text || textSticker.textColor != textColor || textSticker.bgColor != bgColor else {
1702-
return
1703-
}
1704-
textSticker.text = text
1705-
textSticker.textColor = textColor
1706-
textSticker.bgColor = bgColor
1707-
textSticker.textFont = font
1708-
let newSize = ZLTextStickerView.calculateSize(text: text, width: self.view.frame.width, font: font)
1709-
textSticker.changeSize(to: newSize)
1698+
return
1699+
}
1700+
1701+
textSticker.startTimer()
1702+
guard textSticker.text != text || textSticker.textColor != textColor || textSticker.style != style || textSticker.font != font else {
1703+
return
17101704
}
1705+
textSticker.text = text
1706+
textSticker.textColor = textColor
1707+
textSticker.style = style
1708+
textSticker.image = image
1709+
textSticker.font = font
1710+
let newSize = ZLTextStickerView.calculateSize(image: image)
1711+
textSticker.changeSize(to: newSize)
17111712
}
17121713
}
17131714
}

0 commit comments

Comments
 (0)