2012-11-18 19 views
8

Mesela ben "bla bla bla" @, "Bir çocuk var" @ "Uzun süre önce" @ gibi 3 cezan var Ve yüzdenAppendAttributedString neden yeni bir satıra eklenecek?

- (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; 
} 

altında bir yöntem yapılmış çağrılan appendText 3 kez

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

beklediğim sonuç

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

olduğunu Ama çıkış

olduğunu
Long long ago. 
There is a child. 
bla bla bla 

Neden appendAttributedString yeni bir satıra eklenmiş dizeyi gösterir? Ekli metni tek paragrafa nasıl yerleştirebilirim?

Yardımlarınız için teşekkür ederiz.

cevap

12

Kendi deneyimlerimden, UITextView öğesinin attributedText özelliğini ayarladığınızda, metin buna yeni satır ekliyor. Bu nedenle, UITextView numaralı telefondan attributedText numaralı telefonu geri aldığınızda, bu yeni satır metnin sonundadır. Böylece daha fazla metin eklediğinizde, yeni satır ortadadır (ve diğeri sonuna eklenir).

bu UITextView veya muhtemelen NSAttributedString bir hata olup olmadığını bilmiyorum.

Bir geçici çözüm aşağıdaki gibi kodu güncelleyin olacaktır: Bu büyüleyici bir gibi çalışır

- (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

. Sağol rmaddy. Hayatımı gerçekten kurtardın. – echo

+0

rmaddy'yi seviyorum. –

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]; 
İlgili konular