NSAttributedString 每行新颜色(数组中的字符串)
NSAttributedString new color each line (Strings from array)
我正在尝试显示存储在 UITextView 数组中的彩色文本。 NSAttributedStrings 通常有效,但我想在新行中显示每个 objectAtIndex 项目并更改文本的颜色。这是我的代码,它显示奇怪的输出:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
for (NSString *str in self.tvInputArray){
if ([str containsString:@"-"] ) {
NSAttributedString *atrString = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n",str] attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
[attributedString appendAttributedString:atrString];
}
else if ([str containsString:@"+"] ) {
NSAttributedString *atrString2 = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n",str] attributes:@{NSForegroundColorAttributeName: [UIColor blueColor]}];
[attributedString appendAttributedString:atrString2];
}
}
[selectedItemsTV setText:[NSString stringWithFormat:@"%@", attributedString]];
- 遍历数组以检查符号“-”和“+”
- 根据“-”和“+”分配颜色
- 在 UITextView 中显示属性字符串
实际 UITextView 中的奇怪输出是:
MyString - {
NSColor = "UIDeviceRGBColorSpace 0.760784 0.235294 0.623529 1";
}
有人能给我指出正确的方向吗?
知道了,我忘了将它作为属性字符串传递给 UITextView:
[selectedItemsTV setAttributedText:attributedString];
希望这对某人有所帮助
我正在尝试显示存储在 UITextView 数组中的彩色文本。 NSAttributedStrings 通常有效,但我想在新行中显示每个 objectAtIndex 项目并更改文本的颜色。这是我的代码,它显示奇怪的输出:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
for (NSString *str in self.tvInputArray){
if ([str containsString:@"-"] ) {
NSAttributedString *atrString = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n",str] attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];
[attributedString appendAttributedString:atrString];
}
else if ([str containsString:@"+"] ) {
NSAttributedString *atrString2 = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n",str] attributes:@{NSForegroundColorAttributeName: [UIColor blueColor]}];
[attributedString appendAttributedString:atrString2];
}
}
[selectedItemsTV setText:[NSString stringWithFormat:@"%@", attributedString]];
- 遍历数组以检查符号“-”和“+”
- 根据“-”和“+”分配颜色
- 在 UITextView 中显示属性字符串
实际 UITextView 中的奇怪输出是:
MyString - {
NSColor = "UIDeviceRGBColorSpace 0.760784 0.235294 0.623529 1";
}
有人能给我指出正确的方向吗?
知道了,我忘了将它作为属性字符串传递给 UITextView:
[selectedItemsTV setAttributedText:attributedString];
希望这对某人有所帮助