stringByReplacingOccourencesOfString with NSMutableAttributedString

时间:2015-12-22 16:29:25

标签: ios objective-c nsmutableattributedstring

我从XML文件中得到以下文字:

"Weekly menus from December 21<sup>st</sup> till 28<sup>th</sup>"并希望它们显示为:&#34;每周菜单从12月21日 st 直到28 th &#34;

-Tags是因为XML文件用于html和iOS。现在我想&#34;转移&#34;日期中的st / nd / rd / th并生成以下代码:

NSString *basicString = dieMenueDaten.SubtitelE;

NSDictionary *attribute = @{(NSString*)kCTSuperscriptAttributeName: @1 };
NSMutableAttributedString *subStringTh = [[NSMutableAttributedString alloc] initWithString:@"th" attributes:attribute];
NSMutableAttributedString *newSubtitleE = [[NSMutableAttributedString alloc] initWithString:basicString];
NSMutableAttributedString *replaceTh = [[NSMutableAttributedString alloc] initWithString:@"<sup>th</sup>"];

NSRange laenge = NSMakeRange(0, basicString.length);

newSubtitleE = [[newSubtitleE mutableString] stringByReplacingOccurrencesOfString:replaceTh withString:subStringTh options:NSCaseInsensitiveSearch range:laenge];

这样做我得到了

  

不兼容的指针类型发送&#39; NSMutableAttributedString *&#39;参数类型&#39; NSString * _Nonnull&#39;

  

不兼容的指针类型分配给&#39; NSMutableAttributedString *&#39;来自&#39; NSString *&#39;

我该怎么做才能避免错误消息?

3 个答案:

答案 0 :(得分:0)

来自NSString Class Reference

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
                                    withString:(NSString *)replacement
                                       options:(NSStringCompareOptions)options
                                         range:(NSRange)searchRange

此方法为目标和替换采用2 NSString个参数,而不是两个NSMutableAttributedString参数。

您还可以尝试使用replaceTh.stringsubStringTh.string来获取您提供的每个NSString的{​​{1}}属性。

您的第二个警告是相关的,因为上述方法会返回NSMutableAttributedString,而不是NSString,您已声明NSMutableAttributedString

答案 1 :(得分:0)

好的,由于快速提示,我按照以下方式进行了更改(此处有详细说明):

- (NSMutableAttributedString *)attributedSubTitleString:(NSString *)string;
{
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
UIFont *smallFont = [UIFont boldSystemFontOfSize:9.0f];

[attString beginEditing];
[attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"th"]];
[attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"th" options:NSBackwardsSearch]];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"th"]];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"th" options:NSBackwardsSearch]];

[attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"st"]];
[attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"st" options:NSBackwardsSearch]];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"st"]];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"st" options:NSBackwardsSearch]];

[attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"nd"]];
[attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"nd" options:NSBackwardsSearch]];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"nd"]];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"nd" options:NSBackwardsSearch]];

[attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"rd"]];
[attString addAttribute:NSFontAttributeName value:(smallFont) range:[string rangeOfString:@"rd" options:NSBackwardsSearch]];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"rd"]];
[attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:[string rangeOfString:@"rd" options:NSBackwardsSearch]];

[attString endEditing];
return attString;
}

并在cellForRowAtIndexPath中:

NSString *basicString = dieMenueDaten.SubtitelE;

basicString = [basicString stringByReplacingOccurrencesOfString:@"<sup>" withString:@""];
basicString = [basicString stringByReplacingOccurrencesOfString:@"</sup>" withString:@""];

NSMutableAttributedString *newSubtitleE = [[NSMutableAttributedString alloc] initWithString:basicString];

newSubtitleE = [self attributedSubTitleString:basicString];
....
        cell.MenueTitel.text = dieMenueDaten.TitelE;
        cell.MenueSubtitel.attributedText = newSubtitleE;

适合我的作品。

答案 2 :(得分:-1)

检查此存储库:DecodeHTML.swift