Search code examples
iosswiftnsmanagedobject

Reading \n in UILabel


I am assigning value to UILabel. If I get value from NSManagedObject than \n shows as it is and doesn't read it as new line. Here is my code

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

I want UiLabel to read \n as new line

However below code gives expected output

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

Solution

  • I think you title contains escaped character i.e the reason \n is printing try once replacing \\n with \n.

    Swift 3

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

    Swifr 2.3 or lower

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