2012-11-18 25 views
8

Ví dụ, tôi có 3 câu như @ "trước dài dài.", @ "Có một đứa trẻ.", @ "Bla bla bla" Và tôi đã thực hiện một phương pháp dưới đâyTại sao appendAttributedString hiển thị chuỗi được nối thêm trong một dòng mới?

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

Vì vậy, tôi Viện dẫn appendText 3 lần

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

kết quả tôi mong đợi là

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

Nhưng đầu ra là

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

Tại sao appendAttributedString hiển thị chuỗi được nối thêm trong một dòng mới? Làm thế nào tôi có thể đặt các văn bản nối thêm trong một đoạn văn?

Cảm ơn bạn đã được trợ giúp.

Trả lời

12

Từ kinh nghiệm của riêng tôi, khi bạn đặt thuộc tính attributedText của UITextView, văn bản sẽ nhận được dòng mới được thêm vào. Vì vậy, khi bạn truy xuất số attributedText từ số UITextView, dòng mới này nằm ở cuối văn bản. Vì vậy, khi bạn nối thêm văn bản, dòng mới nằm ở giữa (và một dòng mới được thêm vào cuối).

Tôi không biết đây có phải là lỗi trong số UITextView hoặc có thể là NSAttributedString.

Một workaround sẽ được cập nhật mã của bạn như sau:

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

Nó hoạt động giống như một quyến rũ. Cảm ơn, rmaddy. Bạn thực sự đã cứu mạng tôi. – echo

+0

Gotta yêu 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]; 
Các vấn đề liên quan