相关疑难解决方法(0)

如何在NSString中获取特定字符的所有NSRange?

我有两个NSStrings:orgTextsearchLetter.
我想orgText用红色突出显示searchLetter的每一个匹配项.
我怎样才能得到NSRange所有出现的searchLetter
例如:

suppose: orgText = "abcahaiapaoiuiapplma"
         searchLetter = "a".
Run Code Online (Sandbox Code Playgroud)

我想高亮显示红色"abcahaiapaoiuiapplma"中的所有"a"事件.
谢谢.

objective-c nsstring ios nsrange

19
推荐指数
2
解决办法
2万
查看次数

如何更改某个属性字符串的文本颜色或使其变为粗体?

说我有NSString*hello = @"hello world";

现在我想要一个有问题的字符串,其中hello world中的地狱是粗体.

网络上有一个功能:

- (NSMutableAttributedString*) word:(NSString*)substringToHighlight{

    NSMutableAttributedString * mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:self];
    NSUInteger count = 0, length = [mutableAttributedString length];
    NSRange range = NSMakeRange(0, length);

    count = 0,
    length = [mutableAttributedString length];
    range = NSMakeRange(0, length);
    while(range.location != NSNotFound)
    {
        range = [[mutableAttributedString string] rangeOfString:substringToHighlight options:0 range:range];
        if(range.location != NSNotFound) {

            //[mutableAttributedString setTextColor:[UIColor blueColor] range:NSMakeRange(range.location, [word length])];
            range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
            count++;
        }
    }
    return mutableAttributedString;
}
Run Code Online (Sandbox Code Playgroud)

但是,该函数不起作用,因为mutableAttributedString不支持setTextColor

我也试过了 …

objective-c xcode4.5

2
推荐指数
1
解决办法
8072
查看次数

标签 统计

objective-c ×2

ios ×1

nsrange ×1

nsstring ×1

xcode4.5 ×1