iOS實(shí)現(xiàn)文字轉(zhuǎn)化成彩色文字圖片
本文寫了個(gè)將文字轉(zhuǎn)化為多彩圖片的功能,輸入文字將文字轉(zhuǎn)化為彩色的文字圖片,可選擇不同的字體,漸變,先看看效果。

實(shí)現(xiàn)主要用CAGradientLayer漸變,先看看上部展示實(shí)現(xiàn)代碼:
-(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;
}
當(dāng)輸入的文字改變時(shí),重新計(jì)算 self.gradientLayer的frame
-(void)textFieldTextChange:(NSNotification*)notice
{
self.contentLabel.text = self.textField.text;
[self reCalculateGradientLayerFrame];
}
下部分的實(shí)現(xiàn)代碼:
-(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;
}
}
將文字轉(zhuǎn)化為圖片的代碼:
-(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(@"寫入出錯(cuò)");
}
} groupName:@"相冊名稱"];
}
核心代碼如上,主要運(yùn)用到了CAGradientLayer,截圖,裁圖的方法。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
iOS設(shè)計(jì)模式——Category簡單介紹
這篇文章主要介紹了iOS設(shè)計(jì)模式——Category簡單介紹,有興趣學(xué)習(xí)的同學(xué)可以了解一下。2016-11-11
簡單好用的iOS導(dǎo)航欄封裝.runtime屬性控制實(shí)例代碼
這篇文章主要給大家介紹了簡單好用的iOS導(dǎo)航欄封裝.runtime屬性控制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
iOS WKWebView中MessageHandler內(nèi)存泄漏問題的完美解決過程
這篇文章主要給大家介紹了關(guān)于iOS WKWebView中MessageHandler內(nèi)存泄漏問題的完美解決過程,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
iOS開發(fā)中UIImageView控件的常用操作整理
這篇文章主要介紹了iOS開發(fā)中UIImageView控件的常用操作整理,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
詳解IOS 利用storyboard修改UITextField的placeholder文字顏色
這篇文章主要介紹了詳解IOS 利用storyboard修改UITextField的placeholder文字顏色的相關(guān)資料,希望通過本文能實(shí)現(xiàn)這樣類似的功能,需要的朋友可以參考下2017-08-08
OC - 9.基于Quartz2D繪制下載進(jìn)度條(demo)
這篇文章主要介紹了OC - 9.基于Quartz2D繪制下載進(jìn)度條(demo)的相關(guān)資料,需要的朋友可以參考下2015-11-11

