iOS无侵入水印参考

不知道果猿注意到没有,国家和企业越来越注意信息安全了,这不,最近几个项目都接到死命令,给项目打上水印。于是乎。。。。。。

简单思考一下,你要的水印功能无非就酱紫>>>
-自定义水印文字;
-自个决定水印文字显示在某个地方;
-自个决定水印文字显示多少行;
-自定义水印文字大小;
-自定义水印文字颜色及透明度;
-自定义水印文字旋转角度;
-自定义水印文字水平间距;
-自定义水印文字垂直间距;

想想是不是就头皮发痒,没事,跟产品经理干一架,赢了,你明天不用来上班了,输了,你明天还是不用来上班了。但是你的目的达到了,不用写这个水印功能了,是不是想想都有点小激动呢。

言归正传。。。我们先创建一个WQWaterMark继承自UIImageView

@interface BUIWaterMark : UIImageView
@end

首先,我们想哦,水印有必要每个界面都创建一个吗?没必要吧,所以,是不是得先搞个单例。

+ (instancetype)shareInstance {
    static WQWaterMark *waterMark = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        waterMark = [[WQWaterMark alloc] init];
    });
    return waterMark;
}

对于水印这个小功能,是不是使用越简单越好,对于调用方来说,一句代码就能搞掂是不是更爽。所以,先暴露几个方法先

/// 移除水印,根据项目需要调用
+ (void)removeWaterMark;


/// 添加水印,添加到主窗口
/// @param text 水印文字 多行用\n分隔
+ (void)waterMarkWithText:(NSString *)text;


/// 添加水印
/// @param superView 需要显示水印的视图
/// @param text 水印文字 多行用\n分隔
+ (void)waterMarkAt:(UIView *)superView text:(NSString *)text;


/// 添加水印
/// @param superView 需要显示水印的视图
/// @param text 水印文字 多行用\n分隔
/// @param rotationAngle 水印旋转角度 默认-30。 例如:30即顺时针旋转30度,-30即逆时针旋转30度
+ (void)waterMarkAt:(UIView *)superView text:(NSString *)text rotationAngle:(CGFloat)rotationAngle;


/// 添加水印
/// @param superView 需要显示水印的视图
/// @param text 水印文字 多行用\n分隔
/// @param rotationAngle 水印旋转角度 默认-30。 例如:30即顺时针旋转30度,-30即逆时针旋转30度
/// @param waterMarkFont 水印的字体 默认[UIFont systemFontOfSize:14]
/// @param waterMarkColor 水印颜色 [UIColor colorWithRed:152/255.0f green:152/255.0f blue:152/255.0f alpha:0.2]
+ (void)waterMarkAt:(UIView *)superView
               text:(NSString *)text
      rotationAngle:(CGFloat)rotationAngle
      waterMarkFont:(UIFont *)waterMarkFont
     waterMarkColor:(UIColor *)waterMarkColor;


/// 添加水印
/// @param superView 需要显示水印的视图
/// @param text 水印文字 多行用\n分隔
/// @param rotationAngle 水印旋转角度 默认-30。 例如:30即顺时针旋转30度,-30即逆时针旋转30度
/// @param horizontalSpace 水印文字的横向距离 默认40.f
/// @param veticalSpace 水印文字的纵向距离 默认40.f
+ (void)waterMarkAt:(UIView *)superView
               text:(NSString *)text
      rotationAngle:(CGFloat)rotationAngle
    horizontalSpace:(CGFloat)horizontalSpace
       veticalSpace:(CGFloat)veticalSpace;


/// 添加水印
/// @param superView 需要显示水印的视图
/// @param text 水印文字 多行用\n分隔
/// @param rotationAngle 水印旋转角度 默认-30。 例如:30即顺时针旋转30度,-30即逆时针旋转30度
/// @param waterMarkFont 水印的字体 默认[UIFont systemFontOfSize:14]
/// @param waterMarkColor 水印颜色 [UIColor colorWithRed:152/255.0f green:152/255.0f blue:152/255.0f alpha:0.2]
/// @param horizontalSpace 水印文字的横向距离 默认40.f
/// @param veticalSpace 水印文字的纵向距离 默认40.f
+ (void)waterMarkAt:(UIView *)superView
               text:(NSString *)text
      rotationAngle:(CGFloat)rotationAngle
      waterMarkFont:(UIFont *)waterMarkFont
     waterMarkColor:(UIColor *)waterMarkColor
    horizontalSpace:(CGFloat)horizontalSpace
       veticalSpace:(CGFloat)veticalSpace;

以上方法视你项目情况,你爱咋用就咋用。

具体实现也不啰嗦了,看代码
首先先定义几个默认值 宏

#define kWQWaterMarkDefautFont [UIFont systemFontOfSize:14]
#define kWQWaterMarkDefaultColor [UIColor colorWithRed:152/255.0f green:152/255.0f blue:152/255.0f alpha:0.2]
#define kWQWaterMarkDefaultHorizontalSpace 40.f
#define kWQWaterMarkDefaultVericalSpace 40.f
#define kWQWaterMarkDefaultRotationAngle 45.f

下面就是具体的实现代码了

+ (void)removeWaterMark {
    [[BUIWaterMark shareInstance] removeFromSuperview];
}

+ (void)waterMarkWithText:(NSString *)text {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [BUIWaterMark waterMarkAt:keyWindow text:text rotationAngle:kWQWaterMarkDefaultRotationAngle waterMarkFont:kWQWaterMarkDefautFont waterMarkColor:kWQWaterMarkDefaultColor horizontalSpace:kWQWaterMarkDefaultHorizontalSpace veticalSpace:kWQWaterMarkDefaultVericalSpace];
}


+ (void)waterMarkAt:(UIView *)superView text:(NSString *)text {
    
    [BUIWaterMark waterMarkAt:superView text:text rotationAngle:kWQWaterMarkDefaultRotationAngle waterMarkFont:kWQWaterMarkDefautFont waterMarkColor:kWQWaterMarkDefaultColor horizontalSpace:kWQWaterMarkDefaultHorizontalSpace veticalSpace:kWQWaterMarkDefaultVericalSpace];
}


+ (void)waterMarkAt:(UIView *)superView text:(NSString *)text rotationAngle:(CGFloat)rotationAngle {
    
    [BUIWaterMark waterMarkAt:superView text:text rotationAngle:rotationAngle waterMarkFont:kWQWaterMarkDefautFont waterMarkColor:kWQWaterMarkDefaultColor horizontalSpace:kWQWaterMarkDefaultHorizontalSpace veticalSpace:kWQWaterMarkDefaultVericalSpace];
}


+ (void)waterMarkAt:(UIView *)superView
               text:(NSString *)text
      rotationAngle:(CGFloat)rotationAngle
      waterMarkFont:(UIFont *)waterMarkFont
     waterMarkColor:(UIColor *)waterMarkColor {
    
    [BUIWaterMark waterMarkAt:superView text:text rotationAngle:rotationAngle waterMarkFont:waterMarkFont waterMarkColor:waterMarkColor horizontalSpace:kWQWaterMarkDefaultHorizontalSpace veticalSpace:kWQWaterMarkDefaultVericalSpace];
}


+ (void)waterMarkAt:(UIView *)superView
               text:(NSString *)text
      rotationAngle:(CGFloat)rotationAngle
    horizontalSpace:(CGFloat)horizontalSpace
       veticalSpace:(CGFloat)veticalSpace {
    
    [BUIWaterMark waterMarkAt:superView text:text rotationAngle:rotationAngle waterMarkFont:kWQWaterMarkDefautFont waterMarkColor:kWQWaterMarkDefaultColor horizontalSpace:horizontalSpace veticalSpace:veticalSpace];
}


+ (void)waterMarkAt:(UIView *)superView
               text:(NSString *)text
      rotationAngle:(CGFloat)rotationAngle
      waterMarkFont:(UIFont *)waterMarkFont
     waterMarkColor:(UIColor *)waterMarkColor
    horizontalSpace:(CGFloat)horizontalSpace
       veticalSpace:(CGFloat)veticalSpace {
    
    
    BUIWaterMark *waterMark = [BUIWaterMark shareInstance];

    waterMark.frame = superView.bounds;
    
    //绘制水印的宽高
    CGFloat viewWidth = superView.frame.size.width;
    CGFloat viewHeight = superView.frame.size.height;
    
    //为了防止图片失真,绘制区域宽高和原始图片宽高一样
    UIGraphicsBeginImageContext(CGSizeMake(viewWidth, viewHeight));
    
    //sqrtLength:原始image的对角线length。在水印旋转矩阵中只要矩阵的宽高是原始image的对角线长度,无论旋转多少度都不会有空白。
    CGFloat sqrtLength = sqrt(viewWidth*viewWidth + viewHeight*viewHeight);
    
    NSDictionary *attr = @{
        NSFontAttributeName: waterMarkFont,
        NSForegroundColorAttributeName :waterMarkColor,
    };
    NSMutableAttributedString *attrStr = [[NSAttributedString alloc] initWithString:text attributes:attr];
    //绘制文字的宽高
    CGFloat strWidth = attrStr.size.width;
    CGFloat strHeight = attrStr.size.height;
    
    //开始旋转上下文矩阵,绘制水印文字
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //将绘制原点(0,0)调整到原image的中心
    CGContextConcatCTM(context, CGAffineTransformMakeTranslation(viewWidth/2.f, viewHeight/2.f));
    //以绘制原点为中心旋转
    if (rotationAngle!=0) {
        CGContextConcatCTM(context, CGAffineTransformMakeRotation(-M_PI/180.0*rotationAngle));
    }
    //将绘制原点恢复初始值,保证当前context中心和源image的中心处在一个点(当前context已经旋转,所以绘制出的任何layer都是倾斜的)
    CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-viewWidth/2.f, -viewHeight/2.f));
    
    //计算需要绘制的列数和行数
    int horCount = sqrtLength / (strWidth + horizontalSpace)+1;
    int verCount = sqrtLength / (strHeight + veticalSpace)+1;
    
    //此处计算出需要绘制水印文字的起始点,由于水印区域要大于图片区域所以起点在原有基础上移
    CGFloat orignX = -(sqrtLength-viewWidth)/2.f;
    CGFloat orignY = -(sqrtLength-viewHeight)/2.f;
    
    //在每列绘制时X坐标叠加
    CGFloat tempOrignX = orignX;
    //在每行绘制时Y坐标叠加
    CGFloat tempOrignY = orignY;
    for (int i = 0; i < horCount * verCount; i++) {
        [attrStr drawInRect:CGRectMake(tempOrignX, tempOrignY, strWidth, strHeight)];
        if (i % horCount == 0 && i != 0) {
            tempOrignX = orignX;
            tempOrignY += (strHeight + veticalSpace);
        }else{
            tempOrignX += (strWidth + horizontalSpace);
        }
    }
    //根据上下文制作成图片
    UIImage *finalImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGContextRestoreGState(context);
    
    waterMark.image = finalImg;
    
    [superView addSubview:waterMark];
}


-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    
    //1.判断自己能否接收事件
    if(self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {
        return nil;
    }
    //2.判断当前点在不在当前View.
    if (![self pointInside:point withEvent:event]) {
        return nil;
    }
    //3.从后往前遍历自己的子控件.让子控件重复前两步操作,(把事件传递给,让子控件调用hitTest)
    int count = (int)self.subviews.count;
    for (int i = count - 1; i >= 0; i--) {
        //取出每一个子控件
        UIView *chileV =  self.subviews[i];
        //把当前的点转换成子控件坐标系上的点.
        CGPoint childP = [self convertPoint:point toView:chileV];
        UIView *fitView = [chileV hitTest:childP withEvent:event];
        //判断有没有找到最适合的View
        if(fitView){
            return fitView;
        }
    }
    
    //4.没有找到比它自己更适合的View.那么它自己就是最适合的View
    return self;
}

//判断当前点在不在它调用View
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return NO;
}

好了,以上就是我水印的整个实现思路,代码直接粘贴复制就能够使用,不依赖任何环境。不对现有项目有任何影响。如果整个项目要用,可以直接在AppDelegate或者在基类调用以下一句代码

[BUIWaterMark waterMarkWithText:@"水印文字\n支持多行"];

如果不是整个项目用,那么只需传入显示水印的视图即可,即

[BUIWaterMark waterMarkAt:需要显示水印的视图 text:@"水印文字\n支持多行"]
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354