我使用以下内容将换行符附加到我的NSStrings
// Add line break to strings
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
我理想的是在字符串之前和之后添加 换行符 的解决方案,而不是在结尾处使用\n\n
两次?这可能吗?
如果需要,可以在此处查看完整代码:
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];
for (NSString *testWord in ptpArray) {
if ([testWord rangeOfString:@"*"].location != NSNotFound) {
//Remove Marker
NSString *stripped = [testWord stringByReplacingOccurrencesOfString:@"*" withString:@""];
//Get string and add bold
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:stripped];
NSRange selectedRange = [stripped rangeOfString:(stripped)];
[string beginEditing];
[string addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Helvetica-Bold" size:14.0]
range:selectedRange];
[string endEditing];
[attrString appendAttributedString:string];
}
else
{
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\u2022 "]];
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:testWord]];
}
// Add line break to strings
[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
答案 0 :(得分:1)
NSMutableAttributedString
也会回复
- (void)insertAttributedString:(NSAttributedString *)attributedString atIndex:(NSUInteger)index
。
因此,您可以使用它在\n
处插入index 0
(并在结尾处继续附加\n
)。