UITextView中的垂直文本归因于文本

时间:2013-12-13 13:52:45

标签: ios text uitextview nsattributedstring vertical-text

我有一个UITextView,用户可以在其中输入文本。我添加了一些代码,以便当输入的文本包含#hashtag时,该字将显示为蓝色。这是代码:

- (void)textViewDidChange:(UITextView *)textView
{
    //Coloring of hashtags
    NSArray *words = [textView.text componentsSeparatedByString:@" "];

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:textView.text];

    for (NSString *word in words)
    {
        if ([word hasPrefix:@"#"])
        {
            NSRange matchRange = [textView.text rangeOfString:word];
            [attrString addAttribute:NSForegroundColorAttributeName value:[AppereanceConfiguration defaultTintColor] range:matchRange];
        }
    }
    textView.attributedText = attrString;
}

在iOS7上它运行正常,但在iOS6开始输入时,文本开始垂直向下。例如:如果我想输入单词“Test” - 当我按“T”时它看起来很好,但是当我按下“e”时,它会出现在新行中的“T”之后,依此类推。下一个字母出现在前一个字母下面。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个:

- (void)textViewDidChange:(UITextView *)textView {

[textView setTypingAttributes:{@NSForegroundColorAttributeName:[AppereanceConfiguration defaultTintColor]}];
}

添加条件,每次都不需要创建NSMutableAttributedString。

相关问题