you are an ios swift expert and Epson printer TM-L100 iOS SDK expert. Need to print two order items in two lines with strikethrogh text. There is not sdk for Strikethrough text. how can it be done?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course. As an expert in iOS and the Epson ePOS SDK, I can confirm you are correct. The standard command set in the ePOS-SDK for iOS (which is a wrapper around the ESC/POS command language) does not include a direct command for strikethrough text styling.
The most robust and recommended solution is to render the text with a strikethrough effect into a UIImage within your iOS app, and then print that image.
This approach gives you full control over the appearance (font, size, line thickness) and works reliably on any Epson printer that can print graphics, including the TM-L100.
Here is a complete, step-by-step guide and code example.
Strategy: Text-to-Image Rendering
Create a Helper Function: This function will take a String and a UIFont as input and produce a UIImage containing that text with a strikethrough style.
Use NSAttributedString: This is the key to applying rich text styles like strikethrough in iOS.
Render the Attributed String: Draw the NSAttributedString into a UIGraphicsImageRenderer context to create the final UIImage.
Use the Epson SDK: Add the generated UIImage to the printer command builder using the addImage method.
Step 1: Create the Text-to-Image Helper Function
First, let's create a reusable function to generate our image. Add this extension to UIViewController or place it in a dedicated helper file.
swift
import UIKit
extension UIViewController {
/// Creates a UIImage from a string with a strikethrough style.
/// - Parameters:
/// - text: The string to render.
/// - font: The font to use for the text.
/// - textColor: The color of the text. Defaults to black.
/// - Returns: A UIImage containing the rendered text, or nil if it fails.
func createImageWithStrikethrough(text: String, font: UIFont, textColor: UIColor = .black) -> UIImage? {
// 1. Create the NSAttributedString with the strikethrough style
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: textColor,
.strikethroughStyle: NSUnderlineStyle.single.rawValue,
.strikethroughColor: textColor
]
let attributedString = NSAttributedString(string: text, attributes: attributes)
// 2. Calculate the size needed for the image
let size = attributedString.boundingRect(
with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
context: nil
).size
// Ensure size is not zero
guard size.width > 0, size.height > 0 else { return nil }
// 3. Render the attributed string into a UIImage
let renderer = UIGraphicsImageRenderer(size: size)
let image = renderer.image { context in
attributedString.draw(at: .zero)
}
return image
}
}
Step 2: Integrate with the Epson ePOS-SDK
Now, in your printing logic, you will call this helper function for each line item and add the resulting images to the Epos2PrinterBuilder.
Important: You need to know the dot-width of your printer paper. The TM-L100 often uses 80mm paper, which corresponds to 576 dots wide. We will use this in our example.
Here's how your printing function would look:
swift
// Assuming you have an Epos2Printer object named `printer`
// and you've already connected to it.
func printStrikethroughOrderItems() {
// 1. Define your order items and the font
let item1Text = "Regular Burger $10.00"
let item2Text = "Cheese Burger $12.00"
// Choose a clear, simple font. Sans-serif works best for thermal printers.
// The size determines the quality. A larger size will look better when scaled down.
let printFont = UIFont(name: "HelveticaNeue", size: 24) ?? UIFont.systemFont(ofSize: 24)
// 2. Create the images with strikethrough text
guard let strikethroughImage1 = createImageWithStrikethrough(text: item1Text, font: printFont),
let strikethroughImage2 = createImageWithStrikethrough(text: item2Text, font: printFont) else {
print("Error: Could not create images from text.")
return
}
// 3. Build the print commands
var builder: Epos2PrinterBuilder?
builder = Epos2PrinterBuilder(
printerSeries: EPOS2_TM_L100, // IMPORTANT: Use your specific printer series
lang: EPOS2_MODEL_ANK
)
if builder == nil {
print("Error: Failed to initialize Epos2PrinterBuilder.")
return
}
// --- Add a header if you want ---
builder?.addTextSize(2, height: 2)
builder?.addText("VOIDED ITEMS\n")
builder?.addTextSize(1, height: 1)
builder?.addFeedLine(1)
// --- Add the first strikethrough item ---
// The printer will binarize the image (convert to black and white).
// EPOS2_HALFTONE_THRESHOLD is best for crisp text.
builder?.addImage(
strikenImage1,
x: 0,
y: 0,
width: Int(strikenImage1.size.width),
height: Int(strikenImage1.size.height),
color: EPOS2_COLOR_1,
mode: EPOS2_MODE_MONO,
halftone: EPOS2_HALFTONE_THRESHOLD,
brightness: 1.0,
compress: EPOS2_COMPRESS_AUTO
)
builder?.addFeedLine(1) // Add a line break after the first item
// --- Add the second strikethrough item ---
builder?.addImage(
strikenImage2,
x: 0,
y: 0,
width: Int(strikenImage2.size.width),
height: Int(strikenImage2.size.height),
color: EPOS2_COLOR_1,
mode: EPOS2_MODE_MONO,
halftone: EPOS2_HALFTONE_THRESHOLD,
brightness: 1.0,
compress: EPOS2_COMPRESS_AUTO
)
builder?.addFeedLine(2) // Add some space at the end
// --- Add footer and cut ---
builder?.addCut(EPOS2_CUT_FEED)
// 4. Send the data to the printer
if let data = builder?.getBuilderData() {
// The `printer` object is your connected Epos2Printer instance
printer?.sendData(data, timeout: 30000)
}
// Don't forget to clear the builder buffer
builder?.clearCommandBuffer()
}
Key Considerations & Pro-Tips
Performance: Generating images is computationally more expensive than sending plain text. For a receipt with only a few styled items, this is perfectly fine. If you were printing hundreds of such lines, you might notice a slight delay.
Font Choice: Use a standard, non-fancy sans-serif font like "Helvetica Neue" or "Arial". These render cleanly and are easy to read on low-resolution thermal printers.
Image Width: The width parameter in addImage is in dots. Ensure the UIImage you create is not wider than your printer's maximum printable width (e.g., 576 dots for 80mm). The createImageWithStrikethrough function automatically sizes the image to fit the text, so you just need to ensure your text string isn't excessively long.
Halftone/Dithering: For black and white text, EPOS2_HALFTONE_THRESHOLD is the best choice. It converts pixels to either pure black or pure white with no gray patterns, resulting in the sharpest possible output for text.
Advanced Alignment (Left/Right): If you need to align text (e.g., item name on the left, price on the right), you can create a more advanced image-rendering function that draws two NSAttributedStrings at different positions within a single, full-width UIImage. This gives you pixel-perfect control over your layout.
This text-to-image method is the industry-standard workaround for unsupported text styles on thermal receipt printers and will serve you well for this task.