【问题标题】:Vertical text in UITextView attributed textUITextView 属性文本中的垂直文本
【发布时间】:2013-12-13 13:52:45
【问题描述】:

我有一个 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”下面,依此类推。下一个字母出现在前一个字母的下方。

我该如何解决这个问题?

【问题讨论】:

    标签: ios text uitextview nsattributedstring vertical-text


    【解决方案1】:

    试试这个:

    - (void)textViewDidChange:(UITextView *)textView {
    
    [textView setTypingAttributes:{@NSForegroundColorAttributeName:[AppereanceConfiguration defaultTintColor]}];
    }
    

    添加条件并且不需要每次都创建一个NSMutableAttributedString。

    【讨论】:

      猜你喜欢
      • 2013-04-26
      • 2012-09-17
      • 2013-06-06
      • 2023-03-09
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      相关资源
      最近更新 更多