Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change the cursor color in a UITextView?

Tags:

uikit

iphone

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 know 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?

like image 698
Ian Henry Avatar asked Dec 03 '09 19:12

Ian Henry


3 Answers

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:

Almost lickable


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]];
like image 84
nacho4d Avatar answered Oct 17 '22 15:10

nacho4d


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

like image 45
i_am_jorf Avatar answered Oct 17 '22 14:10

i_am_jorf


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

like image 26
Ian Henry Avatar answered Oct 17 '22 15:10

Ian Henry