2013-05-23 10 views
12

私はUITextViewを持っています。ユーザーがテキストを入力しているときに、その場でテキストの書式を設定します。私はテキストビューからテキストを取ると、それのからNSAttributedStringを作る:構文の強調表示のような何か...私はUITextViewを使用したいそのためにUITextViewsのテキストを属性付きの文字列に置き換えます。

...

すべてが一つの問題を期待して正常に動作します。この属性付き文字列を編集してtextView.attributedTextとして返します。

これは、ユーザーが入力するたびに発生します。だから私はattributedTextへの編集の前にselectedTextRangeを覚えておき、後でそれを元に戻して、ユーザが前に入力したところで入力を続けることができるようにしなければならない。唯一の問題は、テキストがスクロールするのに十分な長さになると、ゆっくりと入力するとUITextViewが上部にスクロールし始めることです。ここで

は、いくつかのサンプルコードです:

- (void)formatTextInTextView:(UITextView *)textView 
{ 
    NSRange selectedRange = textView.selectedRange; 
    NSString *text = textView.text; 

    // This will give me an attributedString with the base text-style 
    NSMutableAttributedString *attributedString = [self attributedStringFromString:text]; 

    NSError *error = nil; 
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error]; 
    NSArray *matches = [regex matchesInString:text 
            options:0 
             range:NSMakeRange(0, text.length)]; 

    for (NSTextCheckingResult *match in matches) 
    { 
    NSRange matchRange = [match rangeAtIndex:0]; 
    [attributedString addAttribute:NSForegroundColorAttributeName 
          value:[UIColor redColor] 
          range:matchRange]; 
    } 

    textView.attributedText = attributedString; 
    textView.selectedRange = selectedRange; 
} 

は直接CoreTextを使用せずに任意の解決策はありますか?私はUITextViewのテキストの選択などが好きです....

+1

@ナナフ投稿を編集してSOを改善しようとしてくれてありがとう。しかし、[本当に理由はありません](http://meta.stackexchange.com/q/158564/169503)あなたが思っている任意の大胆な強調のために[http://stackoverflow.com/review/suggested-edits/] 2242818)[紹介](http://stackoverflow.com/review/suggested-edits/2247096)[様々な](http:// http://stackoverflow.com/page/subgested-edits/2247096) [投稿](http://stackoverflow.com/review/suggested-edits/2247183)。それは騒音を導入するだけであり、実際には積極的に有害であるとみなすことができる。 –

答えて

33

これは正しい解決策であるとは確信していませんが、うまくいきます。
は、テキストだけをフォーマットする前にスクロール無効と

- (void)formatTextInTextView:(UITextView *)textView 
{ 
    textView.scrollEnabled = NO; 
    NSRange selectedRange = textView.selectedRange; 
    NSString *text = textView.text; 

    // This will give me an attributedString with the base text-style 
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text]; 

    NSError *error = nil; 
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error]; 
    NSArray *matches = [regex matchesInString:text 
             options:0 
             range:NSMakeRange(0, text.length)]; 

    for (NSTextCheckingResult *match in matches) 
    { 
     NSRange matchRange = [match rangeAtIndex:0]; 
     [attributedString addAttribute:NSForegroundColorAttributeName 
           value:[UIColor redColor] 
           range:matchRange]; 
    } 

    textView.attributedText = attributedString; 
    textView.selectedRange = selectedRange; 
    textView.scrollEnabled = YES; 
} 
+0

あなたの答えをありがとう! :) 私は別の解決策を見つけました...私はUITextViewをサブクラス化し、 'scrollRectToVisible:animated:'を上書きし、アニメーションなしでスーパーを呼び出させました。あなたのソリューションは少しスムーズです:) もう一度、どうもありがとう! – Georg

+0

素晴らしい.......... – svmrajesh

+0

あなたの答えをありがとう。それは私の問題を解決するためのアイデアを与えました。 – Karun

0

スウィフト2.0をフォーマットした後、それを有効にします。Sergeys年代に使用され

let myDisplayTxt:String = "Test String" 

    let string: NSMutableAttributedString = NSMutableAttributedString(string: self.myDisplayTxt) 
    string.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 5)) 
    string.addAttribute(String(kCTForegroundColorAttributeName), value: UIColor.redColor().CGColor as AnyObject, range: NSMakeRange(0, 5)) 

    self.sampleTextView.attributedText = string 
4

自分自身に答え、スウィフト2に移植:

func formatTextInTextView(textView: UITextView) { 
    textView.scrollEnabled = false 
    let selectedRange = textView.selectedRange 
    let text = textView.text 

    // This will give me an attributedString with the base text-style 
    let attributedString = NSMutableAttributedString(string: text) 

    let regex = try? NSRegularExpression(pattern: "#(\\w+)", options: []) 
    let matches = regex!.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count)) 

    for match in matches { 
     let matchRange = match.rangeAtIndex(0) 
     attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: matchRange) 
    } 

    textView.attributedText = attributedString 
    textView.selectedRange = selectedRange 
    textView.scrollEnabled = true 
} 
+0

私はこのメソッドを呼び出しますか?実際にはあなたのコードのように同じリンクが必要です。あなたはそのコードを共有できますか? –

関連する問題