How to change the alignment of a UISearchBar according to the input language
Asked Answered
R

3

4

I would like to be able to input text in either Left-to-Right and Right-to-Left languages into a UISearchBar. This means that once a user has started inputing text in right to left languages, I would like the text alignment to be to the right and vice versa.

The question is - Is there any way to catch these events of language switching? Thanks for your help.

Rhetoric answered 12/2, 2012 at 18:49 Comment(0)
S
1

Yes there is. You want to subscribe to UITextInputCurrentInputModeDidChangeNotification. See the UITextInputMode Class Reference.

Sclar answered 12/2, 2012 at 20:22 Comment(1)
Updated link for UITextInputMode Class - developer.apple.com/documentation/uikit/uitextinputmodeAlpert
T
2

Use this code in ViewDidLoad

    searchbar.semanticContentAttribute = .forceRightToLeft
Tetragonal answered 31/1, 2018 at 5:7 Comment(0)
S
1

Yes there is. You want to subscribe to UITextInputCurrentInputModeDidChangeNotification. See the UITextInputMode Class Reference.

Sclar answered 12/2, 2012 at 20:22 Comment(1)
Updated link for UITextInputMode Class - developer.apple.com/documentation/uikit/uitextinputmodeAlpert
G
0
func customizeSearchBar() {
            //For placeHolder to allign Right
            searchBar.semanticContentAttribute = .forceRightToLeft
            // for text field to align right
             if let textfield = searchBarText.value(forKey: "searchField") as? 
            UITextField {
                 textfield.backgroundColor = UIColor.clear
                 textfield.textAlignment = .right

                 if let leftView = textfield.leftView as? UIImageView {
                     leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)
                     leftView.tintColor = UIColor.clear
                 }
             }
        }
Greenwald answered 23/3, 2020 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.