Creating hyperlink from text in Swift - ios

I have a json file which has all the information in text.
One of the node in json looks like this :
"description": "File. Verify File"
What I would like to do is create a hyperlink in a UILabel textfield in swift like :
File ***Verify File***
I tried to do lookup on stackoverflow but nothing was concrete available. I wrote the following code to get the "href" text using Regex.
let regexOptions:NSRegularExpressionOptions?
= NSRegularExpressionOptions.CaseInsensitive
let regex = try NSRegularExpression(pattern: "<a[^>]+href=\"(.*?)\"[^>]*>.*?</a>", options: regexOptions!)
And then use NSMUtableAttributedString to create a link of it. Am I in the correct direction ?

Yes, it's a good approach but you will work more than if your json only have the essential information: { "description": link" } so you can use a UIButton with a #IBAction attached to open a WebController or something like that with the link.

Related

How to use dynamic localization keys with SwiftUI

I'm trying to display a localized text based on an ID. I have a lot of these, so it feels more efficient to just add dynamically the ID to the string.
The string file looks like this:
"users.1.name" = "Alice"
"users.2.name" = "Bob"
"users.3.name" = "Charles"
...
If I do the following, hardcoding the ID, it works as expected and displays the associated translated key:
Text("users.1.name")
However if I do this it only displays the string:
Text("users.\(user.id).name")
// displays "users.1.name" instead of "Alice"
I've also tried:
Text(LocalizedStringKey("users.\(user.id).name"))
// displays "users.1.name" instead of "Alice"
Am I missing something or is this not possible?
You need then the following. Tested with Xcode 11.4 / iOS 13.4
Text(NSLocalizedString("users.\(id).name", comment: ""))
I'm not a SwiftUI expert but you can use:
let key:String = "users.\(user.id).name"
Text(LocalizedStringKey(key))
// OR compact
Text(LocalizedStringKey(String("users.\(user.id).name")))

iOS: Localise multi-line string literals

Since a recent swift version, multi line string literals are available that allow to format the appearance of a multi line string easily.
I'm looking for a way, though, to localise such a string.
Here is an example of the text of a pre-configured mail, that users can send:
mailComposerVC.setMessageBody("""
Hi,
I would like to share the following feedback:
""",
isHTML: false)
It seems there is no way to properly transform this into the localisable .strings file.
As a workaround, I came up with the solution to individually localise each part of the message and use interpolation:
let localisedGreeting = NSLocalizedString("Hi", comment: "")
let localisedMessage = NSLocalizedString("I would like to share the following feedback: ", comment: "")
mailComposerVC.setMessageBody("""
\(localisedGreeting),
\(localisedMessage)
""",
isHTML: false)
The .strings file looks like this:
"Hi" = "Hallo";
"I would like to share the following feedback: " = "ich möchte folgendes Feedback geben: ";
Is there a better/more concise way to do this?
Both keys and values in the Localizable.strings file can be multi-line.
With
"Hi,
I would like to share the following feedback:
" = "Hallo,
ich möchte folgendes Feedback geben:
";
the call
let localizedMessage = NSLocalizedString("""
Hi,
I would like to share the following feedback:
""", comment: "")
expands as intended.
This might however not work with localization tools, and also does not
display with the correct syntax coloring in the Localizable.strings, which
might be confusing.
I would use a short (single-line) key instead, and localize that for all
languages (including the default language english).
For me this layout of text and quotes worked (Swift 5.3, Xcode 12)
Localizable.strings
"First Line
Second Line" = "Erste Linie
Zweite Linie";
Implementation
let lines = NSLocalizedString("""
First Line
Second Line
""", comment: "")

How to perform on the fly image transformations in iOS using Cloudinary

I am looking to perform image transformations using cloudinary on a web image url using the "fetch" type. I am looking to do something similar to the JAVA example for android here, but using Swift.
How can I achieve this in Swift?
Thanks!
There are many ways to tackle this, for example you can use something like that:
let options:NSDictionary = [
"transformation": CLTransformation(dictionaries: [["width":300,"height":300,"crop":"fill","gravity":"face","radius":"max","fetch_format":"auto",]]),
"type": "fetch"
]
let url:String = Cloudinary!.url("http://upload.wikimedia.org/wikipedia/commons/0/0c/Scarlett_Johansson_Césars_2014.jpg", options:options1 as! [AnyHashable: Any])
The result url in this case will be: http://res.cloudinary.com/demo/image/fetch/c_fill,f_auto,g_face,h_300,r_max,w_300/http://upload.wikimedia.org/wikipedia/commons/0/0c/Scarlett_Johansson_C%C3%A9sars_2014.jpg

Terms and Conditions View Controller for iOS

I was trying to make a simple Terms and Conditions view controller for my app. I was figuring a ScrollView with a Text Field.
What I am running into is displaying a really long formatted text string with multiple line feeds and quoted words and phrases.
NSString #termsAndConditions = #"Terms and Conditions ("Terms") ...
...
..."
How have others handled this? Since terms can change, how would one program this to be easily updated?
A code example would be really appreciated.
There is no one right way to ever do anything. What I have done in the past is to have the client or copywriter supply a word document. A word document is great for some one maintaining the content because it is easy for a non programmer. It also allows someone to easily specify some simple formatting for their terms of service content. Ie. bullet list, headers, links etc.
Then I would use an online doc2html converter to create an HTML file. (There are plenty of these online, a quick google can find one) Then I display the HTML file in a web view with in the terms of service or privacy policy view controller. If the app is using a specific font I just use a text editor to string replace the font declarations. Any extra styling requirements can be specified in a separate css file. For example, one that I just did required a specific colour for links to fit the theme of the app.
When it needs updating I can usually just do the edits in HTML if their are a few or regenerate it from a new Doc file.
Here is some example code of the view controller:
class TermsViewController: UIViewController {
#IBOutlet weak var termsWebView: UIWebView!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
loadTermsHTML()
}
func loadTermsHTML() {
//Load the HTML file from resources
guard let path = NSBundle.mainBundle().
pathForResource("terms", ofType: "html") else {
return
}
let url = NSURL(fileURLWithPath: path)
if let data = NSData(contentsOfURL: url) {
termsWebView.loadHTMLString(NSString(data: data,
encoding: NSUTF8StringEncoding) as! String, baseURL: nil)
}
}
}

D3: Change text in url without making request

Suppose I have a url "http://example.com?name=FOO" on the browser, how do I change it to "http://example.com?name=BAR" without making new request, like selecting and changing title text from "FOO" to "BAR".
d3.select("title").text('BAR');
Is there way or isn't?
Thanks!
Just Javascript, D3.js not required for this. Try this:
window.location.search = "?name=BAR";
if you want to do it without refresh:
window.history.pushState("object or string", "Title", "/new-url");
Refer: http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/

Resources