你能改变UITextView中的光标颜色吗?

Ian*_*nry 6 iphone uikit

是否可以在UITextView中覆盖光标的颜色和自动更正气泡?这是在内置的Notes应用程序中完成的,但我不知道它是否是通过公共方式完成的.

我在任何文档中都找不到任何对此的引用,所以我担心它是UIKeyboard中的私有API集等.我错过了一些明显的东西吗

nac*_*o4d 7

虽然这个问题已经在这里得到解答,但是有一些非常好的(读取私有)方法UITextInputTraits(在iOS5和6b中测试过)我认为读这篇文章的人不是针对AppStore:p

他们之中有一些是:

UITextInputTraits *inputTraits = [_textView textInputTraits];
UIColor *purpleColor = [UIColor purpleColor];
[inputTraits setInsertionPointColor:purpleColor];
[inputTraits setInsertionPointWidth:1]; // Read below note
[inputTraits setSelectionHighlightColor:[purpleColor colorWithAlphaComponent:0.1]];
[inputTraits setSelectionDragDotImage:[UIImage imageNamed:@"CoolHandle"]];
[inputTraits setSelectionBarColor:purple];
Run Code Online (Sandbox Code Playgroud)

insertionPointWidth:显然没有效果.您需要在子类中覆盖caretRectForPosition:(UITextInput协议方法)UITextView.

结果:

几乎可以舔


这些也很有趣:

[inputTraits setAcceptsFloatingKeyboard:NO];
[inputTraits setAcceptsSplitKeyboard:NO];
Run Code Online (Sandbox Code Playgroud)

仔细使用后两个,因为其他文本视图/字段可以接受浮动或拆分键盘,当具有自定义输入特征的文本视图成为第一个响应者时,键盘可能无法正确更新.

要获得完整的方法列表:

- (void)printMethodsOfClass:(Class)class
{
    unsigned int methodCount = 0;
    NSLog(@"%@", NSStringFromClass(class));
    Method *mlist = class_copyMethodList(class, &methodCount);
    for (int i = 0; i < methodCount; ++i){
        NSLog(@"%@", NSStringFromSelector(method_getName(mlist[i])));
    }
    NSLog(@"------------------------------------------------------------");
    free(mlist);
}

[self printMethodsOfClass:[[textview textInputTraits] class]];
Run Code Online (Sandbox Code Playgroud)