如何在 iOS 7 的 UIPickerView 中更改文本字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20698547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change text font in UIPickerView in iOS 7?
提问by user3121912
I was able to change my font color but I also need to change font size, how can I accomplish that? Here's my code for chaning the color,
我可以更改字体颜色,但还需要更改字体大小,我该如何实现?这是我改变颜色的代码,
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title = _currencyName[row];
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
return attString;
}
UPDATE: this didn't work:
更新:这不起作用:
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:40]}];
回答by mak
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel* tView = (UILabel*)view;
if (!tView)
{
tView = [[UILabel alloc] init];
[tView setFont:[UIFont fontWithName:@"Helvetica" size:14]];
//[tView setTextAlignment:UITextAlignmentLeft];
tView.numberOfLines=3;
}
// Fill the label text here
tView.text=[wishvalues objectAtIndex:row];
return tView;
}
回答by Richard Bown
Here's the Swift version tested on iOS8:
这是在 iOS8 上测试的 Swift 版本:
Update in Swift for iOS8, you can add this to your delegate:
在 Swift for iOS8 中更新,您可以将其添加到您的委托中:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
var pickerLabel = view as? UILabel;
if (pickerLabel == nil)
{
pickerLabel = UILabel()
pickerLabel?.font = UIFont(name: "Montserrat", size: 16)
pickerLabel?.textAlignment = NSTextAlignment.Center
}
pickerLabel?.text = fetchLabelForRowNumber(row)
return pickerLabel!;
}
回答by Christopher Pickslay
Updated for Swift 4:
为 Swift 4 更新:
public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = view as? UILabel ?? UILabel()
label.font = .systemFont(ofSize: 16)
label.textColor = .white
label.textAlignment = .center
label.text = text(for: row, for: component)
return label
}
回答by the1pawan
You need to implement pickerView:viewForRow:forComponent:reusingView:
method in picker's delegate
您需要pickerView:viewForRow:forComponent:reusingView:
在选择器的委托中实现方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* lbl = (UILabel*)view;
// Customise Font
if (lbl == nil) {
//label size
CGRect frame = CGRectMake(0.0, 0.0, 70, 30);
lbl = [[UILabel alloc] initWithFrame:frame];
[lbl setTextAlignment:UITextAlignmentLeft];
[lbl setBackgroundColor:[UIColor clearColor]];
//here you can play with fonts
[lbl setFont:[UIFont fontWithName:@"Times New Roman" size:14.0]];
}
//picker view array is the datasource
[lbl setText:[pickerViewArray objectAtIndex:row]];
return lbl;
}
回答by Maulik Kundaliya
You can use following code to set font of pickerview..
您可以使用以下代码来设置pickerview的字体..
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *tView = (UILabel *)view;
if (!tView){
tView = [[UILabel alloc] init];
[tView setFont:[UIFont .....]];//set font
// Setup label properties - frame, font, colors etc
...
}
// Fill the label text here
...
return tView;
}
回答by Scott Zhu
Thanks for @Richard Bown
感谢@Richard Bown
Would this be a better answer for Swift?
这对 Swift 来说是一个更好的答案吗?
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
if let titleLabel = view as? UILabel {
titleLabel.text = "Your Text"
return titleLabel
} else {
let titleLabel = UILabel()
titleLabel.font = UIFont.boldSystemFontOfSize(16)//Font you want here
titleLabel.textAlignment = NSTextAlignment.Center
titleLabel.text = "Your Text"
return titleLabel
}
}
回答by Mark Bourke
let textLabel = view as? UILabel ?? {
let label = UILabel()
label.font = UIFont.boldSystemFontOfSize(16)
label.textAlignment = .Center
return label
}()
textLabel.text = Array(componentDataSources[component].keys)[row]
return textLabel
回答by zbMax
I think you have to add NSFontAttributeName
in your list of attributes
and you could use the class method fontWithName:size:
of UIFont
我认为你必须添加NSFontAttributeName
在你的列表attributes
,你可以使用类方法fontWithName:size:
的UIFont