【问题标题】:NSMutableAttributedString with NSRange issueNSMutableAttributedString 与 NSRange 问题
【发布时间】:2014-06-20 14:31:21
【问题描述】:

我有以下字符串

22\n沙班\n1435

我正在使用NSMutableAttributedString 使用多种字体格式化上述字符串,如下所示:

NSString* orgString=@"22\nShaʻban\n1435";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[dateStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
UIFont *dayFont=[UIFont fontWithName:@"Helvetica-Bold" size:40.0f];
UIFont *monthFont=[UIFont fontWithName:@"Arial" size:22.0f];
UIFont *yearFont=[UIFont fontWithName:@"Arial" size:20.0f];

//format day part
[attString addAttribute:NSFontAttributeName value:dayFont range:NSMakeRange(0,2)];
//format month part
[attString addAttribute:NSFontAttributeName value:monthFont range:NSMakeRange(3,[self indexOf:[dateStr substringFromIndex:3] andSearchChar:@"\n"])];
//format year part, app crashes here
[attString addAttribute:NSFontAttributeName value:yearFont range:NSMakeRange([self indexOf:[dateStr substringFromIndex:3] andSearchChar:@"\n"]+1,[dateStr length])];


- (int) indexOf:(NSString*)orgStr andSearchChar:(NSString *)charToSearc  {
NSRange range = [orgStr rangeOfString:charToSearc];
if ( range.length > 0 ) {
    return range.location;
} else {
    return -1;
  }
}

我不知道为什么在尝试格式化最后一部分时它会崩溃,我从第二部分的最后一个位置+1到字符串的长度,请帮助

【问题讨论】:

  • orgString 是 dateStr 的拼写错误?

标签: objective-c cocoa-touch nsattributedstring nsrange


【解决方案1】:
NSRange NSMakeRange (
   NSUInteger loc,
   NSUInteger len
);

范围是位置和长度,而不是开始和结束位置。所以你需要改变计算范围内容的方式。

或者,拆分源字符串,为每个部分创建一个属性字符串,然后将它们附加在一起。

【讨论】:

    【解决方案2】:

    我建议这样做:

    NSString* orgString=@"22\nShaʻban\n1435";
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];
    
    UIFont *dayFont = [UIFont fontWithName:@"Helvetica-Bold" size:40.0f];
    UIFont *monthFont = [UIFont fontWithName:@"Arial" size:22.0f];
    UIFont *yearFont = [UIFont fontWithName:@"Arial" size:20.0f];
    
    NSArray *array = [orgString componentsSeparatedByString:@"\n"];
    
    [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:[array objectAtIndex:0] attributes:@{NSFontAttributeName: dayFont}]];
    [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:[array objectAtIndex:1] attributes:@{NSFontAttributeName: monthFont}]];
    [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:[array objectAtIndex:2] attributes:@{NSFontAttributeName: yearFont}]];
    

    所以,没有NSRange 问题。另外,正如@Wain 所说,您误解了NSRange 是什么。 而不是你在做什么,一旦你找到了位置,你必须把,作为NSMakeRange的第二个参数:nextLocation-currentLocation。 Id est,对于最后一个,是这样的:

    NSMakeRange([self indexOf:[dateStr substringFromIndex:3] andSearchChar:@"\n"]+1,
                [dateStr length]-[self indexOf:[dateStr substringFromIndex:3] andSearchChar:@"\n"]+1)
    

    【讨论】:

      猜你喜欢
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多