【问题标题】:Extract first word from UITextView从 UITextView 中提取第一个单词
【发布时间】:2010-01-03 21:32:42
【问题描述】:

在我的应用程序中,我有一个作为标题的 UILabel 和一个作为描述的 UITextView,我希望标题 UILabel 是输入到 UITextView 中的第一个单词。有人知道怎么做吗?

【问题讨论】:

    标签: iphone uikit nsstring uitextview


    【解决方案1】:

    如果我能很好地理解您的问题,这段代码可能会解决问题:

    - (void)textViewDidChange:(UITextView *)textView
    {
        NSString *text = _textView.text;
        NSArray *elements = [text componentsSeparatedByString:@" "];
        if ([elements count] > 0)
        {
            _label.text = [elements objectAtIndex:0];
        }
    }
    

    【讨论】:

    • 这适用于英语和其他一些使用空格分隔单词的语言,但对许多其他语言无效。要以“正确”的方式执行此操作,请使用 CFStringTokenizer。
    猜你喜欢
    • 2018-04-05
    • 2021-03-02
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多