ios - 如何在特定行上查找和替换 UILabel 的值?

标签 ios swift swift3 uilabel

我知道我可以使用 .numberOfLines 找到 UILabel 的总行数,但是如何检索和编辑第 2 行的值。

例子:
Get/edit this value

最佳答案

假设您的屏幕截图中的每一行都由换行符分隔,您可以基于此拆分文本。

这是 Swift 3 中的示例:

if let components = label.text?.components(separatedBy: "\n"), components.count > 1 {
    let secondLine = components[2]
    let editedSecondLine = secondLine + "edited"
    label.text = label.text?.replacingOccurrences(of: secondLine, with: editedSecondLine)
}

您应该确保在您感兴趣的任何索引处都有一个值。此示例确保在检索值之前存在多个组件。

然后您可以用您编辑的行替换第二行。

希望对您有所帮助。

关于ios - 如何在特定行上查找和替换 UILabel 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41903591/

相关文章:

ios - resignFirstResponder 不在模态视图 Controller 中工作

ios - 使用 twitter API 时出现 401 未经授权的错误

swift - iOS - 在导航栏的标题中添加图像和文本

ios - 发送一个枚举,它是字典上的元组并返回

ios - 将第二个 UIWindow 与 rootViewController 一起使用时,界面旋转变黑

iOS Swift 从网络流播放音频 (aac)

ios - Swift 中的套接字(不是 webSocket)

iphone - MKMapViewDelegate + JSON Alamofire

ios - '子字符串(来自 : )' is deprecated: Please use String slicing subscript with a ' partial range from' operator. Swift 4 错误

html - SWIFT 3 - 从从 JSON 网址获取的字符串中取出 html 标签