0

I have the following code :

NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];

[boldS addAttribute:NSFontAttributeName value:SOMEBOLDFONT range:NSMakeRange(0, boldS.length)];

[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];

As you can see, I want to bold the Son part. This does not work if I do the above statements but only works if I do :

[[attrS mutableCopy] replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
                         withAttributedString:boldS];

What might be the reason for that?

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • 2
    How does it not work? And where do you call `addAttribute` ? – Martin R May 21 '14 at 21:10
  • 1
    I don't see where you bold you `NSMutableAttributedString` : all you do is change a string for the same string. Is there a `addAttributes` or something like that that you forgot to post here ? – Emilie May 21 '14 at 21:10
  • Let me update it. Thanks. – gran_profaci May 21 '14 at 21:13
  • 2
    I can't replicate this. Is this real code that you've run and had the problem with? – Chuck May 21 '14 at 21:27
  • 1
    As with Chuck, I ran this code and it works perfectly. You've made a mistake elsewhere. – Tommy May 21 '14 at 21:27
  • What is SOMEBOLDFONT? Are you sure you didn't screw that up? – Abhi Beckert May 21 '14 at 21:33
  • make sure your `attrS` is actually `NSMutableAttributedString` not `NSAttributedString` – Bryan Chen May 21 '14 at 21:56
  • I can totally understand that you cannot replicate. What I'm looking for is what exact difference would an NSMutableAttributedString mutable do? Does it copy the pointer? – gran_profaci May 21 '14 at 22:15
  • 1
    Intuitively enough, `mutableCopy` creates a copy of the receiver — that is, a new object that has the same attributes as the receiver — that is mutable. – Chuck May 21 '14 at 22:38

1 Answers1

3

addAttribute works regardless of whether you take a mutableCopy. Your question is based on a false assumption. It therefore has no answer.

Run this:

NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:@"• Get Tested Son"];
NSMutableAttributedString *boldS = [[NSMutableAttributedString alloc] initWithString:@"Son"];

UIFont *someBoldFont = [UIFont fontWithName:@"Arial" size:23.0f];
[boldS addAttribute:NSFontAttributeName value:someBoldFont range:NSMakeRange(0, boldS.length)];

NSMutableAttributedString *attrSCopy = [attrS mutableCopy];

[attrS replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];
[attrSCopy replaceCharactersInRange:[attrS.string rangeOfString:boldS.string]
           withAttributedString:boldS];

NSLog(@"%@", [attrS isEqual:attrSCopy] ? @"equal" : @"different");

It will output equal. Comment out the replaceCharactersInRange: for either attrS or attrSCopy and it will output different.

Tommy
  • 99,986
  • 12
  • 185
  • 204