Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.7.0+

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/order/12345")
    json.updateInt(jsonPath: "render.moduleSize", value: 6)

    //  The caption.  It need not be the same as the text in the QR code.
    json.updateBool(jsonPath: "render.text.enabled", value: true)
    json.updateString(jsonPath: "render.text.value", value: "ORDER 12345")
    //  position is "below" (the default) or "above".  alignment is "center" (the default), "left" or "right".
    json.updateString(jsonPath: "render.text.position", value: "below")
    //  Each dot of the caption's font is drawn this many pixels wide.
    json.updateInt(jsonPath: "render.text.scale", value: 3)

    json.updateString(jsonPath: "output.path", value: "qa_output/qr_caption.png")
    json.updateBool(jsonPath: "output.overwrite", value: true)
    json.updateString(jsonPath: "output.return", value: "none")

    let fmt = CkoFormats()!
    let jsonOut = CkoJsonObject()!
    success = fmt.qrCode(op: "generate", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(fmt.lastErrorText!)")
        return
    }

    print("caption: \(jsonOut.string(of: "measurements[0].text")!)")
    print("caption scale: \(jsonOut.int(of: "measurements[0].textScale").intValue)")
    print("height added for the caption: \(jsonOut.int(of: "measurements[0].textBandPx").intValue) pixels")
    print("image: \(jsonOut.int(of: "measurements[0].canvasWidth").intValue) x \(jsonOut.int(of: "measurements[0].canvasHeight").intValue) pixels")

    //  The image written to qa_output/qr_caption.png:

    //  image

    //  Sample output:

    //  caption: ORDER 12345
    //  caption scale: 3
    //  height added for the caption: 21 pixels
    //  image: 222 x 243 pixels

}