2016-10-14 9 views
0

元々NSStringだったときに挿入した値に対してNSMutableStringをチェックする必要があります。NSMutableAttributedStringの複数の部分の属性を変更する

それの例は:

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だけをポップアップします。これは特にわかりません。ここで

は、私は可能な解決策かもしれないと思うかを説明するのに役立ついくつかの擬似コードである:私は現在、resultOneと、このような値を取得し、色を変えるの追加属性でNSAttributedStringsを作成することができます

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])]; 
    } 
} 

、唯一の問題はもちろんインデックス値を知らないので、別の可能性もあります。あなたは、彼らがresultX文字列で表示されることはありません特定されている場合は、開始エンドの文字または文字列を検索する

答えて

0

はokです...

一つのアプローチは、一枚で、文字列の部分を構築し、範囲の配列を作成することです急いで。最後に、それらの属性を単純に叩くだけです。

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」を使用しました。そのままあなたのものでは動作しません。例のように。テストのために、基本的な(元の)もの以外のフォントを置いてください。

+0

私の質問に答えるおかげで、私はチャンスを得るときにこれをテストしなければなりません。 –

+0

私はあなたの答えをテストして、喜んでそれが動作すると言うことができます:D、本当に助けてくれてありがとう! –

+0

あなたは大歓迎です! –

関連する問題