stackoverflow.com/questions/28906914/how-do-i-add-text-to-an-image-in-ios-swift

 

How do I add text to an image in iOS Swift?

I have looked around and have been unsuccessful at figuring out how take text, overlay it on an image, and then combine the two into a single UIImage. I have exhausted Google using the search term...

stackoverflow.com

swiftify.com/converter/code/

 

Objective-C to Swift Converter | Swiftify

 

swiftify.com

- (UIImage*)drawText:(NSString*)param1 tText:(NSString *)param2 inImage:(UIImage*)image data:(DDTBT_DFCT *)data yn:(BOOL)ynynyn {

    int size = 25;

    int ymargin = 5;

    int coeficient = 10;

   

    if (image == nil) {

        return nil;

        CGSize size = CGSizeMake(10, 10);

        UIGraphicsBeginImageContextWithOptions(size, YES, 0);

        [[UIColor whiteColor] setFill];

        UIRectFill(CGRectMake(0, 0, size.width, size.height));

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return image;

    }

   

    UIFont *font = [UIFont boldSystemFontOfSize:size];

    CGSize textsize = CGSizeMake(20, 20);

    CGPoint margin = CGPointMake(20, 20);

    CGRect textrect = CGRectMake(image.size.width - 0.7 * textsize.width + 0.3 * textsize.width - margin.x,

                                 image.size.height - textsize.height - margin.y,

                                 textsize.width,

                                 textsize.height);

    UIGraphicsBeginImageContext(image.size);

   

    NSString *line1 = [NSString stringWithFormat:@"%@", GlobalVar.globalVar.userSelectionSiteInformation.nm_site];

    NSString *nonzerodong = @"";

    if ([data.cd_dong characterAtIndex:0] == '0') {

        nonzerodong = [data.cd_dong substringFromIndex:1];

    } else nonzerodong = data.cd_dong;

 

//  Your converted code is limited to 1 KB.

//  Please Sign Up (Free!) to double this limit.

//

//  Converted to Swift 5.3 by Swiftify v5.3.19197 - https://swiftify.com/

func drawText(_ param1: String?, tText param2: String?, in image: UIImage?, data: DDTBT_DFCT?, yn ynynyn: Bool) -> UIImage? {

    var image = image

    let size = 25

    let ymargin = 5

    let coeficient = 10

 

    if image == nil {

        return nil

        let size = CGSize(width: 10, height: 10)

        UIGraphicsBeginImageContextWithOptions(size, true, 0)

        UIColor.white.setFill()

        UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height))

        var image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return image

    }

 

    let font = UIFont.boldSystemFont(ofSize: size)

    let textsize = CGSize(width: 20, height: 20)

    let margin = CGPoint(x: 20, y: 20)

    let textrect = CGRect(

        x: (image?.size.width ?? 0.0) - 0.7 * textsize.width + 0.3 * textsize.width - margin.x,

//

//

//  %< ----------------------------------------------------------------------------------------- %<

 

1Kbyte만 번역 된다 해도 API 찾는 수고를 덜 수 있다. 로그인 하면 오히려 1일 개수 제한 걸린다. 뭐, 큰 프로젝트 하게 되면 사겠으나...

행여 사게 되면 프로젝트 단위 번역이 한 방에 되는지 확인 필요함.

 

 

 

+ Recent posts