0

カスタム UIMenuItem を追加するために、UITextView のサブクラスを作成しました。問題は、カスタム アクションを押してカスタム アイテムを表示すると、テキストが強調表示されないことです。何か案が?

ここに画像の説明を入力

ActionsTextView.h

#import <UIKit/UIKit.h>

@protocol ActionsDelegate <NSObject>

- (void)addDreamSignalWithText:(NSString *)text range:(NSRange)range;

@end

@interface ActionsTextView : UITextView

#pragma mark - Delegate
@property IBOutlet id<ActionsDelegate>actionsDelegate;

#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender;

@end

ActionsTextView.m

#import "ActionsTextView.h"

@implementation ActionsTextView

- (BOOL)canBecomeFirstResponder {

    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

    if (action == @selector(addDreamSignalAction:)) {
        return YES;
    }

    return NO;

}

#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender {

    if ([_actionsDelegate respondsToSelector:@selector(addDreamSignalWithText:range:)]) {
        [_actionsDelegate addDreamSignalWithText:[self.text substringWithRange:self.selectedRange]
                                           range:self.selectedRange];
    }

    // Deselect text
    self.selectedTextRange = nil;

}


@end

ありがとう!!

4

1 に答える 1

0

rmaddy のおかげで、解決策が見つかりました。コードは次のとおりです。

ActionsTextView.h

#import <UIKit/UIKit.h>

@protocol ActionsDelegate <NSObject>

- (void)addDreamSignalWithText:(NSString *)text range:(NSRange)range;

@end

@interface ActionsTextView : UITextView

#pragma mark - Delegate
@property IBOutlet id<ActionsDelegate>actionsDelegate;

#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender;

#pragma mark - Notifications
- (void)menuControllerWillShow:(NSNotification *)notification;

@end

ActionsTextView.m

#import "ActionsTextView.h"

@implementation ActionsTextView

- (BOOL)canBecomeFirstResponder {

    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {

    if (action == @selector(addDreamSignalAction:)) {
        return YES;
    }

    return NO;

}

#pragma mark - Methods
- (void)addDreamSignalAction:(id)sender {

    if ([_actionsDelegate respondsToSelector:@selector(addDreamSignalWithText:range:)]) {
        [_actionsDelegate addDreamSignalWithText:[self.text substringWithRange:self.selectedRange]
                                           range:self.selectedRange];
    }

    // Deselect text
    self.selectedTextRange = nil;

}

#pragma mark - Notifications
- (void)menuControllerWillShow:(NSNotification *)notification {

    if (self.selectedRange.length == 0) {

        [self select:self];

    }

}

@end
于 2014-05-05T21:44:37.360 に答える