SwiftUI - Capitalise first letter - swiftui

I'm looking for format a text to automatically capitalise the first letter of every word
Text("hello world")
Will show as:
Hello World

Any Swift (NS)String API can be used also in SwiftUI
Text("hello world".capitalized)

Related

SwiftUI Text doesn't fit full width while having multiple lines

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.

SwiftUI Double Backslash in Text without Raw Strings

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

Google Sheets: Conditional Formatting Based On a Cell - If Contains String

In Google Sheets, is it possible to have conditional formatting on cell A1 that changes the colour if B1 contains the string "Hello World!", but not necessarily exactly the exact same?
try:
=REGEXMATCH(LOWER(B1), "hello world")

Indenting UIlabel around another UILabel (Drop Capping)

I would like to produce this effect:
The number is a UILabel and the words are a UILabel. I can't separate the text into two UILabel's. The first two lines of the words UILabel should match up with the number UILabel, then the third line (etc..) should be below the number UILabel.
What you seem to be looking for is CoreText. See the following answer:
Drop cap with NSAttributedString

How to replace all found ocurrences in a Google Docs for an hyperlink

We are actually wondering how can you for example find Bible verses in the document text and replace them for an URL of the verse on the web.
For example if you have a "Jn 3.1" text it will be replaced for an hiperlink like this:
Text= Jn 3.1
Link= https://www.bible.com/1/jn.3.1
we though on using Body.replaceText(searchPattern, replacement) but you cant use that for insert an hyperlink.
And also we must think that the number of characters of the verse can change, for example, it can be:
Jn 1.3
that is 6 characters or can be
John 10.10
that is 10 characters. I think that this can be covered with regex (if we are be able to use them with the solution, so its irrelevant if the solution cover it.
For this kind of modifications you will have to use the Appsscript functions. They work in the same way than normal javascript functions but here you are able to work directly with the text.
for this case the replace function is: replaceText(searchPattern, replacement)
and this is how you can search the word in your document and then replace the text.
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var word = 'example';
var rep = 'replacement';
var body = doc.getBody().editAsText().findText(word);
var elem = body.getElement().asText();
var idx = elem.editAsText().getText().indexOf(word);
elem.replaceText(word, rep);
}
So basically you find the element that contains the desired word, then you will get the element and then you will edit the text contained in that element.
I personally don't like to put complete urls in the text, rather i would use and inline link so in this case "Jn 1.3" would be the text of the hyperlink.
For that, instead of the replaceText line, you can use:
var result = elem.setLinkUrl(idx, idx+word.length -1, 'www.google.com');
It will be easier to read. I hope it helps.