8

たとえば、@ "ずっと昔。"、@ "子供がいます。"、@"blablabla"のように3つの文があります。以下のメソッドを作成しました。

- (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つの段落に入れるにはどうすればよいですか?

助けてくれてありがとう。

4

2 に答える 2

12

From my own experiences, when you set the attributedText property of UITextView, the text is getting a newline added to it. So when you later retrieve the attributedText from the UITextView, this newline is at the end of the text. So when you append more text, the newline is in the middle (and another one is added to the end).

I don't know if this is a bug in UITextView or possibly NSAttributedString.

One workaround would be to update your code as follows:

- (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;
}
于 2012-11-18T20:38:18.003 に答える
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];
于 2014-06-17T10:59:52.700 に答える