ios - 在 UILabel 中读取\n

标签 ios swift nsmanagedobject

我正在为 UILabel 赋值。如果我从 NSManagedObject 中获取值,则\n 会按原样显示并且不会将其作为新行读取。 这是我的代码

let cards:Card = cardArray[0] as! Card
lblTitle?.text = cards.title 
//Output in UiLabel is "This is \n title"

我希望 UiLabel 将\n 读取为新行

但是下面的代码给出了预期的输出

lblTitle?.text = "This is \n title"

最佳答案

我认为您 title 包含转义字符,即 \n 正在打印的原因 尝试将 \\n 替换为 \n

swift 3

lblTitle?.text = cards.title.replacingOccurrences(of: "\\n", with: "\n")

Swifr 2.3 或更低版本

lblTitle?.text = cards.title.stringByReplacingOccurrencesOfString("\\n", withString: "\n")

关于ios - 在 UILabel 中读取\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858442/

相关文章:

ios - 将照片库中的图像导入 xcode 中的应用程序

ios - SKPaymentQueue.canMakePayments() 总是返回 true

ios - CloudKit 推送通知订阅不起作用

ios - iPhoneX 的 UIScrollView 内容插图 (safeAreaLayoutGuide)

ios - 当上下文初始化为内联时,为什么 NSManagedObject 不会插入到子 NSManagedObjectContext 中?

ios - 添加 NSManagedObject 子类时出现重复符号错误,链接重复

ios - Xcode 6.3.2 转换为最新的 Swift 语法挂起

ios - 使用相同的重新排序对多个 NSArray 对象进行排序

swift - UIImage 大于约束尺寸

core-data - 如何在获取之前更改托管对象类名称