例として、 10 の配列があるとNSAttributedStrings
します。それらすべてを 1 つの文字列に結合するための最良の方法は何ですか? 私はそのappendAttributedString
方法を知っていますが、これは一度に1つしか結合できないため、ループが必要です。
または、それらがテキストビューに入るだけの場合、それらを組み合わせる必要はありますか?それらをそのビューに追加するためのループがあるだけですか? 私は、さまざまな形式のテキストがテキストビューにどのように追加されるかについて頭を悩ませようとしています!
2281 次
3 に答える
1
してみてください:
NSRange range = NSMakeRange(0, 1);
NSString *space = @" ";
NSMutableAttributedString *attSpace = [[NSMutableAttributedString alloc] initWithString:space];
[attSpace addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range];
[attSpace addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light"
size:11.f] range:range];
range = NSMakeRange(0, [string1 length]);
NSMutableAttributedString *att1 = [[NSMutableAttributedString alloc] initWithString:string1];
[att1 addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range];
[att1 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light"
size:11.f] range:range];
range = NSMakeRange(0, [string2 length]);
NSMutableAttributedString *att2 = [[NSMutableAttributedString alloc] initWithString:string2];
[att2 addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range];
[att2 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light"
size:11.f] range:range];
[att1 appendAttributedString:attSpace];
[att1 appendAttributedString:att2];
于 2014-09-27T03:44:18.473 に答える
0
UIButton のタイトルの上に、結果の属性付き文字列でプレーン、ストライク、太字のテキストを組み合わせて作成した一種の複雑な組み合わせを共有します。
// Monthly button
NSMutableAttributedString *strikedOutPrice = [[NSMutableAttributedString alloc] initWithString:@"6,99"];
[strikedOutPrice setAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor], NSStrikethroughStyleAttributeName: @2 } range:NSMakeRange(0, [strikedOutPrice length])];
NSMutableAttributedString *boldPrice = [[NSMutableAttributedString alloc] initWithString:@"4,99"];
[boldPrice setAttributes:@{ NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f], NSForegroundColorAttributeName : [UIColor whiteColor] } range:NSMakeRange(0, [boldPrice length])];
NSMutableAttributedString* titleStringMonthly = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@ %@" , [[LocalizationSystem sharedLocalSystem] localizedStringForKey:@"1 Month for" value:@"1 Mes por"], @"6,99", @"4,99"]];
[titleStringMonthly setAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] } range:NSMakeRange(0, [titleStringMonthly length])];
NSString *language = [Utils getAppLanguage];
if([language isEqualToString:@"es"])
{
[titleStringMonthly replaceCharactersInRange:NSMakeRange(10, 4) withAttributedString:strikedOutPrice];
[titleStringMonthly replaceCharactersInRange:NSMakeRange(15, 4) withAttributedString:boldPrice];
}
else
{
[titleStringMonthly replaceCharactersInRange:NSMakeRange(12, 4) withAttributedString:strikedOutPrice];
[titleStringMonthly replaceCharactersInRange:NSMakeRange(17, 4) withAttributedString:boldPrice];
}
[_oneMonthButton setAttributedTitle:titleStringMonthly forState:UIControlStateNormal];
この場合、次の範囲のインデックスと長さを追跡し、NSMutableAttributedString を置換する動的カウンターをいくつか持つことができると思います (例の titleStringMonthly で行うように)。
それが誰かを助けることを願っています。
于 2016-07-14T14:04:55.840 に答える