1. 程式人生 > >iOS開發 在圖片上新增文字,圖片合成文字,圖片上新增富文字,美圖秀秀,美顏相機文字編輯

iOS開發 在圖片上新增文字,圖片合成文字,圖片上新增富文字,美圖秀秀,美顏相機文字編輯

+ (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啊