将文字转为多彩图片

今天写了个将文字转化为多彩图片的功能,输入文字将文字转化为彩色的文字图片,可选择不同的字体,渐变,先看看效果。


屏幕快照 2016-01-12 上午9.42.23.png

实现主要用CAGradientLayer渐变,先看看上部展示实现代码:

-(void)setupContentView
{
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, ScreenWidth, ScreenHight - 44 -300)];
    [self.view addSubview:contentView];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    [contentView addGestureRecognizer:tapGesture];
    self.topContentView = contentView;
    self.topContentView.backgroundColor = [UIColor clearColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.text = @"ABC";
    label.frame = [self calculateContextLabelFrameWithTitle:@"ABC"];
    label.center = CGPointMake(contentView.bounds.size.width / 2, contentView.bounds.size.height / 2);
    label.font = [UIFont fontWithName:self.fontNameArray[0] size:25];
    label.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:label];
    label.backgroundColor = [UIColor clearColor];
    self.contentLabel = label;
    
    self.gradientLayer = [CAGradientLayer layer];
    self.gradientLayer.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
    self.gradientLayer.backgroundColor = [UIColor clearColor].CGColor;
    self.gradientLayer.startPoint = CGPointMake(0,0.5);
    self.gradientLayer.endPoint = CGPointMake(1,0.5);
    self.gradientLayer.colors = self.grandentArr[0];
    [contentView.layer addSublayer:self.gradientLayer];
    self.gradientLayer.mask = self.contentLabel.layer;
    self.contentLabel.frame = self.gradientLayer.bounds;
}

当输入的文字改变时,重新计算 self.gradientLayer的frame

-(void)textFieldTextChange:(NSNotification*)notice
{
    self.contentLabel.text = self.textField.text;
    [self reCalculateGradientLayerFrame];
}

下部分的实现代码:

-(void)setupBottomView
{
    UIView *bottomView =[[UIView alloc] initWithFrame:CGRectMake(0, ScreenHight - 300, ScreenWidth, 300)];
    bottomView.backgroundColor = JGCOLOR(222, 222, 222);
    [self.view addSubview:bottomView];
    self.bottomContentView = bottomView;
    
    UIView *textContentView = [[UIView alloc] init];
    textContentView.frame = CGRectMake(0, 0, bottomView.bounds.size.width, 50);
    [bottomView addSubview:textContentView];
    textContentView.backgroundColor = JGCOLOR(55, 44, 16);
    
    UITextField *textField = [[UITextField alloc] init];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.frame = CGRectMake(10, 5, textContentView.bounds.size.width - 20, textContentView.bounds.size.height - 10);
    [textContentView addSubview:textField];
    self.textField = textField;
    
    CGFloat orgY = 60;
    CGFloat orgx = 10;
    CGFloat space = 10;
    CGFloat width = (ScreenWidth - orgx * 2 - 3 * space) / 4;
    CGFloat height = 35;
    
    for (int i = 0; i < 16; i++) {
        UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(orgx + (i % 4) * width + (i % 4) * space, orgY + (i / 4) * height + (i / 4) * space, width, height)];
        vw.backgroundColor = [UIColor clearColor];
        vw.tag = i + 1;
        [self.bottomContentView addSubview:vw];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap:)];
        [vw addGestureRecognizer:tapGesture];
        
        UILabel *label = [[UILabel alloc] initWithFrame:vw.bounds
                          ];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"ABC";
        label.frame = vw.bounds;
        label.font = [UIFont fontWithName:self.fontNameArray[i % 4] size:25];
        label.backgroundColor = [UIColor clearColor];
        [vw addSubview:label];
        
        CAGradientLayer *grandient = [CAGradientLayer layer];
        grandient.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
        grandient.backgroundColor = [UIColor clearColor].CGColor;
        grandient.startPoint = CGPointMake(0,0.5);
        grandient.endPoint = CGPointMake(1,0.5);
        grandient.colors = self.grandentArr[i / 4];
        [vw.layer addSublayer:grandient];
        grandient.mask = label.layer;
        label.frame = grandient.bounds;
    }
}

将文字转化为图片的代码:

-(void)getTitleImg
{
    UIGraphicsBeginImageContext(self.topContentView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        [self.topContentView drawViewHierarchyInRect:self.topContentView.frame afterScreenUpdates:YES];
    }
    else
    {
        [self.topContentView.layer renderInContext:context];
    }
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRef newImgRef =  CGImageCreateWithImageInRect(img.CGImage, CGRectMake(self.gradientLayer.frame.origin.x, self.gradientLayer.frame.origin.y + 44, self.gradientLayer.frame.size.width, self.gradientLayer.frame.size.height));
    
    UIGraphicsBeginImageContextWithOptions(self.gradientLayer.frame.size, NO, [UIScreen mainScreen].scale);
    context = UIGraphicsGetCurrentContext();
    
    CGContextTranslateCTM(context, 0, self.gradientLayer.frame.size.height);
    CGContextScaleCTM(context, 1, -1);
    
    CGContextDrawImage(context, CGRectMake(0, 0, self.gradientLayer.frame.size.width, self.gradientLayer.frame.size.height), newImgRef);
    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library toolWriteImageToSavedPhotosAlbum:newImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
        if (error) {
            JGLog(@"写入出错");
        }
    } groupName:@"相册名称"];
    
}

核心代码如上,主要运用到了CAGradientLayer,截图,裁图的方法。上传代码github:https://github.com/jiangtaidi/titleImgCreateDemo.git

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,095评论 25 707
  • 《曾经沧海》第二章 患难与共 《曾经沧海》第三章 裂缝渐深 《曾经沧海》第四章 分道扬镳 《曾经沧海》大结局 《曾...
    轩辕子桑阅读 359评论 0 0
  • 我希望 能枕着你的呢喃入睡 翻身 你便在我手能触到的地方 ——《学飞集》
    李龙草阅读 239评论 0 0
  • 月升东岸鸟归窝,天高云淡泛清波。湖光山色影斜落,农家小院映旋涡。
    沈应齐阅读 326评论 0 0
  • 小时候我们每个人都被问过:“长大以后你想做什么?”, “我想成为一名老师,因为老师很伟大” “我想成为一名医生,我...
    小杰爱生活阅读 712评论 0 4