原文链接(iOS—添加文字水印)
最近有个写水印需求,搜到了上面的文章,修复了一些小问题,改成swift
实现,如下:
import Foundation
import UIKit
fileprivate let HORIZONTAL_SPACEING: CGFloat = 30 //水平间距
fileprivate let VERTICAL_SPACEING: CGFloat = 50 //竖直间距
fileprivate let CG_TRANSFORM_ROTATING = CGFloat(-Double.pi / 6) //旋转角度(正旋45度 || 反旋45度)
class WaterMarkView: UIView {
public private(set) lazy var waterMarkImgV: UIImageView = {
let imgV = UIImageView(frame: self.bounds)
imgV.image = UIImage(named: "")
return imgV
}()
private static let shared = WaterMarkView(frame: UIScreen.main.bounds)
/// 水印文字
public private(set) var markText: NSString = "默认水印文字"
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(waterMarkImgV)
waterMarkImgV.frame = frame
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// 显示水印
public static func show(view: UIView, text: String = "默认水印文字") {
let markText = text as NSString
let image = waterImage(size: view.bounds.size, text: markText)
let shared = WaterMarkView.shared
shared.markText = markText
shared.waterMarkImgV.image = image
view.addSubview(shared)
}
/// 隐藏水印
public static func hide() {
let shared = WaterMarkView.shared
shared.removeFromSuperview()
}
/// 水印图片
private static func waterImage(size: CGSize, text: NSString, font: UIFont = UIFont.systemFont(ofSize: 17), textColor: UIColor = .black) -> UIImage? {
let imgW: CGFloat = size.width
let imgH: CGFloat = size.height
// 1.开启上下文
UIGraphicsBeginImageContextWithOptions(size, false, 0)
// 2.绘制图片
//属性字符串
let attr: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: textColor]
let attrStr = NSMutableAttributedString(string: text as String, attributes: attr)
//文字宽高
let strW: CGFloat = attrStr.size().width
let strH: CGFloat = attrStr.size().height
//获取当前上下文
guard let context = UIGraphicsGetCurrentContext() else { return nil }
//调整矩阵锚点到中心
context.concatenate(CGAffineTransform(translationX: imgW * 0.5, y: imgH * 0.5))
//旋转矩阵
context.concatenate(CGAffineTransform(rotationAngle: CG_TRANSFORM_ROTATING))
//恢复锚点位置到左上角
context.concatenate(CGAffineTransform(translationX: -imgW * 0.5, y: -imgH * 0.5))
//对角线长度(实际绘制范围是对角线长度的正方形)
let sqrtLength: CGFloat = sqrt(pow(imgW, 2) + pow(imgH, 2))
//绘制的行数和列数
let countHor = Int(sqrtLength / (strW + HORIZONTAL_SPACEING)) + 1
let countVer = Int(sqrtLength / (strH + VERTICAL_SPACEING)) + 1
//画布起点
let orginX: CGFloat = -(sqrtLength - imgW) * 0.5
let orginY: CGFloat = -(sqrtLength - imgH) * 0.5
//双循环实现
var i = 0
var j = 0
while i < countVer {
j = 0
let overlayOriginY = orginY + CGFloat(i) * (strH + VERTICAL_SPACEING)
while j < countHor {
let overlayOrginX = orginX + CGFloat(j) * (strW + HORIZONTAL_SPACEING)
//绘制单个文字水印
let drawRect = CGRect(x: overlayOrginX, y: overlayOriginY, width: strW, height: strH)
text.draw(in: drawRect, withAttributes: attr)
j += 1
}
i += 1
}
//3.上下文获取新图片
let newImage = UIGraphicsGetImageFromCurrentImageContext()
//4.关闭上下文
UIGraphicsEndImageContext()
context.restoreGState()
return newImage
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return nil
}
}
网友评论