ios - 我将如何映射/保存这些数据?
问题描述
我正在创建一个具有时间戳功能的应用程序,该功能在UITextView
.
我已经努力创建单个时间戳,但我不确定如何保存多个时间戳。基本上它们是CGRect
需要根据命令更新的按钮对象。我能想到的2个选项是:
1.保存时间戳按钮+字符计数ID
- 这可能会使
CGrect
打开时重绘 all 变得很困难,因为我必须根据字符计数 ID 进行计算,如果段落移动,我将不得不更新每个对象......(只是担心很多段落需要多长时间这需要)。
2.保存时间戳按钮+CGRect
- 我可以使用
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
}
}
解决方案
推荐阅读
- maptitude - 我可以使用什么方法(Maptitude 是我的软件)来查找区域内每条道路的总里程?
- amazon-web-services - SQS fifo 触发器调用 Lambda 函数(1 条消息 - 1 次调用)
- typescript - 动作创建者文档评论未显示
- node.js - 迁移到节点 v14 后 express.js 出现语法错误,将文件更改为 mjs 类型并导入语句
- c - 我在 Arduino 网络服务器中显示浮点变量时遇到问题
- r - 在 MRO 中安装基于 Fortran 的 R 包会在链接行中显示版权信息
- c# - 无法将构建结果发送到 SonarQube 服务器
- vue.js - Vuetify 和 vue.js,改变实际按钮图标大小的最佳方法是什么?
- sql - 根据现在将更改的标准查找回头客
- android - 几秒钟后,我使用 Future.delayed() 切换到新屏幕,但它一遍又一遍地跳转到该页面