【问题标题】:change default cursor icon UITextView iOS?更改默认光标图标 UITextView iOS?
【发布时间】:2014-06-23 11:37:45
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c uitextview


    【解决方案1】:

    无法更改 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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2012-07-16
        • 2023-01-14
        • 2012-04-30
        • 2020-07-16
        • 1970-01-01
        • 2017-03-09
        • 2011-05-02
        • 2016-07-10
        • 2014-07-06
        相关资源
        最近更新 更多