例如,假设我有一个 10 数组,NSAttributedStrings
将它们全部组合成一个字符串的最佳方法是什么?我知道这种appendAttributedString
方法,但这只允许一次组合一个,这意味着需要一个循环。
或者,如果它们只是进入一个文本视图,是否需要将它们组合起来 - 只需有一个循环将它们添加到该视图中?我只是想弄清楚如何将大量不同格式的文本添加到文本视图中!
问问题
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 回答
-2
你可以做这样的事情
NSString *attr = [NSString stringWithFormat:@"%@%@%@", attributedString1, string2, string3];
这样做会产生一个attr
字符串变量,将所有 10 个字符串放在一起,你可以这样做[textView setText:attr];
:)
于 2013-06-11T19:52:29.600 回答