I need to set different font to a label based on whether I'm displaying simple English string or characters from other languages that are not based on the latin characterset. So I just want to know whether if the entire string is all latin characters? How can I do that in Swift? I have read this question, but I don't think I can apply that answer because there is no way I can specify all the latin character including the mark and punctuation, one by one, to be excluded in the detection.
Please help. Thanks.
Similarly as in How can I check if a string contains Chinese in Swift?, you can use a regular expression to check if there is no character not in the "Latin" character class:
extension String {
var latinCharactersOnly: Bool {
return self.range(of: "\\P{Latin}", options: .regularExpression) == nil
}
}
\P{Latin}
(with capital "P") is the pattern matching any character not having the "Latin" Unicode character property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With