2013-05-23 31 views
12

Tôi có một số UITextView và khi người dùng nhập văn bản vào đó, tôi muốn định dạng văn bản khi đang di chuyển. Một cái gì đó giống như làm nổi bật cú pháp ...Thay thế văn bản UITextViews bằng chuỗi được phân bổ

Cho rằng tôi muốn sử dụng UITextView ...

Tất cả mọi thứ hoạt động tốt mong đợi một vấn đề: Tôi lấy văn bản từ độ xem văn bản và thực hiện một NSAttributedString từ của nó. Tôi thực hiện một số chỉnh sửa đối với chuỗi được phân bổ này và đặt lại thành textView.attributedText.

Điều này xảy ra mỗi khi người dùng nhập. Vì vậy, tôi phải nhớ số selectedTextRange trước khi chỉnh sửa theo số attributedText và đặt lại sau để người dùng có thể tiếp tục nhập tại địa điểm mà anh ấy đã nhập trước đó. Vấn đề duy nhất là khi văn bản đủ dài để yêu cầu cuộn, UITextView giờ đây sẽ bắt đầu cuộn lên trên cùng nếu tôi nhập từ từ.

Dưới đây là một số mẫu mã:

- (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; 
} 

Có giải pháp nào mà không sử dụng CoreText trực tiếp? Tôi thích khả năng chọn văn bản và số lượng của UITextView s ....

+1

@ NANNAV Cảm ơn bạn đã nỗ lực cải thiện SO bằng cách chỉnh sửa bài đăng. Tuy nhiên, [thực sự không có lý do] (http://meta.stackexchange.com/q/158564/169503) cho sự nhấn mạnh đậm đặc tùy ý bạn có vẻ [đến] (http://stackoverflow.com/review/suggested-edits/ 2242818) [giới thiệu] (http://stackoverflow.com/review/suggested-edits/2247096) [trong] (http://stackoverflow.com/review/suggested-edits/2248401) [khác nhau] (http: // stackoverflow.com/review/suggested-edits/2248276) [bài đăng] (http://stackoverflow.com/review/suggested-edits/2247183). Nó chỉ giới thiệu tiếng ồn và thực sự có thể được xem là có hại. –

Trả lời

33

Tôi không chắc rằng đây là giải pháp đúng, nhưng nó hoạt động.
Chỉ cần vô hiệu hóa di chuyển trước khi định dạng văn bản và kích hoạt nó sau khi định dạng

- (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

Cảm ơn câu trả lời của bạn !!! Trong khi chờ đợi, tôi tìm thấy một giải pháp khác ... Tôi chỉ phân lớp UITextView và ghi đè lên 'scrollRectToVisible: animated:' và để nó gọi super không có hình động ... Giải pháp của bạn có vẻ hơi mượt mà hơn :) Một lần nữa, cảm ơn rất nhiều! – Georg

+0

tuyệt vời .......... – svmrajesh

+0

Cảm ơn câu trả lời của bạn. Nó đã cho tôi một ý tưởng để giải quyết vấn đề của tôi. – Karun

0

Swift 2.0:

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

Dùng Sergeys của trả lời bản thân mình và chuyển nó vào Swift 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

Khi tôi gọi phương thức này? Trên thực tế tôi muốn giống như mã của bạn nhưng như liên kết. Bạn có thể chia sẻ mã đó không. –