2013-07-14 60 views
1

我試圖爲我的主字符串設置一組Nsattributestring。情況是這樣的:字符串的範圍無法正常工作whit nsAttributeString

例如我有3個字符串是這樣的:

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

和最後一個字符串是這樣的:

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

的matchingString值是一個標籤內。 我會嘗試找到匹配字符串中的子字符串的範圍,併爲蝕刻子字符串添加NsAttributeString。

我使用的是循環中的代碼爲屬性添加到我的標籤:

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

這個代碼不工作,當我從打印標籤的文本,它不具備任何屬性我曾試圖改變

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

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

在這種情況下,它工作正常,但不是我所需要的。所以我認爲問題是在範圍內,但沒有問題。

這是爲addLableAttributes方法的代碼:

- (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; 
    } 
} 

謝謝

回答

0
你也許要檢查你的card.contents是否有值,或者你想嘗試

range:NSMakeRange(0, [shape length]) 
相關問題