there's an issue with SwiftUI's Text view while having multiple lines. So just look:
var body: some View {
Text("Some word Some word Some word Some word")
.frame(maxWidth: .infinity, alignment: .leading)
.border(.red)
.padding(16)
}
However adding one word changing the wrapping:
As result the view doesn't look as expected, for bigger fonts the difference become more visible:
Environment: iOS 15.2, Xcode 13 / Xcode 14b Preview and Simulator
Is there a reliable solution to wrap strings similarly as UILabel do without using UIViewRepresentable?
PS: already tried different combinations with .fixedSize() + .frame(idealWidth:) and no luck
Apple is following typography rules. One of those rules is to avoid having a single word on the last line of a paragraph. This runt, as it is called, is believed to break a reader's focus due to all the white space. However, if the last word and any trailing punctuation is greater than 10 characters this rule does not apply.
No option exists to turn this rule off directly to my knowledge. But a workaround is to use trailing spaces at the end of the text where this could occur. Use enough trailing spaces to make the last line have greater than 10 characters.
Using your example, adding 7 trailing spaces works. "Some" plus 7 spaces being 11 characters:
Text("Some word Some word Some word Some word Some ")
In this example, 8 trailing spaces works. "the" plus 8 spaces provides 11 characters so the rule does not apply and the line breaks where desired:
Text("Some word to demonstrate the ")
And with the trailing spaces, the layout should look ok with larger devices and when orientation changes occur. Fortunately the trailing spaces are truncated and will not create a new line on their own.
extension String {
var padded: String { self + " " }
}
Usage:
Text("Some word to demonstrate the".padded)
Side note: UILabel(versions 13.0, 16.1) also appears to use these typography rules by default. But a UILabel can turn off this typography rule with the line break strategy setting:
label.lineBreakStrategy = NSParagraphStyle.LineBreakStrategy()
Or like this:
label.lineBreakStrategy = []
But it doesn't appear to be available in SwiftUI yet.
Related
In SwiftUI how can i make Text to show Double Backslashes?
Escaping does not seem to help
Text("Hello \\\\") // renders to: Hello \
Expected Output: Hello \\
Raw Strings is not an option here, as localization is not possible without the right escape sequence.
Localization of the String MUST be possible!
Here is possible variant
Text(verbatim: "Hello \\\\")
Tested with Xcode 13.3 / iOS 15.4
So I have a string. Let's say for the argument it is this one:
1234567891113SomeTextExample
I want to have two regular expresions:
Select from beginning to, say, 6th position;
Select from 8th position to 12th position.
I know how to select everything AFTER specific position, e.g.:
(?<=.{6})(.*)$
would select everything after 5 characters.
I am using Sublime Text editor and need to cleanup some logs and these two expressions would save a whole lot of time.
use ^ to get your regex to start at the beginning.
Beginning to 6th position : ^(.{6})
var str = 'xdcfvgbhdsds';
var regex = /^(.{6})/;
console.log(regex.exec(str)[1]);
8th to 12th position : ^.{7}(.{5})
var str = 'xdcfvgbhddsfsffsds';
var regex = /^.{7}(.{5})/;
console.log(regex.exec(str)[1]);
Beginning to 6th position (Demo):
^(.{6}).*$
Characters 8 to 12, inclusive on both ends (Demo):
^.{7}(.{5}).*$
I am assuming here that you want to capture these specific ranges for some sort of use.
Finally I found it out.
First one - Select from beginning to, say, 6th position:
^(.{6})
Thanks Zenoo for this.
And select from 8th position to 12th position:
^(.{8})|(?<=.{12})(.*)$
Well, at least this one works in Sublime Text. I am sure there are lots and lots of editors/applications which are fine with Zenoo's approach (^.{7}(.{5})).
I'm trying to display text which may at times contain a math expression so I am using MTMathUILabel from iosMath. I generate the labels dynamically and add them to a stack as I pull the strings from the db. The problem is that all text which is not math appears with no spaces. i.e:
In db: Solve the following equation: (math here)
In label: Solvethefollowingequation: (math here)
Here is what I have tried so far:
for question in all_questions {
let finalString = question.question?.replacingOccurrences(of: " ", with: "\\space", options: .literal, range: nil)
let label = MTMathUILabel()
label.textColor = UIColor.black
label.latex = finalString
stack.addArrangedSubview(label)
}
But the problem is that it literally places two . And xcode doesn't let me write just one \ because it is not escaped. However if I just write
print("\\space")
Then it will print just one.
How can I fix this so I add only one \? If this cannot be done, how can I achieve what I want? Is there a better library out there?
After giving a quick look at MTMathUILabel's doc and LaTeX conventions, I believe you should replace your spaces with a tilde character "~". This will make them non-breaking spaces and avoid the backslash issue (which is probably due to \space not being understood by MTMathUILabel).
Systematic replacement of all spaces may yield undesirable result if the formula itself has legitimate spaces in it.
For example, a quadratic equation would be expressed as:
x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}
You will end up replacing spaces inside curly braces, and that may or may not be what you want:
x~=~\frac{-b~\pm~\sqrt{b^2-4ac}}{2a}
The place I work has mixed-indents all over. Unfortunately, that is the style the company has decided on (yuck.) I would rather not see the "mixed-indent" errors produced by syntastic in vim, but I don't want to turn it off altogether. Is there a way to disable whitespace checks or something? Or can I switch to a better checker somehow? My vimrc automatically removes trailing whitespace and fixes indents, so I really don't need those checks anyway.
I suppose it's vim-airline.
Reading the help:
* customize the type of mixed indent checking to perform.
" must be all spaces or all tabs before the first non-whitespace character
let g:airline#extensions#whitespace#mixed_indent_algo = 0 (default)
" certain number of spaces are allowed after tabs, but not in between
" this algorithm works well for /** */ style comments in a tab-indented file
let g:airline#extensions#whitespace#mixed_indent_algo = 1
" spaces are allowed after tabs, but not in between
" this algorithm works well with programming styles that use tabs for
" indentation and spaces for alignment
let g:airline#extensions#whitespace#mixed_indent_algo = 2
Also:
* configure which whitespace checks to enable.
" indent: mixed indent within a line
" long: overlong lines
" trailing: trailing whitespace
" mixed-indent-file: different indentation in different lines
let g:airline#extensions#whitespace#checks = [ 'indent', 'trailing', 'long', 'mixed-indent-file' ]
I'm having trouble with a regular expression, I have several images with file name that need changing. I've done them by hand. It was quick easy and painless. However, I wanted to know what I needed to do as a simple replacement reg ex using JavaScript. And that's when it doesn't quite work out. The image is called "muti blossom 02.png" and it's going to be re-sized and saved out as JPEGs with the name "iOS_multi_BLOSSOM_2048.jpg". The others are of the same form but have different nouns; winter, leaf, circus etc.
The file-name is structured as follows:
"mutli" at the start (lower case),
white space,
the noun (lower case),
white-space,
a number (that may have a preceding 0 and may be one or two digits),
file extension which may be .png or .psd (lowercase).
It then needs to be changed to:
iOS_multi (camel case as written),
noun (UPPERCASE),
2048 (new fixed size),
new file extension .jpg(lowercase).
I know that ([a-z]+\s) matches "multi" and that (\s\d+.[a-z]+$) will match the numbers and file extension, but have no idea how to successfully match the bit in the middle as well. And do the uppercase on the noun. But I'm sure there is someone else that does. Thank you.
In JavaScript regex you cannot do this with a replace as it is not possible to uppercase the replacement text. However the match method will return an array which you can then manipulate.
var oldImageName = "multi blossom 02.png";
var matches = oldImageName.match(/multi (\w+) \d{1,2}\.(?:png|psd)/);
var newImageName = "iOS_multi_" + matches[1].toUpperCase() + "_2048.jpg";
Note: this assumes that the "noun" is a single word with no spaces
I was searching for "javascript Regex to replace characters that Windows doesn't accept in a filename" but found nothing,
so here is regex to strip chars from filename that windows filesistem do not allow (/\:?<>|"):
var originalFileName='some filename:with"forbidden/>\? chars.in';
var strippedFileName=originalFileName.replace(/[/\\:?<>|\"]+/g, "")
console.log(strippedFileName);