在 UILabel 中读取 \n

Reading \n in UILabel

我正在为 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")