JCGroupNameViewController.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // JCGroupNameViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/5/16.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. class JCGroupNameViewController: UIViewController {
  11. var group: JMSGGroup!
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. _init()
  15. groupName = group.displayName()
  16. groupNameTextField.text = groupName
  17. var count = 20 - groupName.count
  18. count = count < 0 ? 0 : count
  19. tipLabel.text = "\(count)"
  20. groupNameTextField.becomeFirstResponder()
  21. }
  22. private var topOffset: CGFloat {
  23. if isIPhoneX {
  24. return 88
  25. }
  26. return 64
  27. }
  28. private lazy var navRightButton: UIBarButtonItem = UIBarButtonItem(title: "完成", style: .plain, target: self, action: #selector(_saveNickname))
  29. fileprivate lazy var groupNameTextField: UITextField = UITextField(frame: CGRect(x: 0, y: self.topOffset, width: self.view.width, height: 45))
  30. fileprivate lazy var tipLabel: UILabel = UILabel(frame: CGRect(x: self.view.width - 15 - 50, y: self.topOffset + 21, width: 28, height: 12))
  31. private var groupName = ""
  32. //MARK: - private func
  33. private func _init() {
  34. self.title = "群组名称"
  35. automaticallyAdjustsScrollViewInsets = false
  36. view.backgroundColor = UIColor(netHex: 0xe8edf3)
  37. groupNameTextField.backgroundColor = .white
  38. groupNameTextField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 0))
  39. groupNameTextField.rightView = UIView(frame: CGRect(x: 0, y: 0, width: 27, height: 0))
  40. groupNameTextField.leftViewMode = .always
  41. groupNameTextField.rightViewMode = .always
  42. groupNameTextField.addTarget(self, action: #selector(textFieldDidChanged(_ :)), for: .editingChanged)
  43. view.addSubview(groupNameTextField)
  44. tipLabel.textColor = UIColor(netHex: 0x999999)
  45. tipLabel.font = UIFont.systemFont(ofSize: 12)
  46. tipLabel.textAlignment = .right
  47. view.addSubview(tipLabel)
  48. _setupNavigation()
  49. }
  50. private func _setupNavigation() {
  51. navigationItem.rightBarButtonItem = navRightButton
  52. }
  53. @objc func textFieldDidChanged(_ textField: UITextField) {
  54. if textField.markedTextRange == nil {
  55. let text = textField.text!
  56. if text.count > 20 {
  57. let range = (text.startIndex ..< text.index(text.startIndex, offsetBy: 20))
  58. let subText = text.substring(with: range)
  59. textField.text = subText
  60. }
  61. let count = 20 - (textField.text?.count)!
  62. tipLabel.text = "\(count)"
  63. }
  64. }
  65. //MARK: - click func
  66. @objc func _saveNickname() {
  67. groupNameTextField.resignFirstResponder()
  68. let groupName = groupNameTextField.text
  69. MBProgressHUD_JChat.showMessage(message: "修改中...", toView: view)
  70. var desc = group.desc
  71. if group.desc != nil && group.desc!.isEmpty {
  72. desc = nil
  73. }
  74. JMSGGroup.updateGroupInfo(withGroupId: group.gid, name: groupName!, desc: desc) { (result, error) in
  75. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  76. if error == nil {
  77. NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateGroupInfo), object: nil)
  78. self.navigationController?.popViewController(animated: true)
  79. } else {
  80. MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.view)
  81. }
  82. }
  83. }
  84. }