2012-11-18 9 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が新しい行に追加された文字列を表示するのですか?どのようにして、追加されたテキストを1つの段落に入れることができますか?

ありがとうございました。

答えて

12

私自身の経験から、プロパティをUITextViewに設定すると、テキストに改行が追加されています。したがって、後で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]; 
関連する問題