您的位置:首页 > 移动开发 > IOS开发

iOS开发 在图片上添加文字,图片合成文字,图片上添加富文本,美图秀秀,美颜相机文字编辑

2017-04-21 17:32 2803 查看


一:前言

 1.0  :在图片上增加文字,是图片编辑中非常常见的需求。

 1.1 :无demo说话不硬气,上github地址:https://github.com/horisea/PictureWhitening    

           欢迎star,你的星星是我持续创作的动力

二:上一下效果图



下面我们看一下功能: 1.首先文字的颜色是可以换的;

                                   2.文字的大小是可以控制的

                                   3.给定宽度,文字的高度是动态计算的 

                                   4.文字的高度不会超出图片等等。

三:上方法声明

 
/**
图片合成文字
@param text            文字
@param fontSize        字体大小
@param textColor       字体颜色
@param textFrame       字体位置
@param image           原始图片
@param viewFrame       图片所在View的位置
@return UIImage *
*/
+ (UIImage *)imageWithText:(NSString *)text
textFont:(NSInteger)fontSize
textColor:(UIColor *)textColor
textFrame:(CGRect)textFrame
originImage:(UIImage *)image
imageLocationViewFrame:(CGRect)viewFrame;
参数确实多了一点,但是为了更加智能,好用。。我发现一个参数也不能去掉。 

四:方法实现

+ (UIImage *)imageWithText:(NSString *)text
textFont:(NSInteger)fontSize
textColor:(UIColor *)textColor
textFrame:(CGRect)textFrame
originImage:(UIImage *)image
imageLocationViewFrame:(CGRect)viewFrame {

if (!text)      {  return image;   }
if (!fontSize)  {  fontSize = 17;   }
if (!textColor) {  textColor = [UIColor blackColor];   }
if (!image)     {  return nil;  }
if (viewFrame.size.height==0 || viewFrame.size.width==0 || textFrame.size.width==0 || textFrame.size.height==0 ){return nil;}

NSString *mark = text;
CGFloat height = [mark sizeWithPreferWidth:textFrame.size.width font:[UIFont systemFontOfSize:fontSize]].height; // 此分类方法要导入头文件
if ((height + textFrame.origin.y) > viewFrame.size.height) { // 文字高度超出父视图的宽度
height = viewFrame.size.height - textFrame.origin.y;
}

//    CGFloat w = image.size.width;
//    CGFloat h = image.size.height;
UIGraphicsBeginImageContext(viewFrame.size);
[image drawInRect:CGRectMake(0, 0, viewFrame.size.width, viewFrame.size.height)];
NSDictionary *attr = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize], NSForegroundColorAttributeName : textColor };
//位置显示
[mark drawInRect:CGRectMake(textFrame.origin.x, textFrame.origin.y, textFrame.size.width, height) withAttributes:attr];

UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return aimg;
}


最后:github地址里还附带了,图片美白,变灰,旋转,图片合成等方法。。欢迎star啊

如果你喜欢这篇文章,或者有任何疑问,可以扫描第一个二维码,加楼主好友哦

也可以扫第二个二维码,关注楼主个人微信公众号。这里有很多生活,职业,技术相关的文章哦。欢迎您的到来。

微信号:

 
                                           公众号
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息