8

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

- (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 在新行中显示附加字符串?如何将附加文本放在一个段落中?

谢谢你的帮助。

4

2 回答 2

12

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

我不知道这是否是一个错误UITextView或可能NSAttributedString

一种解决方法是按如下方式更新您的代码:

- (void)appendText:(NSString*)text {
    NSMutableAttributedString *originalText = [[NSMutableAttributedString alloc] initWithAttributedString:_textView.attributedText];
    // Remove any trailing newline
    if (originalText.length) {
        NSAttributedString *last = [originalText attributedSubstringFromRange:NSMakeRange(originalText.length - 1, 1)];
        if ([[last string] isEqualToString:@"\n"]) {
            originalText = [originalText attributedSubstringFromRange:NSMakeRange(0, originalText.length - 1)];
        }
    }

    NSAttributedString *appendText = [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName:DefaultTextColor}];

    [originalText appendAttributedString:appendText];
    _textView.attributedText = originalText;
}
于 2012-11-18T20:38:18.003 回答
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];
于 2014-06-17T10:59:52.700 回答