0
我想在將文本字段留爲空白時將佔位符顏色更改爲紅色(例如,第一個文本字段的佔位符將在我離開第一個文本字段時更改爲閱讀並開始在第二個文本字段中輸入..)在驗證文本字段時更改佔位符顏色
我想在將文本字段留爲空白時將佔位符顏色更改爲紅色(例如,第一個文本字段的佔位符將在我離開第一個文本字段時更改爲閱讀並開始在第二個文本字段中輸入..)在驗證文本字段時更改佔位符顏色
您需要使用NSAttributedString
。驗證失敗後,更改字符串的顏色。
-(void)methodForValidation{
... //code for validation
if(validationFail){
NSMutableAttributedString *attriButedString = [[NSMutableAttributedString alloc]initWithString:yourStringHere"];
[attriButedString addAttribute:NSForegroundColorAttributeName
value:[NSColor redColor]
range:NSMakeRange(0, lengthOfYourString)];
[[self.yourTextField cell] setPlaceholderAttributedString:attriButedString];
}
}
你可以在這裏找到答案:http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color – ZeMoon
您需要使用'NSAttributedString'。驗證失敗後,更改字符串的顏色。你可以從[這裏](http://stackoverflow.com/questions/20423541/placing-placeholder-in-textfield/20423592#20423592) –