如何在iOS中将UIPickerView文本颜色更改为白色
问题描述:
我想知道是否有人知道如何将UIPickerView文本颜色更改为白色。我的代码如下:如何在iOS中将UIPickerView文本颜色更改为白色
代码:
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
[self.pickerView setValue:[UIColor whiteColor] forKey:@"textColor"];
}
任何想法?
答
使用此代码
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *tView = (UILabel*)view;
if (!tView)
{
tView = [[UILabel alloc] init];
[tView setTextColor:[UIColor whiteColor]];
[tView setFont:[UIFont fontWithName:@"font-name-here" size:15]];
[tView setTextAlignment:NSTextAlignmentCenter];
}
// Fill the label text here
return tView;
}
答
有一个委托方法:
- (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;
}
答
有一个委托方法,通过它可以实现期望的输出。
的Objective-C:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *strTitle = @"YourTitle";
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:strTitle attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
return attString;
}
斯威夫特:
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let strTitle = "YourTitle"
let attString = NSAttributedString(string: strTitle, attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
return attString
}
希望这将帮助你:)
- (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 clearColor]; label.textColor = [UIColor WhiteColor]; label.font = [UIFont fontWithName:@“HelveticaNeue-Bold”size:18]; label.text = [NSString stringWithFormat:@“%c”,65 + row]; // ASCII 65是“A” 返回标签; } 明白了谢谢 – Nimmy