我知道pickerView:viewForRow:forComponent:reusingView
方法,但是当使用view
时,它会通过reusingView:
如何将其更改为使用不同的文本颜色?如果我使用view.backgroundColor = [UIColor whiteColor];
,则不再显示任何视图。如何更改iOS 7下UIPickerView中文本的颜色?
回答
有一个在委托方法是更优雅的函数:
目的-C:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title = @"sample title";
NSAttributedString *attString =
[[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
return attString;
}
如果你也想改变选择栏的颜色,我发现我必须添加2个单独的UIViews
到包含012的视图,间隔开的35点为180
夫特3的拾取器高度:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let string = "myString"
return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white])
}
夫特4:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let string = "myString"
return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}
记住当使用方法:你不不需要执行titleForRowInComponent()
,因为在使用attributedTitleForRow()
时从不会调用它。
令人惊叹!这是最好的,应该是选定的正确答案。谢谢。 – user1949873
迄今为止,这是最好的答案,因为它也适用于具有多个组件的选取器视图。 – PKCLsoft
不错的工作。我为使用数组的人添加了一个编辑到你的答案的编辑。如果这个问题让我知道。 – Rick
原始讯息[这里](can I change the font color of the datePicker in iOS7?)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
label.text = [NSString stringWithFormat:@" %d", row+1];
return label;
}
// number Of Components
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// number Of Rows In Component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
return 6;
}
该解决方案适用于包含单个组件的pickerview。如果您有超过1个,标签将与我所能看到的重叠。 – PKCLsoft
我遇到了同样的问题,pickerView使用两个组件。我的解决方案与上述类似,只需进行一些修改。因为我使用两个组件,所以需要从两个不同的数组中抽取数据。
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor blueColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
//WithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 60)];
if(component == 0)
{
label.text = [countryArray objectAtIndex:row];
}
else
{
label.text = [cityArray objectAtIndex:row];
}
return label;
}
,重写UIPickerModelView方法GetAttributedTitle
public override NSAttributedString GetAttributedTitle(UIPickerView picker, nint row, nint component)
{
// change text white
string title = GetTitle (picker, row, component); // get current text from UIPickerViewModel::GetTitle
NSAttributedString ns = new NSAttributedString (title, null, UIColor.White); // null = font, use current font
return ns;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel* pickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 37)];
pickerLabel.text = @"text";
pickerLabel.textColor = [UIColor redColor];
return pickerLabel;
}
夫特4(更新至接受的答案)
extension MyViewController: UIPickerViewDelegate{
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: "your-title-goes-here", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}
}
这将工作:[?我能更改日期选择在iOS7的字体颜色]
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,
pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
label.text = [NSString stringWithFormat:@" %d", row+1];
return label;
}
// total Components
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// total rows In Component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:
(NSInteger)component
{
return 6;
}
看起来像这是一个答案的复制/粘贴。它有什么不同? – Sunil
- 1. 如何在iOS中将UIPickerView文本颜色更改为白色
- 2. 如何在iOS 7中更改UIPickerView中的文本字体?
- 3. 从白色更改状态栏文本颜色iOS 7/Xcode 5
- 4. 更改UIPickerView主体颜色
- 5. 如何在iOS 7的UINavigationBar中更改我的文本的颜色?
- 6. 在iOS 7中,如何更改UIActionSheet中选项的颜色?
- 7. 在iOS 7中更改键盘上方的自定义UIToolbar的颜色(不仅仅是更改文本颜色)
- 8. 如何更改iOS 7中UIActionSheet的颜色?
- 9. 如何更改UIActivityViewController中的文本颜色和图标颜色
- 10. 变化的iOS 7 morenavigationcontroller文本颜色
- 11. 如何更改QxtLabel的文本颜色
- 12. 如何更改文本颜色的IFrame
- 13. 如何更改javascript文本的颜色?
- 14. 如何更改JButton的文本颜色
- 15. 更改iOS 6与iOS 7中的UIImage颜色不同
- 16. Xamarin MvvmCross iOS Bool颜色ValueConverter不更改标签文本颜色
- 17. 如何更改UIPickerView行中各个字符的字体颜色?
- 18. 如何更改iOS 5上表格标题的文本颜色?
- 19. 如何更改IOS的文本链接颜色
- 20. UIPickerView行颜色
- 21. 状态栏文本颜色iOS 7
- 22. 如何更改文本颜色代码?
- 23. PhpStorm:我如何更改文本颜色
- 24. 如何更改disqus文本颜色
- 25. 如何更改CCLabelBMFont文本颜色
- 26. 如何更改tinymce textarea文本颜色
- 27. 如何更改JProgressBar文本颜色
- 28. 如何更改文本颜色在AlertDialog
- 29. 如何更改ActionBar Spinner文本颜色?
- 30. 如何更改微调文本颜色
的可能重复(http://stackoverflow.com/questions/18807940/can-i -change-the-font-color-of-date-date-picker -in-ios7) –
@AaronBrager。在这个问题之后,不要重复你提供的链接。 –