繁体   English   中英

更改NSMutableAttributedString的多个部分的属性

[英]Change attributes of multiple parts of a NSMutableAttributedString

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

一个例子是:

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是红色的,我试图在回答解决在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,唯一的问题当然是不知道索引值,因此这是另一种可能。

如果确定它们永远不会出现在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...
}

我在我的应用中使用了“ Roboto-Light”-它无法在您的应用中正常工作。 举个例子。 为了进行测试,只需在基本(原始)字体之外再放一些字体即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM