How do I detect Arabic character in UITextField? - xcode

I am a beginner programmers iPhone.(swfit)
How do I prevent the user does not use Arabic words in the text field?
In other words, how do I detect Arabic character?
thanks

You can regex
let regex = try! NSRegularExpression(pattern: "[^A-Za-z0-9 \\-\\.\\:\\/]", options: [])
let firstMatch = regex.rangeOfFirstMatchInString(yourString, options: [], range: NSMakeRange(0, yourString.characters.count))
if firstMatch.location != NSNotFound {
// Invalid character found
}

Related

Make UITextField with VoiceOver read numbers as digits

Our App has a few UITextField where users enter alphanumeric input codes such as "AA149873A".
We'd like voice over to read these as "A A 1 4 9 8 7 2 A", but it instead reads the digits as a number: "One hundred and forty nine thousand, eight hundred and seventy three".
Is there some way to configure UITextField so that it knows its content shouldn't be thought of as numbers, but individual digits?
Thanks.
We'd like voice over to read these as "A A 1 4 9 8 7 2 A", but it instead reads the digits as a number: "One hundred and forty nine thousand, eight hundred and seventy three".
The fastest wy to reach your goal is to add spaces between each character in the accessibilityValue of the UITextField as follows: 🤓
class AccessibilityTextField: UITextField {
var _str: String?
override var text: String? {
get { return _str}
set {
_str = newValue!
accessibilityValue = _str!.map{String($0)}.joined(separator: " ")
}
}
convenience init() { self.init() }
required init?(coder: NSCoder) { super.init(coder: coder) }
}
I didn't implement all the text field delegate stuff to test it ⟹ I created a blank project only with an UITextField adding a "BB0024FG" plain text and changed the text in the viewDidAppear of my view controller:
myTextField.text = "AA14987A"
In the end, VoiceOver spells out each character after another without reading out the initial plain text. 👍
Following this rationale, you have a way that let VoiceOver know that the UITextField content must be thought as individual digits. 😉
As of iOS 13 it is possible to add a string attribute to direct voice-over vocalisation to spell strings out as individual letters and digits.
I've not found a way to direct UITextField to add this attribute to its content. However, a UITextField subclass can override it's accessibilityValue to achieve this.
The subclass given here adds a property to enable or disable this behaviour.
final class AccessibilityTextField: UITextField {
var isAlphanumeric: Bool = false
override public var accessibilityAttributedValue: NSAttributedString? {
get {
guard let text = text, !text.isEmpty else {
return nil
}
return NSAttributedString(string: text, attributes: valueAttributes)
}
set {
// Ignore these values.
_ = newValue
}
}
private var valueAttributes: [NSAttributedString.Key: Any] {
guard #available(iOS 13.0, *), isAlphanumeric else {
return [:]
}
return [.accessibilitySpeechSpellOut: true]
}
}
An alternative approach is given in another answer here that doesn't use the iOS 13 feature . accessibilitySpeechSpellOut. However, I've seen it suggested that this is not ideal for brail output systems as they also use accessibilityLabel. Perhaps this is a good fallback on pre iOS 13 systems.

How to type subscripts/superscripts in Xcode string literals?

I have seen this code in Xcode editor :
How can I type these strings with different character position and font size ?
With these string literals, it's not a question of character position or font size. These are special unicode superscript/subscript characters. If you go to the macOS keyboard preferences and choose "Show keyboard and emoji viewers in menu bar", you'll have a new option in your menu bar "Show emoji & symbols". You can then, for example, type "2" in the searchbox and you'll then see subscript and superscript rendition in the "related characters" section:
So, this is not a general subscript/subscripting feature, but dedicated unicode characters for just a few common subscripts/superscripts (e.g. "2", "x", etc.).
Note, if you need more fine grained control over fonts, baseline adjustments, etc., many user interface controls support the use of attributed strings, e.g.:
let bigFont = UIFont.systemFont(ofSize: 20)
let smallFont = UIFont.systemFont(ofSize: 12)
let title = NSMutableAttributedString(string: "foo", attributes: [.font: bigFont])
title.append(NSMutableAttributedString(string: "bar", attributes: [.font: smallFont, .baselineOffset: 10]))
button.setAttributedTitle(title, for: .normal)
Yielding:
Or, as described in this answer, you can apparently also do:
let string = "foobar"
let range = NSRange(location: 3, length: 3)
let title = NSMutableAttributedString(string: string)
let superscript = NSAttributedStringKey(rawValue: kCTSuperscriptAttributeName as String)
title.addAttributes([superscript: 1], range: range) // Use 1 for superscript; use -1 for subscript
But your code snippet is clearly just using the predefined unicode superscript/subscript characters. These various programmatic approaches can be useful, though, if you need to render something that doesn't already exist in unicode.

NumberFormatter, NSLocale, Xcode, iOS, Swift 3.0

Problem: A textField with a "number" containing a decimal separator (a comma in my case), how can I change this to a decimal separator that Xcode will understand (.), and the other way around - display a result with the local decimal separator?
Ok...half way there...
let label = Input.text
let formatter = NumberFormatter()
let maybeNumber = formatter.number(from: label!)
if maybeNumber != nil {
Output.text = String(describing: maybeNumber!)
}
I'm not sure if this is what you mean, but you can change the comma in the textField.text to a (.) by using this piece of code:
textField.text = textField.text.replacingOccurrences(of: ",", with: ".", options: NSString.CompareOptions.literal, range: nil)
This finds the commas in your textField and replaces them with (.)

Adding different alignment and font size to different lines in one TextLabel in TableCell swift

so i'm trying to implement a simple english to farsi dictionary in iOS
i'd like to include both words in one table cell, problem is that english is L>R and farsi is R>L, also i'd like to make the farsi word a bit bigger.
I created an AttributedMutableString and I thought I put down all the correct values but it looks like there is a problem since it isn't rendering correctly.
code:
cell.textLabel?.numberOfLines = 0
var myString = "\(englishConvo[indexPath.row])\n\(farsiConvo[indexPath.row])"
var mutableString = NSMutableAttributedString()
var lenOfLang1 = englishConvo[indexPath.row].characters.count
var lenOfLang2 = farsiConvo[indexPath.row].characters.count
let increaseFontSize = UIFont(name: (cell.textLabel?.font.fontName)!, size: (cell.textLabel?.font?.pointSize)! + 5)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Right
mutableString = NSMutableAttributedString(string: myString)
mutableString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: lenOfLang1 + 1, length: lenOfLang2))
mutableString.addAttribute(NSFontAttributeName, value: increaseFontSize!, range: NSRange(location: lenOfLang1 + 1, length: lenOfLang2))
cell.textLabel?.attributedText = mutableString
If i convert to a string using this code this is what I get
cell.textLabel?.text = String(mutableString)
Any thoughts / ideas would be super appreciated
Table cells already come with a layout that gives you two labels (text and detail), so why not just use it? Here, for example, in a language app of mine, I'm displaying a Latin word in the text label and an English translation in the detail label. You could easily do the same with Farsi and English.

UITextField - Limit text lenght not by character count

is there a way to limit a textfield but not by character count? I would like to stop the ability to type when the end of the field is reached. But as different characters have different dimensions stopping at a certain count ist not really a solution. Something like "stop at the end of the line" would be perfect.
this doesn't work for me
Max length UITextField
What you want to do is instead of calculating the maximum number of characters of the new string, calculate it's width in points and compare it to the width you need.
[Swift]
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let combinedString = textField.attributedText!.mutableCopy() as NSMutableAttributedString
combinedString.replaceCharactersInRange(range, withString: string)
return combinedString.size().width > textField.bounds.size.width
}
You might need to edit the attributes to get it to render on a single line.
Subscribe to controlTextDidChange and calculate the size of the text via
let s = NSString(string: <yourtext>)
let size = s.boundingRectWithSize(<#size: NSSize#>, options: <#NSStringDrawingOptions#>, attributes: <#[NSObject : AnyObject]?#>)
Now you can check if the text is too large.

Resources