自定义UITextView不显示蓝色突出显示

时间:2014-05-05 21:18:33

标签: ios uitextview highlight uimenuitem

我创建了UITextView的子类来添加自定义UIMenuItem。问题是,当我按自定义操作以显示自定义项时,文本不会突出显示。有什么想法吗?

enter image description here

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

谢谢!

1 个答案:

答案 0 :(得分: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