结合归属字符串

时间:2013-06-11 18:23:42

标签: iphone xcode nsattributedstring

作为一个例子,假设我有一个10 NSAttributedStrings的数组,将它们组合成一个字符串的最佳方法是什么?我知道appendAttributedString方法,但这只允许一次合并一个,这意味着需要一个循环。
或者是否需要将它们组合在一起才能进入textview - 只需要一个循环将它们添加到该视图中?我只是试图了解如何将不同格式的大量文本添加到textview中!

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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)

希望它可以帮助某人。

答案 2 :(得分:-3)

你可以做这样的事情

NSString *attr = [NSString stringWithFormat:@"%@%@%@", attributedString1, string2, string3];

这样做会产生一个attr字符串变量,所有10个字符串放在一起,你可以[textView setText:attr];:)