Objective-C Requires Chilkat v11.7.0+
Objective-C
QR Code with a Text Caption
See more QR Code Examples
Demonstrates printing a line of text under a QR code, such as the order number or the address the code holds, so that a person can read it too. render.text turns the caption on and gives its value, which is printable ASCII and is drawn in a built-in font that needs no font files. The caption can go above or below the symbol, be aligned left, center or right, and be scaled.Chilkat Objective-C Downloads
#import <CkoJsonObject.h>
#import <CkoFormats.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"payload.text" value: @"https://www.chilkatsoft.com/order/12345"];
[json UpdateInt: @"render.moduleSize" value: [NSNumber numberWithInt: 6]];
// The caption. It need not be the same as the text in the QR code.
[json UpdateBool: @"render.text.enabled" value: YES];
[json UpdateString: @"render.text.value" value: @"ORDER 12345"];
// position is "below" (the default) or "above". alignment is "center" (the default), "left" or "right".
[json UpdateString: @"render.text.position" value: @"below"];
// Each dot of the caption's font is drawn this many pixels wide.
[json UpdateInt: @"render.text.scale" value: [NSNumber numberWithInt: 3]];
[json UpdateString: @"output.path" value: @"qa_output/qr_caption.png"];
[json UpdateBool: @"output.overwrite" value: YES];
[json UpdateString: @"output.return" value: @"none"];
CkoFormats *fmt = [[CkoFormats alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [fmt QrCode: @"generate" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",fmt.LastErrorText);
return;
}
NSLog(@"%@%@",@"caption: ",[jsonOut StringOf: @"measurements[0].text"]);
NSLog(@"%@%d",@"caption scale: ",[[jsonOut IntOf: @"measurements[0].textScale"] intValue]);
NSLog(@"%@%d%@",@"height added for the caption: ",[[jsonOut IntOf: @"measurements[0].textBandPx"] intValue],@" pixels");
NSLog(@"%@%d%@%d%@",@"image: ",[[jsonOut IntOf: @"measurements[0].canvasWidth"] intValue],@" x ",[[jsonOut IntOf: @"measurements[0].canvasHeight"] intValue],@" pixels");
// The image written to qa_output/qr_caption.png:
//
// Sample output:
// caption: ORDER 12345
// caption scale: 3
// height added for the caption: 21 pixels
// image: 222 x 243 pixels