iOS实现文字转化成彩色文字图片

 更新时间:2020年07月23日 11:43:04   作者:jiangamh  
这篇文章主要为大家详细介绍了iOS文字转化成彩色文字图片的实现方法,可以实现不同字体,渐变的效果,感兴趣的小伙伴们可以参考一下

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

实现主要用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,截图,裁图的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关文章

  • IOS 代理方式实现实例详解

    IOS 代理方式实现实例详解

    这篇文章主要介绍了IOS 代理方式实现实例详解的相关资料,需要的朋友可以参考下
    2016-11-11
  • iOS实现应用内切换语言及字体大小(模仿微信)

    iOS实现应用内切换语言及字体大小(模仿微信)

    这篇文章主要给大家介绍了关于利用iOS如何实现应用内切换语言及字体大小的相关资料,实现的效果类似我们经常在微信中见到的,文中通过示例代码介绍的非常详细,需要的朋友们可以参考借鉴,下面随着小编来一起学习学习吧。
    2018-01-01
  • 详解IOS中如何实现瀑布流效果

    详解IOS中如何实现瀑布流效果

    说到瀑布流, 或许大家都不陌生, 瀑布流的实现也有很多种! 从scrollView 到 tableView 书写的瀑布流, 然后再到2012年iOS6 苹果API新加进的collectionView进行的瀑布流封装! 确实,不论是写起来还是用起来都要方便很多!那么下面一起来看看IOS中具体如何实现瀑布流效果。
    2016-08-08
  • iOS 图片旋转方法实例代码

    iOS 图片旋转方法实例代码

    这篇文章主要介绍了iOS 图片旋转方法实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-03-03
  • iOS开发之详谈属性设置readwrite、readonly、retain、copy、assign、nonatomic

    iOS开发之详谈属性设置readwrite、readonly、retain、copy、assign、nonatomic

    这篇文章主要介绍了iOS开发之详谈属性设置readwrite、readonly、retain、copy、assign、nonatomic的相关资料,需要的朋友可以参考下
    2015-10-10
  • iOS监控笔记之启动crash

    iOS监控笔记之启动crash

    iOS崩溃是让iOS开发人员比较头痛的事情,app崩溃了,说明代码写的有问题,下面这篇文章主要给大家介绍了关于iOS监控笔记之启动crash的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-05-05
  • iOS开发之MRC(手动内存管理)详解

    iOS开发之MRC(手动内存管理)详解

    这篇文章主要介绍了 iOS开发之MRC(手动内存管理)详解的相关资料,需要的朋友可以参考下
    2022-08-08
  • iOS实现双向滑动条效果

    iOS实现双向滑动条效果

    这篇文章主要为大家详细介绍了iOS实现双向滑动条效果的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • iOS实现UITableView数据为空时的提示页面

    iOS实现UITableView数据为空时的提示页面

    最近工作中遇到一个需求,当UITableView数据为空的时候,给出一个简单的提示页面,通过从网上查找解决的方法,发现了两种实现的方法,现在分享给大家,有需要的朋友们可以参考借鉴,下面感兴趣的朋友们来一起学习学习吧。
    2016-11-11
  • iOS中UIImagePickerController图片选取器的用法小结

    iOS中UIImagePickerController图片选取器的用法小结

    UIImagePickerController平时就是用来做应用中从相册中选取图片功能的,这里我们就来整理一下iOS中UIImagePickerController图片选取器的用法小结,需要的朋友可以参考下
    2016-05-05

最新评论