【问题标题】:iPhone:How to split NSString in multiple lines and its set into UILabel? [duplicate]iPhone:如何将 NSString 拆分为多行并将其设置为 UILabel? [复制]
【发布时间】:2011-08-17 11:55:06
【问题描述】:

可能重复:
Split up NSString using a comma

我有一个类似Hello,How are you,Norman,Stanley,Fletcher 的 NSString,所以我想在出现逗号分隔符时拆分该字符串,并将该字符串设置为 iPhone 中的 UILabel。

我该怎么做?

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    原字符串:

    NSString originalString = @"Hello,How are you,Norman,Stanley,Fletcher";
    

    分成多行:

    NSString multiLineString = [originalString stringByReplacingOccurrencesOfString:@"," withString:"@\n"];
    

    分配给您的标签:

    label.text = mutliLineString;
    

    需要确保标签占用多行:

    label.numberOfLines = 0;
    

    将使其显示所需的行数。

    【讨论】:

    • 谢谢,但我想设置分隔的字符串多行 uilabel
    • 多个单独的 UILabel,还是一个 UILabel 包含多行文本?
    • 谢谢,但我将拆分的字符串显示到 uilabel 中动态更改标签高度
    • 这个答案有帮助吗? stackoverflow.com/questions/990221/…
    【解决方案2】:

    您可以使用componentsSeparatedByString: 方法来分割字符串。此方法将返回一个 NSStrings 数组。 您可以将它们显示在 UITextView 中,并将 editable 属性设置为 no。

    【讨论】:

      【解决方案3】:

      你可以像这样使用:

      NSString * str  = @"Hello,How are you,Norman,Stanley,Fletcher";
      
      yourLabel.text = [str stringByReplacingOccurrencesOfString:@"," withString:@"\n"]);
      

      【讨论】:

      • 谢谢,但我想设置分隔的字符串多行 uilabel
      【解决方案4】:

      要拆分你的@“你好,你好吗”,请使用以下代码:

      NSString *string = @"Hello,How are you";
      NSArray *array = [string componentsSeparatedByString: @","];
      

      要检查字符串中是否有逗号,请使用:

      NSRange matchNotFound;
      matchNotFound = [[label text] rangeOfString: @","];
      if ((matchNotFound.location != NSNotFound) {
      //if there is a comma 
      } else {
      //if there is no comma 
      }
      

      Hopping 觉得这很有用。

      【讨论】:

        猜你喜欢
        • 2014-05-18
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2019-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多