迅速なバージョン
func addToolBar(textField: UITextView){
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
let bold = UIBarButtonItem(image: #imageLiteral(resourceName: "download 12.01.19.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(boldFunc))
let italic = UIBarButtonItem(image: #imageLiteral(resourceName: "italic-png-image-54776.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(italicFunc))
let underlined = UIBarButtonItem(image: #imageLiteral(resourceName: "underlined_font-512.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(underlineFunc))
let strikeThrough = UIBarButtonItem(image: #imageLiteral(resourceName: "Strikethrough_font_awesome.svg.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(strikeFunc))
let size = UIBarButtonItem(title: "\(fontSize)", style: UIBarButtonItemStyle.done, target: self, action: #selector(changeSize))
let textColor = UIBarButtonItem(image: #imageLiteral(resourceName: "text_color1600.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(changetextColor))
let backgroundColor = UIBarButtonItem(image: #imageLiteral(resourceName: "background color.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(changebackgroundColor))
let textLeft = UIBarButtonItem(image: #imageLiteral(resourceName: "left-27877_960_720.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(alignLeft))
let textRight = UIBarButtonItem(image: #imageLiteral(resourceName: "align_right_filled1600.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(alignRight))
let textCenter = UIBarButtonItem(image: #imageLiteral(resourceName: "center.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(alignCenter))
let pic = UIBarButtonItem(image: #imageLiteral(resourceName: "add.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(appendPic))
let bulletpoint = UIBarButtonItem(image: #imageLiteral(resourceName: "bulletpoint.png"), style: UIBarButtonItemStyle.done, target: self, action: #selector(makeBulletpoints))
toolBar.setItems([bold, italic, underlined, strikeThrough, size, textColor, backgroundColor, textLeft, textRight, textCenter, pic, bulletpoint], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
toolBar.frame = CGRect(x: 0, y: 0, width: 33 * 12, height: toolBar.frame.size.height)
textField.delegate = self
//////////try to add a scroll view
let scrollView = UIScrollView()
scrollView.frame = toolBar.frame;
scrollView.bounds = toolBar.bounds;
scrollView.autoresizingMask = toolBar.autoresizingMask;
scrollView.showsVerticalScrollIndicator = false;
scrollView.showsHorizontalScrollIndicator = false;
scrollView.contentSize = toolBar.frame.size;
scrollView.addSubview(toolBar)
textField.inputAccessoryView = scrollView
}
幅を計算するとき
toolBar.frame = CGRect(x: 0, y: 0, width: 33 * 12, height: toolBar.frame.size.height)
幅は、使用するボタンのサイズによって異なります。私は12個のボタンを持っていて、ボタンのすべての画像は25 x 25で、テキスト付きのボタンが1つあります。最初に25x12と書きましたが、収まりませんでした。自動的にマージンが追加されると思います。ボタンのサイズの計算方法についてはあまり詳しく説明しなかったので、次の数が見つかるまで実験しました。私のためにうまくいった。