2016-01-28 229 views
3

我正在使用swift處理我的第一個iOS應用程序。我需要用Newline(\ n)在UILabel中顯示一些細節。它的工作很好,當我直接給像下面換行符( n)不能在swift中工作

myLabel.text = "\nSome \n\n Text here\n" 

因爲我需要存儲的文字,我保存它作爲核心數據和檢索爲String。

let context: NSManagedObjectContext = appDel.managedObjectContext 


    let fetchRequest = NSFetchRequest() 

    let entityDescription = NSEntityDescription.entityForName("Details", inManagedObjectContext: context) 

    fetchRequest.entity = entityDescription 

    do { 
     let result = try context.executeFetchRequest(fetchRequest) 
     print(result.count) 
     if (result.count > 0) { 
      for i in 0..<result.count 
      { 
       let person = result[i] as! NSManagedObject 

       fee = person.valueForKey("fee") as! String 
       content = person.valueForKey("content") as! String 
       pattern = person.valueForKey("pattern") as! String 
       documentation = person.valueForKey("documentation") as! String 

      } 
     } 

    } catch { 
     let fetchError = error as NSError 
     print(fetchError) 
    } 

但是,當我將費用設置爲標籤文本時,Newline不起作用。

myLabel.text = fee 

我該怎麼做才能做到這一點?
在此先感謝。

+0

myLabel.text =費用myLabel.text =「\\(費用)」。我認爲它應該工作..試試這個.. – Bhoomi

+0

我試過了。但仍然沒有改善。 –

+0

同樣的事情發生在我身上。它只是在普通文本中打印控制檯中的\ n。 –

回答

6

當您使用coredata或sqlite在本地數據庫中保存\n的文本時。 它會自動另外設置另一個反斜槓。 因此,當你讀取字符串並嘗試打印時,只顯示你保存的方式。 只需使用此線在標籤中顯示多行文本。

夫特2.0

let fee = person.valueForKey("fee") as? String 
    let newText = fee?.stringByReplacingOccurrencesOfString("\\n", withString: "\n") 
    myLabel.text = newText 

夫特3.0

guard let fee = person.valueForKey("fee") as? String else {return} 
    let newText = fee.replacingOccurrences(of: "\\n", with: "\n") 
    myLabel.text = newText 

不要忘記允許的UILabel對象通過設定線= 0

的數目,以顯示無限線