2012-03-15 95 views
-1

它在UITextView和UITextField中出現 - 閃爍的藍色線條向您顯示您鍵入的任何東西將被鍵入。我見過一些應用程序改變了這種顏色,並不知道如何做到這一點。我如何改變它的顏色?如何更改文字輸入指示器的顏色?

+1

可能重複【如何改變光標顏色的文本框?](http://stackoverflow.com/questions/2190352/how-to-change-the-color-of-the-cursor-in-the-textfield) – 2012-03-15 19:18:26

回答

2
[[textField textInputTraits] setValue:[UIColor redColor] forKey:@"insertionPointColor"]; 
+5

但請注意,通過上面的代碼,你可以得到你的解決方案,但「 – 2012-12-22 20:47:44

+0

感謝@ MohitNigam我想知道,除了這個解決方案,有什麼好辦法來改變iOS6的textfield光標顏色? – 2014-07-14 06:29:42

0

沒有記錄的方式來改變指針的顏色。 Hovewer,有一個沒有記錄的 textField的屬性,即textInputTraits,這是一個UITextInputTraits實例。

您可以嘗試此代碼以使其正常工作,但是您的應用可能在審覈過程中被拒絕。

[[textField textInputTraits] setValue:[UIColor redColor] forKey:@"insertionPointColor"]; 

https://stackoverflow.com/a/4273440/736649

8

採取IOS 7中,可以簡單的設置tintColor,它改變顏色用於視圖和子視圖的所有有源元件,包括光標顏色:

_textField.tintColor = [UIColor redColor]; 
1

試試這個:

[[textField valueForKey:@"textInputTraits"] setValue:CUSTOM_COLOR forKey:@"insertionPointColor"]; 

儘管它似乎是無證的,但它的工作原理。坦率地說,你在這裏不使用任何 私有方法 - 只有鍵值編碼是合法的。

P.S.昨天我的新應用出現在AppStore中,沒有任何問題。當我使用KVC更改一些只讀屬性(如navigatonBar)或私有ivars時,這不是第一種情況。

-1

更改着色顏色(的iOS 7)

Change the tint color in the nib

或做編程

myTextField.tintColor = [的UIColor blackColor];

+0

不起作用 – 2013-12-31 13:46:27

+0

ID似乎不起作用。 – 2014-04-04 10:24:59

0

對於iOS 10 SDK,只有指標和Objective-C(總有一天會錯過的)工作的解決方案是:

for (UIView *subView in self.sBar.subviews){ 
    for (UIView *ndLevelSubView in subView.subviews){ 

     if ([ndLevelSubView isKindOfClass:[UITextField class]]) 
     { 
      UITextField * sbTextField = (UITextField *)ndLevelSubView; 
      [[sbTextField valueForKey:@"textInputTraits"] setValue:[UIColor n2pMainPurpleColor] forKey:@"insertionPointColor"]; 
      break; 
     } 

    } 

}