我知道该pickerView:viewForRow:forComponent:reusingView
方法,但是在使用view
它时,reusingView:
如何将其更改为使用不同的文本颜色?如果我使用view.backgroundColor = [UIColor whiteColor];
不再显示任何视图。
问问题
78098 次
8 回答
329
委托方法中有一个更优雅的函数:
目标-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;
}
如果您还想更改选择栏颜色,我发现我必须在UIViews
包含 的视图中添加 2 个单独的UIPickerView
,间隔 35 pts 的选择器高度为 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])
}
斯威夫特 4.2:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let string = "myString"
return NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}
请记住,当您使用该方法时:您不需要实现titleForRowInComponent()
,因为在使用attributedTitleForRow()
.
于 2013-11-06T23:44:34.623 回答
29
原帖在这里:我可以在 iOS7 中更改 datePicker 的字体颜色吗?
- (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;
}
于 2013-10-08T00:46:41.137 回答
23
- 转到故事板
- 选择 PickerView
- 转到身份检查器(第三个选项卡)
- 添加用户定义的运行时属性
- KeyPath = textColor
- 类型 = 颜色
- 值 = [您选择的颜色]
于 2014-05-07T14:18:29.153 回答
7
在 Xamarin 中,覆盖 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;
}
于 2015-05-16T14:51:47.560 回答
7
这个对我有用
pickerView.setValue(UIColor.yellow, forKeyPath: "textColor")
于 2020-01-24T08:21:12.627 回答
2
我在使用两个组件的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;
}
于 2014-03-23T21:20:23.127 回答
2
Swift 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])
}
}
于 2017-12-11T10:13:00.197 回答
1
- (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;
}
于 2016-12-23T05:33:45.403 回答