更改NSMutableAttributedString的多个部分的属性

时间:2016-10-14 00:18:01

标签: ios objective-c cocoa-touch nsattributedstring

我需要针对我最初是NSString时插入的值检查NSMutableString。

它的一个例子是:

NString* textToDraw = @"";
textToDraw = [textToDraw stringByAppendingString:[[NSString alloc] initWithFormat:@"       
Summary of currently worked on process\n\nTitle name:\nResults for Process\n    %@\n%@\n%@\n, resultOne, resultTwo, resultThree]];

resultOneresultTworesultThree只是标准的NSString值

然后我为一些添加的格式初始化NSMutableAttributedString。

NSMutableAttributedString *summaryBody  = [[NSMutableAttributedString alloc]  initWithString:textToDraw];

CTFontRef centuryGothicStandard;
CTFontRef centuryGothicTitle;

centuryGothicStandard = CTFontCreateWithName(CFSTR("Century Gothic"), 12.0, nil);
centuryGothicTitle = CTFontCreateWithName(CFSTR("Century Gothic-Bold"), 24.0, nil);

//Modify the summary, so that the title is larger, and bolded, the subtitles are bolded, and the text is century gothic font
[summaryBody addAttribute:(id)kCTFontAttributeName
                    value:(__bridge id)centuryGothicStandard
                    range:NSMakeRange(0, [summaryBody length])];

我需要做的是将resultOneresultTworesultThree修改为红色,我在Change attributes of substrings in a NSAttributedString中尝试了解答的解决方案,但我不明白这个概念。

我可以简单地遍历NSMutableAttributedString并找到我插入的特定字符“|”一个起点和'?'为了一个终点? 我已经搜索了一个特定的索引,但是没有任何与NSAttributedMutatableString相关的内容弹出NSRange,我不会特别知道。

这是一些伪代码,以帮助解释我认为可能的解决方案:

textToDraw = [textToDraw stringByAppendingString:[[NSString alloc] initWithFormat:@" Summary of currently worked on process\n\nTitle name:\nResults for Process\n    |%@?\n|%@?\n|%@?\n, resultOne, resultTwo, resultThree]];
(for int i = 0; i < [NSAttributedMutatableString length]; i++)
{
    if(char at i == '|')
    {
        get an NSRange from this value to the char at i == '?'
        [NSAttributedMutatableStringS addAttribute:(id)kCTForegroundColorAttributeName
                    value:(id)[UIColor redColor].CGColor
                    range:NSMakeRange(0, NSRangeAbove])];
    }
}

我目前可以获得resultOne等值,并创建NSAttributedStrings并添加更改颜色的属性,唯一的问题当然是不知道索引值,这是另一种可能性。

1 个答案:

答案 0 :(得分:0)

如果您确定它们永远不会出现在resultX字符串中,那么搜索起始字符或字符序列就可以了......

一种方法是逐个构建一个字符串,并在运行中创建一个范围数组。最后,你只需打上它们的属性。

NSMutableString *mTextToDraw = [[NSMutableString alloc] init];
NSMutableArray *mRangesToMod = [[NSMutableArray alloc] init];
NSValue *mRange = nil;

[mTextToDraw appendFormat:@"Summary of currently worked on process\n\nTitle name:\nResults for Process\n    "];

mRange = [NSValue valueWithRange:NSMakeRange(mTextToDraw.length,resultOne.length)];;
[mRangesToMod addObject: mRange];
[mString appendFormat:@"%@\n",resultOne];

mRange = [NSValue valueWithRange:NSMakeRange(mTextToDraw.length,resultTwo.length)];;
[mRangesToMod addObject: mRange];
[mString appendFormat:@"%@\n",resultTwo];

mRange = [NSValue valueWithRange:NSMakeRange(mTextToDraw.length,resultThree.length)];;
[mRangesToMod addObject: mRange];
[mString appendFormat:@"%@\n",resultThree];

NSMutableAttributedString *summaryBody = [[NSMutableAttributedString alloc] initWithString:mTextToDraw];

for (NSValue *vRange in mRangesToMod)
{
    NSRange range = vRange.rangeValue;

    [summaryBody setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Roboto-Light" size:smallFont]}
                         range:range];

    // or you can use addAttribute...
}

我用了#34; Roboto-Light&#34;在我的应用程序中 - 它不会按原样在你的应用程序中工作。仅作为一个例子。对于测试,只需要放置一些字体,然后是基本的(原始的)...