Word Wrap Trick

Word Wrap Trick in WinCC OA

In WinCC OA, one of the more overlooked features is the word wrap for text. There are several ways to implement a word wrap that are included in the standard language. However, they all have their respective limitations. Some methods are exclusive to specific objects like the "text" or the "textfield" object and may not work for others like the "label". The property "text format" can be utilized but it's pixel width-centric and can cause issues with multiple languages.

A common solution is to use one word wrap for certain objects and text format for others. But, there's an alternative that offers more flexibility: implementing the word wrap in the symbol code. Whether you're extracting the text from a DP property or it's being parsed some other way, the following code offers control on a character-by-character basis rather than being pixel-based:


int textLength = text.length();
int space = text.count(" ");
int currentLineLength = 0;
int maxLineLength = 4;  // You can modify this as per your requirements.
string wrappedText = "";

for (int i = 0; i < textLength; i++) {
    if (text[i] == ' ') {
        if (currentLineLength + 1 > maxLineLength) {
            wrappedText = wrappedText + "\n";
            currentLineLength = 0;
        } else {
            currentLineLength++;
        }
    } else {
        wrappedText = wrappedText + text[i];
        currentLineLength++;
    }
}
txtLabel.text(wrappedText);

To further refine this approach, especially for multilingual support, consider the modification below. This takes into account words of different languages such as "de", "of", "vor", and more:


int textLength = text.length();
int space = text.count(" ");
int currentLineLength = 0;
int maxLineLength = 4;  // Modify this value as needed.
string wrappedText = "";

for (int i = 0; i < textLength; i++) {
    if (text[i] == ' ') {
        if (currentLineLength + 1 > maxLineLength) {
            wrappedText = wrappedText + "\n";
            currentLineLength = 0;
        } else {
            currentLineLength++;
        }
    } else if (text.mid(i, 3) == "de ") {
        wrappedText = wrappedText + "de ";
        i += 2;
        currentLineLength += 3;
    } else {
        wrappedText = wrappedText + text[i];
        currentLineLength++;
    }
}

Note: You can add more conditions for different words or adjust the maxLineLength for optimal wrapping. This script ensures compatibility with all symbols that accept strings.

The above approach provides granular control over the wrapping process and offers a robust solution, especially when dealing with multilingual content in WinCC OA.