UITextView tidak mengaktifkan textViewDidChange pada perubahan atribut? (UITextView not firing textViewDidChange on attribute change?)


問題描述

UITextView tidak mengaktifkan textViewDidChange pada perubahan atribut? (UITextView not firing textViewDidChange on attribute change?)

I have a UITextView configured with a delegate.  I've enabled editing attributes by setting allowsEditingTextAttributes to YES.

When the user types a character in the text view, the delegate receives the  textViewDidChange: message.

But when the user changes an attribute (such as making a selection and tapping Bold or Italic), no textViewDidChange: message.

The documentation specifically says that textViewDidChange: should receive a message when the user changes attributes:

  

Tells the delegate that the text or attributes in the specified text view were changed by the user.

But it's not working for me.  What am I missing here?

‑‑‑‑‑

參考解法

方法 1:

I tested this scenario in iOS 6 and had the exact same outcome: attribute changes from the "select" pop‑up did not trigger the  textViewDidChange: method.  It seems that this is a bug or the documentation needs to clarify what type of attribute change would trigger this event.

A possible workaround is to implement the textViewDidChangeSelection: method.  It gets called whenever a selection is made (which the user would have to do before changing an attribute).  Check to see if the selectedRange.length is > 0 (which would mean an actual word has been selected, instead of just moving the cursor around), and save that selectedRange.  Once the length is zero again, it means they deselected the item.  At that time, you could take the previous range and work with the text.

‑ (void)textViewDidChangeSelection:(UITextView *)textView
{
    static BOOL rangeSet = NO;
    static NSRange mySelectedRange;
    if( textView.selectedRange.length > 0 && !rangeSet )
    {
        mySelectedRange = textView.selectedRange;
        rangeSet = YES;
    }
    else if( textView.selectedRange.length == 0 && rangeSet)
    {
        // Work with text
        NSLog(@"Working with previously select text: %d, %d", mySelectedRange.location, mySelectedRange.length);
    }
}

方法 2:

In documentation it has also been mentioned that

This method is not called in response to programmatically initiated changes

So if you are setting bold or italic programatically so it will not invoke this delegate method

方法 3:

Please check if you've set UITextViewDelegate in in your .h file. Also you'll have to `make 

yourTextView.delegate = self

So that your Text View will give control to your current class.

方法 4:

I got around this by subclassing UITextView, overriding the toggleBoldface:toggleItalics: and toggleUnderline: methods, and calling [self.delegate textViewDidChange:self] from those methods. This answer helped me figure that out.

(by stevexJ ShapiroSunil PandeyRushiarlomedia)

參考文件

  1. UITextView not firing textViewDidChange on attribute change? (CC BY‑SA 3.0/4.0)

#iphone #uitextview






相關問題

iPhone 3.0 MPMoviePlayerController 在屏幕上顯示視頻播放器的等待時間長有問題嗎? (Problem with iPhone 3.0 MPMoviePlayerController long wait time to display the video player on screen?)

將 NavigationBar 背景更改為圖像 (Changing the NavigationBar background to image)

如何將以下行添加到我的 plist 文件中? (How can I add the following lines to my plist file?)

iPhone SDK:檢查 UITableView 中每個對象的 ID 號並相應地更改徽章號 (iPhone SDK: Checking the ID number for each object in a UITableView and change the badge number accordingly)

無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)

嵌套 XML 示例? (Nested XML Example?)

提交應用程序 - 添加本地化,但我的語言未列在組合框中? (Submitting app - Add Localizations, but my language is not listed in the combo box?)

如何從移動瀏覽器直接訪問移動設備的硬件功能 (How to gain direct access to the hardware capabilities of mobile devices from mobile browsers)

Cocos2d 中的粒子碰撞檢測 (Collision detection with particles in Cocos2d)

捕捉返回按鈕導航事件 (Catching back button navigation event)

iphone初學者問題:畫一個矩形。我究竟做錯了什麼? (Beginner iphone question: drawing a rectangle. What am I doing wrong?)

帶有 UITableView 的 UIPopoverController (UIPopoverController with UITableView)







留言討論