我将如何映射/保存此数据?

时间:2019-06-18 08:09:53

标签: ios swift core-data uitextview

我正在创建一个带有时间戳功能的应用程序,该功能在UITextView中的段落旁边显示时间。

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

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

  • 这可能使重新打开所有CGrect的难度变得很困难,就像我想要的那样 必须根据字符数ID以及是否有段落来计算 我必须更新每个对象的动作...(只是担心 很多段落需要多长时间)。

2。保存时间戳记按钮+ CGRect

  • 我可以使用CGRect绘制每个对象,而无需重新计算 打开
  • 在编辑文本时,我只需要一种方法来检查它是否是新段落,以及是否创建了时间戳(如果不是) 离开它。

编辑:我刚刚意识到这也行不通,因为当我更新视图时,由于每次函数调用都将替换数组,因此仍需要某种方式将数据保存在按钮中。

我觉得第二个选项是错误的,因为它使用对象来制作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
}

}

0 个答案:

没有答案