【问题标题】:range of string does not work correctly whit nsAttributeString字符串范围不能与 nsAttributedString 一起正常工作
【发布时间】:2013-07-14 21:36:31
【问题描述】:

我正在尝试为我的主字符串设置一组 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;
    }
}

谢谢

【问题讨论】:

    标签: ios objective-c string ios6 cs193p


    【解决方案1】:

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

    range:NSMakeRange(0, [shape length])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2018-11-10
      相关资源
      最近更新 更多