更改默认光标图标UITextView iOS?

时间:2014-06-23 11:37:45

标签: ios objective-c uitextview

我需要更改UITextView的默认光标图标。我想将其更改为圆形图标而不是如下所示的线条。任何帮助表示赞赏。提前谢谢。

enter image description here

2 个答案:

答案 0 :(得分:2)

无法更改textView的原始插入符号。

你应该自己定制一个。为此,请创建UITextView的子类:

@interface CustomTextView()

@property (strong, nonatomic) UIImageView *caretImageView;

@end

@implementation CustomTextView

- (void)awakeFromNib
{
    [super awakeFromNib];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textDidChange:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:self];

    self.caretImageView = // create and customize your caret view
    //...

    [self addSubview:self.caretImageView];

    // Make careImageView to blink every 0.5 secs
    [NSTimer scheduledTimerWithTimeInterval:0.5
                                     target:self
                                   selector:@selector(blink)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)blink
{
    self.caretImageView.alpha = !(BOOL)self.caretImageView.alpha;
}

// Hiding original caret
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return CGRectZero;
}

// Repositioning caretImageView everytime when text did change
- (void)textDidChange:(NSNotification *)note
{
    CGPoint endPoint = // calculate the x and y coords of the text's end.

    CGRect frame = self.caretImageView.frame;
    frame.origin = endPoint;
    self.caretImageView.frame = frame;
}


//
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

@end

答案 1 :(得分:0)

我不确定这是可能的。我不认为Apple公开了在文本视图中绘制光标的代码。