【问题标题】:iOS UITextView ignoring newline char from JSON ResponseiOS UITextView 忽略 JSON 响应中的换行符
【发布时间】:2012-10-13 06:59:59
【问题描述】:

我需要在我的 iOS 应用程序中的多行 UITextView 中显示来自 JSON Webservice 的一些数据,但是当我在 textview 上使用字符串时,新行不起作用。

这是我正在使用的代码:

// Data Value from JSON = testing the data value \r\n Another \n liine maybe?
NSString *responseDataString = [dictRequestorDetails objectForKey:@"data"];
txtWorkflowDetails.text = responseDataString;

当我执行这段代码时,UITextField 会显示

测试数据值 \r\n 可能是另一个 \n 线?

但是,如果我输入类似的字符串

// Data Value from JSON = testing the data value \r\n Another \n liine maybe?
NSString *responseDataString = [dictRequestorDetails objectForKey:@"data"];
txtWorkflowDetails.text = @"testing the data value \r\n Another \n liine maybe?";

换行符然后在 TextView 中起作用

【问题讨论】:

    标签: objective-c ios json uitextview


    【解决方案1】:

    刚刚关注了这个帖子:Adding a line break to a UITextView

    我更新了我的 JSON 提要以输出新的行,例如

    测试数据值\n可能是另一个\nmline?

    然后在我的代码中运行;

    responseDataString = [responseDataString stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    

    【讨论】:

      【解决方案2】:

      斯威夫特 4:

      let stringForUITextView = originJsonString.replacingOccurrences(of: "\\n", with: "\n")
      

      【讨论】:

        【解决方案3】:

        您可以先处理字符串并将所有出现的\n替换为\n

        NSString *newString = [responseDataString stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
        

        【讨论】:

          【解决方案4】:

          txtWorkflowDetails 是否设置为允许换行(例如自动换行)?

          【讨论】:

            猜你喜欢
            • 2011-12-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-06-17
            • 2017-03-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多