开发者

Can you change the cursor color in a UITextView?

Is it possible to override the color of the cursor and autocorrection bubbles in a UITextView? This is done in the built-in Notes application, but I don't 开发者_如何学Goknow if it was done by public means or not.

I can't find any reference to this in any documentation, so I'm worried that it's a private API set in the UIKeyboard or something. Am I missing something obvious?


Although this question is already answered here are some terrific (read private) methods of UITextInputTraits (tested in iOS5 and 6b :) I assume people reading this are not targeting the AppStore :p

Some of them are:

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];

insertionPointWidth: apparently has no effect. You need to override caretRectForPosition: (method of UITextInput protocol) in your UITextView subclass.

Result:

Can you change the cursor color in a UITextView?


Also these are interesting:

[inputTraits setAcceptsFloatingKeyboard:NO];
[inputTraits setAcceptsSplitKeyboard:NO];

Use these last two carefully because other text views/fields could accept floating or split keyboards and the keyboard might not update correctly when your textview with the customized input traits becomes the first responder.

To get a full method list:

- (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]];


On iOS 7+ this is now accomplished by setting the tintColor property on your text field.


I found this link that described the "hidden" UIKeyboard magic. Looks like UITextTraits has a CaretColor property. Sadly, I don't think messing with this would make it through Apple's review process. Unless they didn't notice? It's possible...

http://ericasadun.com/iPhoneDocs112/interface_u_i_keyboard.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜