我有一个UIPickerView。这有近200个项目,每个项目都有长文本,因此,我想调整UIPickerView的字体大小。我怎么能改变它?有可能的? 谁能帮我?谢谢!
Yuva.M
答案 0 :(得分:89)
您需要在选择器委托中实施pickerView:viewForRow:forComponent:reusingView:
方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
...
}
// Fill the label text here
...
return tView;
}
答案 1 :(得分:26)
在Swift for iOS8中更新,您可以将其添加到您的代理:
org.springframework.security.web.authentication.AuthenticationFailureHandler
答案 2 :(得分:15)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel)
{
pickerLabel = [[UILabel alloc] init];
pickerLabel.font = [UIFont fontWithName:@"SourceSansPro-Semibold" size:16];
pickerLabel.textAlignment=NSTextAlignmentCenter;
}
[pickerLabel setText:[self.data objectAtIndex:row]];
return pickerLabel;
}
答案 3 :(得分:8)
试试这个,它应该有所帮助:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
...
//adjustsFontSizeToFitWidth property to YES
tView.minimumFontSize = 8.;
tView.adjustsFontSizeToFitWidth = YES;
}
// Fill the label text here
...
return tView;
}
// altro modo completo sembrerebbe...
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
}
[pickerLabel setText:[pickerDataArray objectAtIndex:row]];
return pickerLabel;
}
答案 4 :(得分:7)
Swift 4: 更新@Alessandro Ornano回答并避免Force_cast违规错误:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label = UILabel()
if let v = view as? UILabel { label = v }
label.font = UIFont (name: "Helvetica Neue", size: 10)
label.text = dataArray[row]
label.textAlignment = .center
return label
}
答案 5 :(得分:5)
斯威夫特3 | 自动收缩 强>
设置adjustsFontSizeToFitWidth=true
和minimumScaleFactor=0.5
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label: UILabel
if let view = view as? UILabel { label = view }
else { label = UILabel() }
label.text = "My Picker Text"
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 20)
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
return label
}
答案 6 :(得分:2)
目标c
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
[pickerLabel setFont:[UIFont fontWithName:LATO_REGULAR_FONT size:SIZE_SEMIBOLD_FONT]];
pickerLabel.textColor = primaryTextColor;
pickerLabel.textAlignment = NSTextAlignmentCenter;
}
// Fill the label text here
pickerLabel.text = self.dataSourceArray[row];
return pickerLabel;
}
适用于Swift 2.3
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView{
var label = view as! UILabel!
if label == nil {
label = UILabel()
}
label.font = LATO_REGULAR_FONT_17
label.text = dataArray[row] as? String
label.textAlignment = .Center
return label
}
答案 7 :(得分:2)
Swift 4.x
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var label = UILabel()
if let v = view {
label = v as! UILabel
}
label.font = UIFont (name: "Helvetica Neue", size: 10)
label.text = dataArray[row]
label.textAlignment = .center
return label
}
答案 8 :(得分:0)
要设置系统字体,需要做一些不同的操作,如下所示:
let pickerLabel = UILabel()
pickerLabel.font = UIFont.systemFont(ofSize: 20.0, weight: .medium)
您可以选择常规,粗体等任何重量
答案 9 :(得分:0)
干净的Swift解决方案没有强制展开
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = (view as? UILabel) ?? UILabel()
// setup label
return label
}
答案 10 :(得分:0)
大多数答案提供了中心对齐标签的解决方案。我不得不将文本对齐到左侧,并面对事实文本已被截断的事实。向左增加边距为我解决了这个问题。解决方案适用于Swift 5。
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel: UILabel
if let label = view as? UILabel {
pickerLabel = label
} else {
pickerLabel = UILabel()
// Customize text
pickerLabel.font = pickerLabel.font.withSize(20)
pickerLabel.textAlignment = .left
pickerLabel.textColor = UIColor.secondaryLabel
// Create a paragraph with custom style
// We only need indents to prevent text from being cut off
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 12 // May vary
// Create a string and append style to it
let attributedString = NSMutableAttributedString(string: subjects[row])
attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
// Update label's text
pickerLabel.attributedText = attributedString
}
return pickerLabel
}