0
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];

Application gets crashed at this line [attributedString addAttributes:linkAttributes range:policylinkRange];

  • 1
    Please add some more elements to your question, just pasting here your code won't help users identify your problem. – Luca De Nardi Nov 03 '17 at 10:11
  • 2
    My guess? `NSMakeRange(50,70)` should be `NSMakeRange(50,20)`. It's `(location, length)` not `(location, end)`. Same issue for the other range – Larme Nov 03 '17 at 10:13
  • Thanks , used end character index instead of length – Rukku Guptha Nov 03 '17 at 10:15
  • Possible duplicate of [NSMutableAttributedStrings - objectAtIndex:effectiveRange:: Out of bounds](https://stackoverflow.com/questions/11571948/nsmutableattributedstrings-objectatindexeffectiverange-out-of-bounds) – Larme Nov 03 '17 at 10:51
  • Your Range length is greater than actual length – Nisar Ahmad Nov 03 '17 at 11:49

2 Answers2

1

Try This

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];
Sapana Ranipa
  • 889
  • 7
  • 20
0

Use this:

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];
Sapana Ranipa
  • 889
  • 7
  • 20
Sargis
  • 1,196
  • 1
  • 18
  • 31