【问题标题】:Getting crash while adding attributes to NSMutableAttributedString for a particular range in iOS在为 iOS 中的特定范围向 NSMutableAttributedString 添加属性时出现崩溃
【发布时间】:2017-11-03 10:07:58
【问题描述】:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
                                                initWithString:@"I have read, understand and agree to the following terms and conditions and web usage policy." attributes:nil];
NSRange conditionlinkRange = NSMakeRange(50,70);
// for the word "terms and conditions" in the string above
NSRange policylinkRange = NSMakeRange(75,92);
NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0],
                                  NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };
[attributedString addAttributes:linkAttributes range:policylinkRange];
[attributedString addAttributes:linkAttributes range:conditionlinkRange];

应用程序在此行崩溃 [attributedString addAttributes:linkAttributes range:policylinkRange];

【问题讨论】:

  • 请在您的问题中添加更多元素,仅在此处粘贴您的代码不会帮助用户识别您的问题。
  • 我猜? NSMakeRange(50,70) 应该是 NSMakeRange(50,20)。这是(location, length) 不是(location, end)。其他范围也有同样的问题
  • 谢谢,使用结束字符索引而不是长度
  • 您的 Range 长度大于实际长度

标签: ios objective-c


【解决方案1】:

试试这个

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"I have read, understand and agree to the following terms and conditions and web usage policy." attributes:nil];

NSRange conditionlinkRange = NSMakeRange(51,20);
NSRange policylinkRange = NSMakeRange(86,6);
NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };

[attributedString addAttributes:linkAttributes range:policylinkRange];
[attributedString addAttributes:linkAttributes range:conditionlinkRange];

【讨论】:

    【解决方案2】:

    使用这个:

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"I have read, understand and agree to the following terms and conditions and web usage policy." attributes:nil];
        NSRange conditionlinkRange = [attributedString.string rangeOfString:@"terms and conditions"];
    
        // for the word "terms and conditions" in the string above
        NSRange policylinkRange = [attributedString.string rangeOfString:@"policy"];
        NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0],  NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };
    
        [attributedString addAttributes:linkAttributes range:policylinkRange];
        [attributedString addAttributes:linkAttributes range:conditionlinkRange];
    

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多