SwiftUI: How to suppress line breaks for a part of texts? - swiftui

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.

Related

Is there a SwiftUI equivalent for lineBreakMode for Text views?

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.

Readme.io - How to indent multiline list item

How do I indent a multiline list item so that the designator so that both lines are flush left and the designor (in this case the d) is outside of them to the left.
So it appears like this:
I've tried two spaces at the end of first line with a break, placing where I want the break to happen, indenting manually, nothing seems to work.
Instead of indenting manually, just let the line run on:
1. **Expired:** a post that was once live, but has been removed from the Suggested Library either manually by an admin, or reached its set expiration date
If you must break the line manually, indent further lines by four spaces, e.g.
1. **Expired:** a post that was once live,
but has been removed from the Suggested
Library either manually by an admin, or
reached its set expiration date
This will work by default in most Markdown implementations I'm aware of, though bear in mind that Markdown itself is only concerned with content. Presentation rules (e.g. CSS) can further modify presentation, including alignment of lists.

Adobe brackets: write spaces instead of tabs

I am using brackets with coffeescript, but when I hit Tab, it insert a tabulation whereas i only need 2 spaces. Also, when I create a line break, the indent is tabs, and not spaces. Can I change these 2 setting ?
In the lower-right of the status bar you should see an indicator saying "Tab Size." Click the label to toggle to spaces. To change the amount of indent, click the number next to it and type a new value.
Note: if the indicator already says "Spaces" then Brackets should be using spaces instead of tab characters already. But it might not feel that way because when you move the cursor or press Backspace, there's a "soft tabs" behavior: the cursor will smartly skips over contiguous spaces to line up evenly with the next tab stop. If that bothers you, there will be a preference in the next release of Brackets (Sprint 38) to disable that behavior, making the cursor never move more than one space at a time.
For language specific control, Brackets allows you to provide different tab and space indentation values in the brackets.json file. For example:
"language": {
"html": {
"spaceUnits": 4
},
"javascript": {
"tabSize": 2
}
}
For changing tab spacing you can edit your brackets.json file. you can find it on Debug->Open Preferences File and simple add "spaceUnits": 2 at the end of the file for 2 space. Remember to add a comma on previous line. You can edit that file for customizing your bracket.
Or you can change it more easily at the right-bottom of your bracket interface you saw a option Space: 4 click on the number and change it as your wish...

How do tabs work for text boxes?

I'm making a game gui api and I'm wondering how to implement tabs. I'm using freetype for text. When I try to render '\t' It looks like a square. I'm wondering how tabs are implemented because they are not a fixed width.
Thanks
For a fixed-width font you could compute how many spaces to the next tab stop, but the general solution is to stop rendering when you hit a tab, move to the next tabstop, and then render the text that comes after the tab character starting from there. Where the tabstops are is up to you, but a good default is probably something like every 8 ems.
The simplest strategy is to swap a tab out for a some number of spaces. I.e. when the user pushes tab, pretend they pushed tab four times instead. (Or just print out four spaces, whatever works for you)

Change tab stop size in rendered HTML using Qt's QLabel class

I'm rendering some HTML in a QT QLabel. The HTML looks like this:
<pre>foo\tbar</pre>
(note that I've put "\t" where there is a tab chracter in the code).
This renders fine, but the tab character appears to be rendered as eight spaces, whereas I want it to be redered as 4. How can I change this without having to change the source HTML?
According to W3 (HTML4):
The horizontal tab character (decimal 9 in [ISO10646] and [ISO88591]) is usually interpreted by visual user agents as the smallest non-zero number of spaces necessary to line characters up along tab stops that are every 8 characters. We strongly discourage using horizontal tabs in preformatted text since it is common practice, when editing, to set the tab-spacing to other values, leading to misaligned documents.
It's implementation-defined, essencially. Most, if not all, browsers/renderers use eight spaces for tabs. This cannot be configured in Qt.
It is, however somewhat trivial to go through your HTML and replace tabs with however many spaces you wish. Write a simple parser for that. Pseudocode:
for each <pre> block {
for each line in block {
position_in_line = 0
for each character in line {
if character is a tab {
remove tab character
do {
add a space character
++position_in_line
} while position_in_line % 8 != 0
} else {
++position_in_line
}
}
}
}
In case you're curious, HTML3 specifies the use of eight-character tabs:
Within <PRE>, the tab should be interpreted to shift the horizontal column position to the next position which is a multiple of 8 on the same line; that is, col := (col+8) mod 8.
While QLabel uses a QTextDocument internally when rendering rich text, it does not allow access to it in it's API. However, since QTextDocument is a QObject, you can try to use
QTextDocument * tl = label->findChild<QTextDocument>();
to get access to it (will work if the QLabel creates the QTextDocument as a (direct or indirect) child of itself).
Once you have a pointer to the text document, you can use the QTextDocument API, e.g. QTextOption::setTabsStop(), to change the tab stops.
The last step is to somehow make the QLabel repaint itself. Probably a call to QWidget::update() suffices, but caching (or worse, recreating the text document) might thward that. In this case, you can register an event listener on the label to adjust the text document just prior to a paintEvent(), but note that the sizeHint() might also change when the tab stops change, so it might be more complicated still.
That said, it's how I'd approach the problem.
Try this:
<pre style="tab-interval:0.5in">foo\tbar</pre>
Could work