I would like to prevent line breaks in a parts of SwiftUI Text - for example, when I have label like Car speed is 10 m/s., I would like to force SwiftUI to keep the m/s (or preferrably 10 m/s) together (but still allow it to do linebreaks elsewhere in text).
With default settings, the label might break like this 10 m<br>/s, which I'd like to prevent.
If the character in question would have been dash, I could use a non-breaking dash, but no such luck here.
I am rendering a Text with a custom string inside it surrounded by parenthesis. I am not sure if this is a bug or I am doing something wrong, as the alignment seems totally broken.
Here is the code:
Text("All ()()\(allAutoFX.count) ))(()")
and here is the result, where you can see different base lines for the parenthesis before / after the count:
https://imgur.com/a/oJT8ssY
I am looking for a way using which there should be no emoji character allowed in the textfield
or else a way to find out if there is a emoji present in a string or not [regex]
any one way of it could workout.
I have gone through all the related questions and their solutions but they don't cover the entire use cases.
Eventually I Found the solution to the query so posting the answer here.
For the first part
Restrict Emoji's in textfield
add below code inside your textfield widget.
inputFormatters: [
BlacklistingTextInputFormatter(
RegExp('(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])')
],
this will prevent all kinds of emoji's from the textfield.
2nd part
if there is a emoji present in a string or not [regex]
below mentioned code will detect if any emoji is present in the string or not
if(string.contains(RegExp(r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])'))){
// your code here
}
I have a Text view, and I would like to configure it to wrap the first character that doesn't fit. In UIKit, this would be the equivalent of setting label.lineBreakMode = .byCharWrapping. Has this been implemented for SwiftUI Text yet? I haven't been able to find anything in the documentation for Text.
The reason that I want to do this is that I'm displaying a long code to the user, so wrapping by character rather than by word is desirable.
Not sure if this helps, but I am using the following which leads to long words getting wrapped by characters:
Text("Supercallifragilisticexpialidocious")
.font(.system(size: 100))
.minimumScaleFactor(0.01)
.lineLimit(3)
.multilineTextAlignment(.leading)
Unfortunately for my use case, I do not want the Text to wrap by characters. If I set lineLimit(1) that works fine and the font size is reduced to keep the Text on 1 line. But if Text is multiple words such as Text("Practically perfect in every way") then I want the string wrapped by word. I can't seem to get both Word wrapping for multiple words and font scaling for long words.
I need to use the old Qt pieMenu, I know it's deprecated, But I've found the source code here, and the installations instructions here(they are in the zip file as well)
I've successfully compiled and ran the examples, but my problem is that I need to add spaces and manipulate the font in the text of the pie menu
for example in the editor.cpp example, these lines insert items to the pie menu with the first parameter as the displayed text :
markMenu->insertItem("Cut", this, SLOT(cut()));
markMenu->insertItem("Del", this, SLOT(del()));
markMenu->insertItem("Copy", this, SLOT(copy()));
I tried setting the lines like this as a test :
markMenu->insertItem("item cut", this, SLOT(cut()));
markMenu->insertItem(tr("Del item"), this, SLOT(del()));
markMenu->insertItem("test on item", this, SLOT(copy()));
and the results are like in the photo(everything after the space character is not displayed)
then I used the '\t' instead of using space directly, it worked as one space instead of a tab.
markMenu->insertItem("test\ton\titem", this, SLOT(copy()));
but when trying to add a new line to the text things didn't work right.
I tried using '\n' but didn't work for me.
Another issue, I was trying to change the font settings of the menu, I tried :
markMenu->setFont(QFont("Arial", 9, 5, true)); // size 9, weight 5, italic
but this didn't affect the font, I tried styleSheets as well but no success.
Any ideas how can I display the new liens and adjust the font of the text?
In the link provided by OP, scrolling to bottom, the source code can be found:
painter.drawText(center.x() + x
- metrics.width(itemText(i)) / 2,
center.y() + y, itemText(i));
Now, a look into the Qt doc. of QPainter::drawText() provides the explanation:
This function does not handle the newline character (\n), as it cannot break text into multiple lines, and it cannot display the newline character. Use the QPainter::drawText() overload that takes a rectangle instead if you want to draw multiple lines of text with the newline character, or if you want the text to be wrapped.
...and a possible fix as well. (Emphasize by me.)
As the source code is available, this can be fixed.
To achieve a proper pre-calculation of boundary rectangle, QFontMetrics can be used.
The answer to How to automatically increase/decrease text size in label in Qt shows an example for text boundary determining with QFontMetrics which even considers automatic word-wrapping.