首页 > 解决方案 > 我将如何映射/保存这些数据?

问题描述

我正在创建一个具有时间戳功能的应用程序,该功能在UITextView.

我已经努力创建单个时间戳,但我不确定如何保存多个时间戳。基本上它们是CGRect需要根据命令更新的按钮对象。我能想到的2个选项是:

1.保存时间戳按钮+字符计数ID

2.保存时间戳按钮+CGRect

编辑:我刚刚意识到这也行不通,因为当我更新视图时,我仍然需要以某种方式将数据保存在按钮中,因为在每个函数调用中都会替换数组。</p>

我觉得第二个选项是错误的,因为它使用对象来制作 UI,但不确定第一个选项将如何工作!哈哈。谁能帮助我就如何完成此功能提出一些建议?我是不是理解错了哈哈

现在用于创建段落组件和时间戳的代码:

var paragraphMarkers: [UIView] = [UIView]()

@objc func updateParagraphMarkers() -> Void {

// clear previous paragraph marker views
paragraphMarkers.forEach {
    $0.removeFromSuperview()
}

// reset paraMarkers array
paragraphMarkers.removeAll()

// probably not needed, but this will make sure the the text container has updated
textView.layoutManager.ensureLayout(for: textView.textContainer)

// make sure we have some text
guard let str = textView.text else { return }

// get the full range
let textRange = str.startIndex..<str.endIndex

// we want to enumerate by paragraphs
let opts:NSString.EnumerationOptions = .byParagraphs

var i = 0

str.enumerateSubstrings(in: textRange, options: opts) {
    (substring, substringRange, enclosingRange, _) in

    // get the bounding rect for the sub-rects in each paragraph
    if let boundRect = self.textView.boundingFrame(ofTextRange: enclosingRange) {

        // create a UIView
        let paragraphView = UIView()

        // give it a background color from our array of colors
        paragraphView.backgroundColor = .red

        // init the frame
        paragraphView.frame = boundRect

        // position views
        paragraphView.frame.origin.y += self.textView.frame.origin.y
        paragraphView.frame.origin.x = self.timeStampView.frame.origin.x
        paragraphView.frame.size.width = self.timeStampView.frame.width

        // add it to the view
        let timeText = self.calculateFirstLabelFromNSTimeInterval(currentTime: self.audioPlayer.currentTime)
        let currentTimeText  = "\(timeText.minute):\(timeText.second)"

        let timeButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 20))
        timeButton.setTitle(currentTimeText, for: .normal)
        timeButton.setTitleColor(UIColor.white, for: .normal)
        timeButton.titleLabel?.font = UIFont.systemFont(ofSize:10, weight: UIFont.Weight.medium)
        timeButton.backgroundColor = UIColor.gray
        timeButton.autoresizingMask = [.flexibleRightMargin, .flexibleBottomMargin]

        self.timeStampView.addSubview(paragraphView)

        // save a reference to this UIView in our array of markers
        self.paragraphMarkers.append(paragraphView)
    }
}


extension UITextView {

func boundingFrame(ofTextRange range: Range<String.Index>?) -> CGRect? 
   {

    guard let range = range else { return nil }
    let length = range.upperBound.utf16Offset(in: self.text)-range.lowerBound.utf16Offset(in: self.text)
    guard
        let start = position(from: beginningOfDocument, offset: range.lowerBound.utf16Offset(in: self.text)),
        let end = position(from: start, offset: length),
        let txtRange = textRange(from: start, to: end)
        else { return nil }

    // we now have a UITextRange, so get the selection rects for that range
    let rects = selectionRects(for: txtRange)

    // init our return rect
    var returnRect = CGRect.zero

    // for each selection rectangle
    for thisSelRect in rects {

        // if it's the first one, just set the return rect
        if thisSelRect == rects.first {
            returnRect = thisSelRect.rect
        } else {
            // ignore selection rects with a width of Zero
            if thisSelRect.rect.size.width > 0 {
                // we only care about the top (the minimum origin.y) and the
                // sum of the heights
                returnRect.origin.y = min(returnRect.origin.y, thisSelRect.rect.origin.y)
                returnRect.size.height += thisSelRect.rect.size.height
            }
        }

    }
    return returnRect
}

}

标签: iosswiftcore-datauitextview

解决方案


推荐阅读