为什么appendAttributedString在新行中显示附加的字符串?

时间:2012-11-18 15:36:55

标签: objective-c nsattributedstring

例如,我有3个句子,如@“很久很久以前。”,@“有一个孩子。”,@“bla bla bla” 我在下面做了一个方法

- (void)appendText:(NSString*)text
{
    NSMutableAttributedString *originalText = [[NSMutableAttributedString alloc] initWithAttributedString:_textView.attributedText];
    NSAttributedString *appendText = [[NSAttributedString alloc] initWithString:text
                                                                     attributes:@{NSForegroundColorAttributeName:DefaultTextColor}];

    [originalText appendAttributedString:appendText];
    _textView.attributedText = originalText;
}

所以我调用了appendText 3次

[self appendText:@"Long long ago."];
[self appendText:@"There is a child."];
[self appendText:@"bla bla bla."];

我期望的结果是

Long long ago.There is a child.bla bla bla.

但输出是

Long long ago.
There is a child.
bla bla bla

为什么appendAttributedString在新行中显示附加字符串?如何将附加文本放在一个段落中?

感谢您的帮助。

2 个答案:

答案 0 :(得分:12)

根据我自己的经验,当您设置attributedText的{​​{1}}属性时,文本会添加一个换行符。因此,当您稍后从UITextView检索attributedText时,此换行符位于文本的末尾。因此,当您添加更多文本时,换行符位于中间(另一个添加到结尾)。

我不知道这是UITextView还是UITextView中的错误。

一种解决方法是更新代码,如下所示:

NSAttributedString

答案 1 :(得分:0)

NSMutableAttributedString *titleAttrString    = [[NSMutableAttributedString alloc] initWithString:
                                                 @"Hello" attributes:
                                                 [NSDictionary dictionaryWithObjectsAndKeys:
                                                  [UIFont  systemFontOfSize:15],NSFontAttributeName,
                                                  [UIColor blackColor], NSStrokeColorAttributeName,nil]];

NSAttributedString *descAttrString    = [[NSAttributedString alloc] initWithString:
                                         @"\nWorld" attributes:
                                         [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont  systemFontOfSize:19],NSFontAttributeName,
                                          [UIColor blueColor], NSStrokeColorAttributeName,nil]];
[titleAttrString appendAttributedString:descAttrString];


NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:
[UIFont systemFontOfSize:20]forKey: NSFontAttributeName];

CGSize tsize = [@"Test" boundingRectWithSize:CGSizeMake(self.view.frame.size.width-20, CGFLOAT_MAX)
                                     options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                  attributes:stringAttributes context:nil].size;

UILabel *menuTitle = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, CGRectGetWidth(self.view.frame)-2, tsize.height+2)];
menuTitle.numberOfLines = 0;
menuTitle.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
menuTitle.textAlignment = NSTextAlignmentCenter;
menuTitle.adjustsFontSizeToFitWidth = YES;
menuTitle.minimumScaleFactor = 0.5f; // Chnage as your need
[GlobalSettings setCellLableProperities:menuTitle font:[GlobalSettings normalFont]];
menuTitle.font = [GlobalSettings getFontWith:9];
menuTitle.textColor = [UIColor colorWithRed:0.8 green:0.11 blue:0.11 alpha:1.0];

menuTitle.attributedText = titleAttrString;
[self.view addSubview:menuTitle];