2012-11-18 29 views
8

例如,我有3句,如@“很久很久以前”,@“有一个孩子。” @“唧唧歪歪” 我提出以下为什么appendAttributedString在新行中显示附加字符串?

- (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在新行中显示附加字符串?我怎样才能把附加的文本放在一个段落中?

感谢您的任何帮助。

回答

12

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

我不知道这是否是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; 
} 
+0

它就像一个迷人。谢谢,rmaddy。你真的救了我的命。 – echo

+0

得爱rmaddy。 –

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]; 
相关问题