iOS 仿钉钉的文字水印

1,933 阅读1分钟

项目中需要用给重要的页面添加当前登陆人的名字水印,于是就自己简单造了个轮子。核心代码比简单如下:

for i in 0...line {

            for j in 0...row
            {
            //初始化textLayer,textLayer直接添加在view上 
                let textLayer = CATextLayer.init()
                textLayer.contentsScale = UIScreen.main.scale
                textLayer.font = font
                textLayer.fontSize = font.pointSize
                textLayer.foregroundColor = color.cgColor
                textLayer.string = waterText
                let hSpace : Int = j*lineSpace
                let vSpace : Int = i * rowHeight
                
                textLayer.frame = CGRect(x: hSpace, y: vSpace, width: textWidth , height: textHeight)
             //旋转文字,角度可以根据自己需要调整
                textLayer.transform = CATransform3DMakeRotation(CGFloat(-Double.pi / 16), 0, 0, 3)
                self.layer.addSublayer(textLayer)
            }
        }

效果如图

demo地址 oc版本:github.com/kongmingyan… swift版本: github.com/kongmingyan…