iOS 图片上绘制文字


- (UIImage *)imageWithTitle:(NSString *)title fontSize:(CGFloat)fontSize

{

    //画布大小

    CGSize size=CGSizeMake(self.size.width,self.size.height);

    //创建一个基于位图的上下文

    UIGraphicsBeginImageContextWithOptions(size,NO,0.0);//opaque:NO  scale:0.0

    

    [self drawAtPoint:CGPointMake(0.0,0.0)];

    

    //文字居中显示在画布上

    NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paragraphStyle.alignment=NSTextAlignmentCenter;//文字居中

    

    //计算文字所占的size,文字居中显示在画布上

    CGSize sizeText=[title boundingRectWithSize:self.size options:NSStringDrawingUsesLineFragmentOrigin

                                     attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:[UIColor whiteColor]}context:nil].size;

    CGFloat width = self.size.width;

    CGFloat height = self.size.height;

    

    CGRect rect = CGRectMake((width-sizeText.width)/2, (height-sizeText.height)/2, sizeText.width, sizeText.height);

    //绘制文字

    [title drawInRect:rect withAttributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:[ UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle}];

    

    //返回绘制的新图形

    UIImage *newImage= UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM