在键盘上的工具栏中间居中 UILabel

Posted

技术标签:

【中文标题】在键盘上的工具栏中间居中 UILabel【英文标题】:Center UILabel in the Middle of the ToolBar on a Keyboard 【发布时间】:2022-01-02 06:45:40 【问题描述】:

如何将标签放在键盘工具栏的中间?我可以在键盘中间找到标签,但它没有正确居中

lazy var textView: UITextView = 
    // ...
()

lazy var dummyToolBarButton: UIButton = 
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setImage(UIImage(named: "cogIcon"), for: .normal)
    button.alpha = 0
    button.sizeToFit()
    return button
()

lazy var timerLabel: UILabel = 
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "00:00"
    label.font = UIFont.monospacedDigitSystemFont(ofSize: 15.5, weight: .regular)
    label.backgroundColor = .clear
    label.textColor = .gray
    label.textAlignment = .center
    label.sizeToFit()
    return label
()

override func viewDidLoad() 
    super.viewDidLoad()

    setToolBarOnKeyboard()


func setToolBarOnKeyboard() 
    
    let toolBar = UIToolbar()
    toolBar.sizeToFit()
    
    let invisibleDummyButton = UIBarButtonItem(customView: dummyToolBarButton) // alpha is set to 0
    
    let flexibleSpaceLeft = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    
    let labelItem = UIBarButtonItem(customView: timerLabel)
    
    let flexibleSpaceRight = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissKeyboard))
    
    toolBar.setItems([invisibleDummyButton, flexibleSpaceLeft, labelItem, flexibleSpaceRight, doneButton], animated: false)
    
    textView.inputAccessoryView = toolBar


@objc func dismissKeyboard()  

【问题讨论】:

【参考方案1】:

我所要做的就是在工具栏左侧设置一个按钮以使用UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: nil),并将其前景色设置为清除invisibleDummyButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.clear], for: .normal)

func setToolBarOnKeyboard() 
    
    let toolBar = UIToolbar()
    toolBar.sizeToFit()
    
    let invisibleDoneButtonLeft = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: nil)
    invisibleDoneButtonLeft.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.clear], for: .normal)

    let flexibleSpaceLeft = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    
    let middelLabelItem = UIBarButtonItem(customView: timerLabel)
    
    let flexibleSpaceRight = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    
    let doneButtonRight = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissKeyboard))
    
    toolBar.setItems([invisibleDoneButtonLeft, flexibleSpaceLeft, middleLabelItem, flexibleSpaceRight, doneButtonRight], animated: false)
    
    textView.inputAccessoryView = toolBar

这是正确居中的标签图片

这是我设置之前工具栏的图片我将左侧的按钮设置为.clear

【讨论】:

以上是关于在键盘上的工具栏中间居中 UILabel的主要内容,如果未能解决你的问题,请参考以下文章

根据实际可见字母高度在 UILabel 中垂直居中文本

表格怎么让文字居中?

如何在 UIWebView 上的键盘上方显示工具栏 [关闭]

staruml怎么没有找到setting

让UILabel的文字顶部对齐

如何使表单域和工具栏按钮居中?