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

4

1 回答 1

0

您可能想检查您的 card.contents 是否有值,或者您想尝试:

range:NSMakeRange(0, [shape length])
于 2014-08-06T07:47:23.957 回答