带有复选框的UITextView

时间:2018-07-02 10:24:54

标签: ios swift

我想使用UIText View创建一个检查列表应用程序,因此需要在选中复选框时调用侦听器。另外,我需要将那些复选框与用户给出的文本左对齐。有什么办法可以在Swift中完成?

1 个答案:

答案 0 :(得分:2)

检查以下代码:

    textView.layer.borderColor = UIColor.gray.cgColor
    textView.layer.borderWidth = 1.0

    textView.textContainerInset = UIEdgeInsets(top: 8, left: 8, bottom: 0, right: 8)

    // Create Button Programmatically
    let button = UIButton(frame: CGRect(x: 8, y: 8, width: 18, height: 18))
    button.setTitleColor(UIColor.red, for: .normal)
    button.setImage(#imageLiteral(resourceName: "check"), for: .normal)
    button.setImage(#imageLiteral(resourceName: "uncheck")  , for: .selected)
    button.addTarget(self, action: #selector(buttonCheckUncheckTapped), for: .touchUpInside)
    button.clipsToBounds = true
    button.layer.cornerRadius = 9

    // Create Exclusion Path to avoid text overlapping.
    let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: button.frame.width, height: button.frame.height))
    textView.textContainer.exclusionPaths = [path]
    textView.addSubview(button)

enter image description here