2012-11-18 24 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]; 
相關問題