1

i'm trying to set a set of Nsattributestring for my primary string. the situation is this:

for example i have 3 string like this:

card1.contents = @"■■"; 
card2.contents = @"▲▲▲";
card3.contents = @"■■■";

and the last string like this:

matchingString = @"Matched 3 card: ▲▲▲&■■■&▲▲▲ for 80 points"

the matchingString value is inside a label. i would try to find the range of substrings in a matchingString, and add an NsAttributeString for etch substring.

i'm using this code inside a loop for add the attribute to my label:

 [self addLableAttributes:@{NSForegroundColorAttributeName: [SetCardGameViewController getUIColorFromColorCard:card.color alphavalue:1],
                 NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)} range:[[self.matchedResulLabel.attributedText string] rangeOfString:card.contents]];

this code doesn't work, when i print the text from label, it don't have any attribute, i have tried to change

[[self.matchedResulLabel.attributedText string] rangeOfString:card.contents]

to

range:[[self.matchedResulLabel.attributedText string] rangeOfString:self.matchedResulLabel.text]

in this case it works fine, but is not what i need. So i think the problems is in the Range, but e cant fine were .

this is the code for the addLableAttributes method:

    - (void)addLableAttributes:(NSDictionary *)attributes range:(NSRange)range
{
    if (range.location != NSNotFound) {
        NSMutableAttributedString *mat = [self.matchedResulLabel.attributedText mutableCopy];
        [mat addAttributes:attributes range:range];
        self.matchedResulLabel.attributedText = mat;
    }
}

thank you

Max_Power89
  • 1,710
  • 1
  • 21
  • 38

1 Answers1

0

you maybe want to check whether your card.contents has values, or maybe you want to try:

range:NSMakeRange(0, [shape length])
dumduke
  • 471
  • 4
  • 15