Dou*_*ith 115 objective-c uipickerview uiview ios ios7
我知道这个pickerView:viewForRow:forComponent:reusingView方法,但是当使用view它时,reusingView:如何更改它以使用不同的文本颜色?如果我view.backgroundColor = [UIColor whiteColor];不再使用任何视图显示.
Jon*_*Jon 316
委托方法中有一个更优雅的功能:
Objective-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;
}
Run Code Online (Sandbox Code Playgroud)
如果你想改变选择条的颜色,我发现我必须UIViews在包含UIPickerView间隔35磅的视图中添加2个单独的选择器高度为180.
斯威夫特3:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let string = "myString"
return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white])
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let string = "myString"
return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}
Run Code Online (Sandbox Code Playgroud)
Swift 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])
}
Run Code Online (Sandbox Code Playgroud)
请记住使用该方法时:您不需要实现,titleForRowInComponent()因为它在使用时从未被调用过attributedTitleForRow().
Bor*_*rdz 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;
}
Run Code Online (Sandbox Code Playgroud)
小智 22

小智 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;
}
Run Code Online (Sandbox Code Playgroud)
这个对我有用
pickerView.setValue(UIColor.yellow, forKeyPath: "textColor")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
70485 次 |
| 最近记录: |