更改 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]];
resultOne
、resultTwo
、resultThree
只是标准的 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])];
我需要做的是将resultOne
、resultTwo
和resultThree
修改为红色,我尝试了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,唯一的问题当然是不知道索引值,所以这是另一种可能性。
如果您确定它们永远不会出现在 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" - 它在你的应用程序中不起作用。举个例子。为了测试,只需在基本(原始)字体之外放一些字体...
我需要检查 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]];
resultOne
、resultTwo
、resultThree
只是标准的 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])];
我需要做的是将resultOne
、resultTwo
和resultThree
修改为红色,我尝试了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,唯一的问题当然是不知道索引值,所以这是另一种可能性。
如果您确定它们永远不会出现在 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" - 它在你的应用程序中不起作用。举个例子。为了测试,只需在基本(原始)字体之外放一些字体...