【问题标题】:Remove special characters in NSMutableAttributedString删除 NSMutableAttributedString 中的特殊字符
【发布时间】:2012-11-20 13:10:00
【问题描述】:

在我的应用程序中显示CATextLayer 中的文本(更改字符颜色),使用NSMutableAttributedString 更改颜色,我想删除NSMutableAttributedString 中的特殊字符以显示弹出视图,但不知道如何删除特殊字符,帮助解决问题

我想要这种类型的o/p

"code" to code  //in NSMutableAttributedString, not in NSString

【问题讨论】:

    标签: iphone ios nsattributedstring catextlayer


    【解决方案1】:

    要删除这些字符,您只需编写如下内容:

    NSMutableString* mutableString = ...;
    [mutableString replaceOccurrencesOfString:@"\"" withString:@"" options:0 range:NSMakeRange(0, mutableString.length)];
    

    注意符号“写成\” - 这称为转义序列。 以下是 C 语言中此类特殊字符的列表 - http://msdn.microsoft.com/en-us/library/h21280bw(v=vs.80).aspx

    【讨论】:

    • iam 解释说,我想要 NSMutableAttributedString 而不是 NSMutableString,NSMutableString 没有设置为 CATextLayer
    • 首先使用 NSMutableString,删除不必要的字符,然后使用修改后的 NSMutableString 创建 NSMutableAttributedString。
    【解决方案2】:

    试试这个...这可能对你有帮助。

    NSMutableString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"] invertedSet];
    NSMutableString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
    NSLog (@"Result: %@", resultString);
    

    【讨论】:

    • 如何在CATextLayer中使用NSMutableString,你使用的是NSMutableAttributedString
    【解决方案3】:

    你可以试试这个可能适合你的代码:

     NSMutableString *mutableStrng = [NSMutableString stringWithCapacity:1000];
    [mutableStrng setString:@"my name is i#pho#ne"];
    
    [mutableStrng replaceOccurrencesOfString:@"#" withString:@"" options:0 range:NSMakeRange(0,
     mutableStrng.length)];
    NSMutableAttributedString *mutableAtrString = [[NSMutableAttributedString
     alloc]initWithString:mutableStrng];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      相关资源
      最近更新 更多