Swift social app text input optimization "hodgepodge"

Swift social app text input optimization "hodgepodge"

1. Input-related optimization issues

In most applications, there is a need for input. Faced with many users, their ideas are different, and the text content they input is also strange. Faced with different inputs, how can we optimize the input experience? Here we summarize the input-related issues, mainly as follows:

 1、输入控件UITextField跟随键盘移动2、过滤输入内容3、响应编程的处理,去除体验不好的对话框、HUD提示4、中文输入

2. The input box moves with the keyboard

There are two ways to build the interface, code or storyboard/xib. These two methods are the same in handling keyboard movement. Here we recommend using a packaged third-party framework: TPKeyboardAvoiding

1. Code processing method

rootView uses TPKeyboardAvoidingScrollView in the TPKeyboardAvoiding framework to initialize. For example, the login interface, LoginViewController (inherited from UIViewController), the processing method is as follows:

  1. let rootView = TPKeyboardAvoidingScrollView(frame: self.view.bounds);
  2. //...  
  3. //add all subviews to rootView  
  4. //...  
  5. self.view.addSubview(rootView)

The code constructs the interface and realizes that the input box moves with the keyboard. The class TPKeyboardAvoidingScrollView needs to be processed as the root view.

2. Storyboard/xib processing method

Storyboard/xib is easier to handle, just set the rootView of the view controller to TPKeyboardAvoidingScrollView

(1) Select the root view of the controller

(2) Setting the default instantiation class

#p#

3. Commonly used basic settings

1. Common basic settings

Including opening the keyboard, closing the keyboard, specifying the keyboard input type, and specifying the type of the return button, such as the following code

  1. //Open the keyboard  
  2. self.inputText.becomeFirstResponder()
  3. //Close the keyboard  
  4. self.inputText.resignFirstResponder()
  5. //Specify the keyboard input type  
  6. self.inputText.keyboardType = UIKeyboardType.NumberPad
  7. //Specify the type of the return button  
  8. self.inputText.returnKeyType = UIReturnKeyType.Go

2. Filter input through a proxy

Through the UITextField/UITextView proxy, you can control the input more accurately, for example: filter specified characters, prohibit input when the number of characters exceeds the limit, etc.

(1) The UITextField code is as follows:

  1. //Set the proxy. You can set the proxy according to the actual situation. Here, self is used to specify  
  2. self.textField.delegate = self
  3.  
  4. //Proxy method implementation  
  5. func textField(textField: UITextField, shouldChangeCharactersInRange
  6. range: NSRange, replacementString string: String) -> Bool
  7. {
  8.          //No spaces allowed  
  9.          if (string == " " ) {
  10.              return   false  
  11. }
  12.  
  13.          //Cancel the keyboard after pressing enter  
  14.          if (string == "\n" ) {
  15. textField.resignFirstResponder()
  16.              return   false  
  17. }
  18.  
  19.          return   true  
  20. }

(2) The UITextView code is as follows:

  1. /Set the proxy. You can set the proxy according to the actual situation. Here, self is used to specify
  2. self.textView.delegate = self
  3.  
  4. //Proxy method implementation  
  5. func textView(textView: UITextView, shouldChangeTextInRange range: NSRange,
  6. replacementText text: String) -> Bool
  7. {
  8.          //No spaces allowed  
  9.          if (text == " " ) {
  10.              return   false  
  11. }
  12.  
  13.          //Cancel the keyboard after pressing enter  
  14.          if (text == "\n" ) {
  15. textView.resignFirstResponder()
  16.              return   false  
  17. }
  18.  
  19.          return   true  
  20. }

UITextField/UITextView can detect the user input in real time through the proxy method, which is convenient for input constraints. For example, when the input exceeds 10 characters, the user is prohibited from inputting. However, this experience is not good and it is recommended not to use it.

#p#

4. Response programming processing, accurate prompt information

1. How to optimize

The constraints of input information are generally to directly prompt the rules to the user, for example: the input of user nicknames in social networking:

请输入1-8位的字符作为昵称,不能包括空格、回车、标点

After the user clicks the OK button, the validity of the input is checked and the user is prompted with information in the form of a dialog box (or HUD)

The above processing method is very common and can meet basic needs. However, we no longer use the above design for the following two reasons:

 1.提示信息过多,大部分用户不会看2.对话框及HUD提示比较突兀,容易使用户产生挫败感

In the actual development process, the prompt information is reduced to

请输入1-8个字符

When the user actively enters characters such as spaces, carriage returns, punctuation marks, or exceeds the length, the system will automatically prompt the user for information. As shown in the following figure, there is no input, the OK button is disabled, and only very little useful information is prompted.

The input is legal, the confirmation button is enabled

If the input is illegal, the error is highlighted and the OK button is disabled.

2. Code implementation

Using the third-party framework ReactiveCocoa, first implement the function of the prompt below and the picture on the right when the user inputs (do not use the third-party framework, you can implement it yourself through the proxy)

  1. @IBOutlet weak var nickTextField: UITextField! //text input box  
  2. @IBOutlet weak var checkResultShowImageView: UIImageView! //Image on the right side of the input box  
  3. @IBOutlet weak var button: UIButton!
  4. @IBOutlet weak var hintLabel: UILabel! //Hint text below the text box  
  5.  
  6. override func viewDidLoad() {
  7.        super .viewDidLoad()
  8.        //Configure input  
  9. configInput()
  10. }
  11.  
  12. unc configInput() {
  13. self.nickTextField.rac_textSignal().subscribeNext { (text) -> Void in  
  14.            if (text == nil || text.length == 0) {
  15. self.checkResultShowImageView.hidden = false  
  16.                return  
  17. }
  18.  
  19. self.checkResultShowImageView.hidden = true  
  20.            var imageName = ""  
  21.            if (self.checkInputValidate()) {
  22. imageName = "ok.png"  
  23. self.hintLabel.text = ""  
  24. } else {
  25. imageName = "warning.png"  
  26. self.hintLabel.text = "Exceeds \(text.length - 8) characters"  
  27. }
  28. self.checkResultShowImageView.image = UIImage(named: imageName)
  29.  
  30. }
  31. }
  32.  
  33. func checkInputValidate() -> Bool {
  34.        //Input condition check, here is an example, only check the character length  
  35. let length = (self.nickTextField.text as NSString).length
  36.        return length > 0 && length <= 8
  37. }

The following functions are implemented: According to the legality of the input, set the enabled property of the button. This step requires downloading the RAC syntax support file. More detailed introduction to Swift support for ReactiveCocoa

  1. func configButtonEnable() {
  2. RAC(self.button, "enabled" ) <~ RACSignal.combineLatest(
  3. [self.nickTextField.rac_textSignal()],
  4. reduce: { () -> AnyObject! in  
  5.  
  6.              return self.checkInputValidate()
  7.  
  8. })
  9. }

#p#

5. Chinese processing method

When Chinese characters are input, the above word count check is inaccurate. For example, when 6 characters of "I love Chinese culture" are input through the input method, the number of characters in self.nickTextField.text is 23, and the prompt information is incorrect.

UITextView/UITextFiled has a markedTextRange property, which is used to identify whether there is currently selected text (when there is selected text, it is the unfinished input state in the above figure). This principle is used to solve similar problems such as Chinese

  1. @IBOutlet weak var nickTextField: UITextField!
  2. @IBOutlet weak var checkResultShowImageView: UIImageView!
  3. @IBOutlet weak var button: UIButton!
  4. @IBOutlet weak var hintLabel: UILabel!
  5.  
  6. var chineseText: NSString!
  7.  
  8. override func viewDidLoad() {
  9.      super .viewDidLoad()
  10. self.nickTextField.delegate = self
  11. filterInput()
  12. configButtonEnable()
  13.  
  14.  
  15. }
  16.  
  17. func filterInput() {
  18. self.nickTextField.rac_textSignal().subscribeNext { (text) -> Void in  
  19.          if (self.nickTextField.markedTextRange != nil) {
  20.              return ;
  21. }
  22.          //You can add operations such as removing spaces and punctuation here  
  23. self.chineseText = text as NSString
  24.  
  25.          if (text == nil || text.length == 0) {
  26. self.checkResultShowImageView.hidden = false  
  27.              return  
  28. }
  29.  
  30. self.checkResultShowImageView.hidden = true  
  31.          var imageName = ""  
  32.          if (self.checkInputValidate()) {
  33. imageName = "ok.png"  
  34. self.hintLabel.text = ""  
  35. } else {
  36. imageName = "warning.png"  
  37. self.hintLabel.text = "Exceeds \(text.length - 8) characters"  
  38. }
  39. self.checkResultShowImageView.image = UIImage(named: imageName)
  40.  
  41. }
  42. }
  43.  
  44. func checkInputValidate() -> Bool {
  45.      //Input condition check, here is an example, only check the character length  
  46. let length = chineseText.length
  47.      return length > 0 && length <= 8
  48. }
  49.  
  50. func configButtonEnable() {
  51. RAC(self.button, "enabled" ) <~ RACSignal.combineLatest(
  52. [self.nickTextField.rac_textSignal()],
  53. reduce: { () -> AnyObject! in  
  54.  
  55.          if (self.nickTextField.markedTextRange == nil) {
  56.              return self.checkInputValidate()
  57. }
  58.          return self.button.enabled
  59.  
  60. })
  61. }
  62.  
  63.  
  64. @IBAction func buttonPressed(sender: AnyObject) {
  65. println( "------>\(self.chineseText)" )
  66. }

VI. Conclusion

Input is the most time-consuming operation in mobile apps. If it is not handled properly, it is easy to lose users. Here are some points to summarize:

 1.不要将所有的约束信息直接展示给用户,只展示那些对大部分用户都有用的信息,对于其他约束在用户输入错误的时候再提示2.尽量少用或者不用对话框及HUD的方式提示错误3.提示信息准确,例如超出字符数,一种提示为:超出***140字符另一种为:超出n个字符,显然后者提示对用户更有价值4.不要擅自更改用户输入内容或者粗暴禁止用户输入

<<:  On Chance and Skill in Game Design

>>:  Is Android controlled by Google not a good Android?

Recommend

Xiaohongshu store operation, delivery and promotion

At the beginning of 2022, Xiaohongshu's commu...

How to attract user traffic and trigger social communication?

Today’s topic is how to ignite social communicati...

It's really "bald"! Microplastics may cause hair loss?

It's quite "bald" Some friends lame...

Are women's periods contagious? The truth may be different from what you think!

There are usually two reasons why menstruation ap...

21 user analysis models you must know for operations and promotion!

1. Redefine growth 01. Traditional Funnel Model T...

Why do humans love to use electricity?

01 Did you know that the third new aircraft carri...

As an operator, do you know which copywriting cannot be used casually?

Everyone knows the banned words in the new advert...

5 Examples of User Growth for SaaS Products

This compiled article mainly introduces the low-c...