Skip to content

Commit

Permalink
Text sticker can customize text color.
Browse files Browse the repository at this point in the history
  • Loading branch information
longitachi committed Nov 18, 2020
1 parent dae8190 commit 3ca1cb5
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 45 deletions.
32 changes: 27 additions & 5 deletions Example/Example/PhotoConfigureCNViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class PhotoConfigureCNViewController: UIViewController {

var editImageClipToolSwitch: UISwitch!

var editImageTextStickerToolSwitch: UISwitch!

var editImageMosaicToolSwitch: UISwitch!

var editImageFilterToolSwitch: UISwitch!
Expand Down Expand Up @@ -513,11 +515,28 @@ class PhotoConfigureCNViewController: UIViewController {
make.centerY.equalTo(clipToolLabel)
}

// 裁剪
// 文本
let textStickerToolLabel = createLabel("文字")
self.editImageToolView.addSubview(textStickerToolLabel)
textStickerToolLabel.snp.makeConstraints { (make) in
make.top.equalTo(clipToolLabel.snp.bottom).offset(velSpacing)
make.left.equalTo(self.editImageToolView)
}

self.editImageTextStickerToolSwitch = UISwitch()
self.editImageTextStickerToolSwitch.isOn = config.editImageTools.contains(.textSticker)
self.editImageTextStickerToolSwitch.addTarget(self, action: #selector(textStickerToolChanged), for: .valueChanged)
self.editImageToolView.addSubview(self.editImageTextStickerToolSwitch)
self.editImageTextStickerToolSwitch.snp.makeConstraints { (make) in
make.left.equalTo(textStickerToolLabel.snp.right).offset(horSpacing)
make.centerY.equalTo(textStickerToolLabel)
}

// 马赛克
let mosaicToolLabel = createLabel("马赛克")
self.editImageToolView.addSubview(mosaicToolLabel)
mosaicToolLabel.snp.makeConstraints { (make) in
make.top.equalTo(clipToolLabel.snp.bottom).offset(velSpacing)
make.top.equalTo(textStickerToolLabel.snp.bottom).offset(velSpacing)
make.left.equalTo(self.editImageToolView)
}

Expand Down Expand Up @@ -550,11 +569,10 @@ class PhotoConfigureCNViewController: UIViewController {

// 编辑视频开关
editVideoLabel = createLabel("允许编辑视频")
editVideoLabel.tag = 1000
containerView.addSubview(editVideoLabel)
editVideoLabel.snp.makeConstraints { (make) in
if config.allowEditImage {
make.top.equalTo(editImageLabel.snp.bottom).offset(180)
make.top.equalTo(editImageLabel.snp.bottom).offset(225)
} else {
make.top.equalTo(editImageLabel.snp.bottom).offset(velSpacing)
}
Expand Down Expand Up @@ -843,7 +861,7 @@ class PhotoConfigureCNViewController: UIViewController {
self.editImageToolView.alpha = self.config.allowEditImage ? 1 : 0
self.editVideoLabel.snp.updateConstraints({ (make) in
if self.config.allowEditImage {
make.top.equalTo(self.editImageLabel.snp.bottom).offset(180)
make.top.equalTo(self.editImageLabel.snp.bottom).offset(225)
} else {
make.top.equalTo(self.editImageLabel.snp.bottom).offset(20)
}
Expand All @@ -860,6 +878,10 @@ class PhotoConfigureCNViewController: UIViewController {
config.editImageTools = ZLEditImageViewController.EditImageTool(rawValue: config.editImageTools.rawValue ^ ZLEditImageViewController.EditImageTool.clip.rawValue)
}

@objc func textStickerToolChanged() {
config.editImageTools = ZLEditImageViewController.EditImageTool(rawValue: config.editImageTools.rawValue ^ ZLEditImageViewController.EditImageTool.textSticker.rawValue)
}

@objc func mosaicToolChanged() {
config.editImageTools = ZLEditImageViewController.EditImageTool(rawValue: config.editImageTools.rawValue ^ ZLEditImageViewController.EditImageTool.mosaic.rawValue)
}
Expand Down
32 changes: 27 additions & 5 deletions Example/Example/PhotoConfigureViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class PhotoConfigureViewController: UIViewController {

var editImageClipToolSwitch: UISwitch!

var editImageTextStickerToolSwitch: UISwitch!

var editImageMosaicToolSwitch: UISwitch!

var editImageFilterToolSwitch: UISwitch!
Expand Down Expand Up @@ -513,11 +515,28 @@ class PhotoConfigureViewController: UIViewController {
make.centerY.equalTo(clipToolLabel)
}

// 裁剪
// 文本
let textStickerToolLabel = createLabel("Text sticker")
self.editImageToolView.addSubview(textStickerToolLabel)
textStickerToolLabel.snp.makeConstraints { (make) in
make.top.equalTo(clipToolLabel.snp.bottom).offset(velSpacing)
make.left.equalTo(self.editImageToolView)
}

self.editImageTextStickerToolSwitch = UISwitch()
self.editImageTextStickerToolSwitch.isOn = config.editImageTools.contains(.textSticker)
self.editImageTextStickerToolSwitch.addTarget(self, action: #selector(textStickerToolChanged), for: .valueChanged)
self.editImageToolView.addSubview(self.editImageTextStickerToolSwitch)
self.editImageTextStickerToolSwitch.snp.makeConstraints { (make) in
make.left.equalTo(textStickerToolLabel.snp.right).offset(horSpacing)
make.centerY.equalTo(textStickerToolLabel)
}

// 马赛克
let mosaicToolLabel = createLabel("Mosaic")
self.editImageToolView.addSubview(mosaicToolLabel)
mosaicToolLabel.snp.makeConstraints { (make) in
make.top.equalTo(clipToolLabel.snp.bottom).offset(velSpacing)
make.top.equalTo(textStickerToolLabel.snp.bottom).offset(velSpacing)
make.left.equalTo(self.editImageToolView)
}

Expand Down Expand Up @@ -550,11 +569,10 @@ class PhotoConfigureViewController: UIViewController {

// 编辑视频开关
editVideoLabel = createLabel("Edit video")
editVideoLabel.tag = 1000
containerView.addSubview(editVideoLabel)
editVideoLabel.snp.makeConstraints { (make) in
if config.allowEditImage {
make.top.equalTo(editImageLabel.snp.bottom).offset(180)
make.top.equalTo(editImageLabel.snp.bottom).offset(225)
} else {
make.top.equalTo(editImageLabel.snp.bottom).offset(velSpacing)
}
Expand Down Expand Up @@ -843,7 +861,7 @@ class PhotoConfigureViewController: UIViewController {
self.editImageToolView.alpha = self.config.allowEditImage ? 1 : 0
self.editVideoLabel.snp.updateConstraints({ (make) in
if self.config.allowEditImage {
make.top.equalTo(self.editImageLabel.snp.bottom).offset(180)
make.top.equalTo(self.editImageLabel.snp.bottom).offset(225)
} else {
make.top.equalTo(self.editImageLabel.snp.bottom).offset(20)
}
Expand All @@ -860,6 +878,10 @@ class PhotoConfigureViewController: UIViewController {
config.editImageTools = ZLEditImageViewController.EditImageTool(rawValue: config.editImageTools.rawValue ^ ZLEditImageViewController.EditImageTool.clip.rawValue)
}

@objc func textStickerToolChanged() {
config.editImageTools = ZLEditImageViewController.EditImageTool(rawValue: config.editImageTools.rawValue ^ ZLEditImageViewController.EditImageTool.textSticker.rawValue)
}

@objc func mosaicToolChanged() {
config.editImageTools = ZLEditImageViewController.EditImageTool(rawValue: config.editImageTools.rawValue ^ ZLEditImageViewController.EditImageTool.mosaic.rawValue)
}
Expand Down
55 changes: 40 additions & 15 deletions Sources/Edit/ZLEditImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,16 @@ public class ZLEditImageViewController: UIViewController {
self.view.addSubview(self.ashbinView)

self.ashbinImgView = UIImageView(image: getImage("zl_ashbin"), highlightedImage: getImage("zl_ashbin_open"))
self.ashbinImgView.frame = CGRect(x: (ashbinSize.width-25)/2, y: 20, width: 25, height: 25)
self.ashbinImgView.frame = CGRect(x: (ashbinSize.width-25)/2, y: 15, width: 25, height: 25)
self.ashbinView.addSubview(self.ashbinImgView)

let asbinTipLabel = UILabel(frame: CGRect(x: 0, y: ashbinSize.height-25, width: ashbinSize.width, height: 20))
let asbinTipLabel = UILabel(frame: CGRect(x: 0, y: ashbinSize.height-34, width: ashbinSize.width, height: 34))
asbinTipLabel.font = getFont(12)
asbinTipLabel.textAlignment = .center
asbinTipLabel.textColor = .white
asbinTipLabel.text = "拖动到此处删除"
asbinTipLabel.text = localLanguageTextValue(.textStickerRemoveTips)
asbinTipLabel.numberOfLines = 2
asbinTipLabel.lineBreakMode = .byCharWrapping
self.ashbinView.addSubview(asbinTipLabel)

if self.tools.contains(.mosaic) {
Expand Down Expand Up @@ -596,8 +598,8 @@ public class ZLEditImageViewController: UIViewController {
}

func textStickerBtnClick() {
self.showInputTextVC(nil) { [weak self] (text) in
self?.addTextStickersView(text)
self.showInputTextVC { [weak self] (text, textColor, bgColor) in
self?.addTextStickersView(text, textColor: textColor, bgColor: bgColor)
}
}

Expand Down Expand Up @@ -745,7 +747,7 @@ public class ZLEditImageViewController: UIViewController {
}
}

func showInputTextVC(_ text: String?, completion: @escaping ( (String) -> Void )) {
func showInputTextVC(_ text: String? = nil, textColor: UIColor? = nil, bgColor: UIColor? = nil, completion: @escaping ( (String, UIColor, UIColor) -> Void )) {
// Calculate image displayed frame on the screen.
var r = self.scrollView.convert(self.view.frame, to: self.containerView)
r.origin.x += self.scrollView.contentOffset.x / self.scrollView.zoomScale
Expand All @@ -756,18 +758,18 @@ public class ZLEditImageViewController: UIViewController {
r.size.width *= scale
r.size.height *= scale
let bgImage = self.buildImage().clipImage(self.angle, self.editRect)?.clipImage(0, r)
let vc = ZLInputTextViewController(image: bgImage, text: text)
let vc = ZLInputTextViewController(image: bgImage, text: text, textColor: textColor, bgColor: bgColor)

vc.endInput = { (text) in
completion(text)
vc.endInput = { (text, textColor, bgColor) in
completion(text, textColor, bgColor)
}

vc.modalPresentationStyle = .fullScreen
self.showDetailViewController(vc, sender: nil)
}

/// Text sticker
func addTextStickersView(_ text: String) {
func addTextStickersView(_ text: String, textColor: UIColor, bgColor: UIColor) {
guard !text.isEmpty else { return }
let scale = self.scrollView.zoomScale
let size = ZLTextStickerView.calculateSize(text: text, width: self.view.frame.width)
Expand All @@ -781,7 +783,7 @@ public class ZLEditImageViewController: UIViewController {
let r = self.containerView.convert(CGRect(x: x, y: y, width: w, height: h), to: self.textStickersContainer)
let originFrame = CGRect(x: r.minX + (r.width - size.width) / 2, y: r.minY + (r.height - size.height) / 2, width: size.width, height: size.height)

let textSticker = ZLTextStickerView(text: text, textColor: .white, bgColor: .clear, zoomScale: 1 / scale, originAngle: -self.angle, originFrame: originFrame)
let textSticker = ZLTextStickerView(text: text, textColor: textColor, bgColor: bgColor, zoomScale: 1 / scale, originAngle: -self.angle, originFrame: originFrame)
self.textStickersContainer.addSubview(textSticker)
textSticker.frame = originFrame

Expand Down Expand Up @@ -896,8 +898,7 @@ public class ZLEditImageViewController: UIViewController {
if !self.textStickersContainer.subviews.isEmpty, let context = UIGraphicsGetCurrentContext() {
let scale = self.imageSize.width / self.textStickersContainer.frame.width
self.textStickersContainer.subviews.forEach { (sv) in
(sv as? ZLTextStickerView)?.hideBorder()
(sv as? ZLTextStickerView)?.cleanTimer()
(sv as? ZLTextStickerView)?.resetState()
}
context.concatenate(CGAffineTransform(scaleX: scale, y: scale))
self.textStickersContainer.layer.render(in: context)
Expand Down Expand Up @@ -1064,7 +1065,6 @@ extension ZLEditImageViewController: UICollectionViewDataSource, UICollectionVie
default:
break
}
collectionView.reloadData()
} else if collectionView == self.drawColorCollectionView {
self.currentDrawColor = self.drawColors[indexPath.row]
} else {
Expand Down Expand Up @@ -1113,6 +1113,13 @@ extension ZLEditImageViewController: ZLTextStickerViewDelegate {
UIView.animate(withDuration: 0.25) {
self.ashbinView.alpha = 1
}

self.textStickersContainer.subviews.forEach { (view) in
if let ts = view as? ZLTextStickerView, ts !== textSticker {
ts.resetState()
ts.gesIsEnabled = false
}
}
}

func textStickerOnOperation(_ textSticker: ZLTextStickerView, panGes: UIPanGestureRecognizer) {
Expand All @@ -1135,15 +1142,33 @@ extension ZLEditImageViewController: ZLTextStickerViewDelegate {
if self.ashbinView.frame.contains(point) {
textSticker.moveToAshbin()
}

self.textStickersContainer.subviews.forEach { (view) in
(view as? ZLTextStickerView)?.gesIsEnabled = true
}
}

func textStickerDidTap(_ textSticker: ZLTextStickerView) {
self.textStickersContainer.subviews.forEach { (view) in
if let ts = view as? ZLTextStickerView, ts !== textSticker {
ts.resetState()
}
}
}

func textSticker(_ textSticker: ZLTextStickerView, editText text: String) {
self.showInputTextVC(text) { [weak self] (text) in
self.showInputTextVC(text, textColor: textSticker.textColor, bgColor: textSticker.bgColor) { [weak self] (text, textColor, bgColor) in
guard let `self` = self else { return }
if text.isEmpty {
textSticker.moveToAshbin()
} else {
textSticker.startTimer()
guard textSticker.text != text else {
return
}
textSticker.text = text
textSticker.textColor = textColor
textSticker.bgColor = bgColor
let newSize = ZLTextStickerView.calculateSize(text: text, width: self.view.frame.width)
textSticker.changeSize(to: newSize)
}
Expand Down
Loading

0 comments on commit 3ca1cb5

Please sign in to comment.