在我的项目中,我需要在其titleLabel和NSAttributedString中显示一个UIButton,其中包含两行文本。它在iOS8中运行良好,但在iOS7中无法正常工作,我可以在选中按钮后看到两个带衬里的文本。
这是我使用的代码:
calendarBtn= [UIButton buttonWithType:UIButtonTypeCustom];
calendarBtn.frame=CGRectMake(50 ,10, 45, 45);
[calendarBtn addTarget:self action:@selector(calendarBtnclicked:)forControlEvents:UIControlEventTouchUpInside];
calendarBtn.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
calendarBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
calendarBtn.titleLabel.numberOfLines=2;
[calendarBtn setTitleColor:BLACK_COLOR forState:UIControlStateNormal];
NSString *dateString=@"27\nApr";
dateString=[dateString uppercaseString];
[calendarBtn setTitle:dateString forState:UIControlStateNormal];
// Font For 27
UIFont *ddFont = [UIFont fontWithName:ArkitechLight size:17.0f];
NSMutableDictionary *ddDict = [NSMutableDictionary dictionaryWithObject:ddFont forKey:NSFontAttributeName];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:3];
style.alignment=NSTextAlignmentCenter;
[style setLineBreakMode:NSLineBreakByWordWrapping];
[ddDict addEntriesFromDictionary:@{NSParagraphStyleAttributeName : style,}];
NSMutableAttributedString *ddStr = [[NSMutableAttributedString alloc] initWithString:[dateString substringToIndex:2] attributes:ddDict];
// Font For Apr
UIFont *mmmFont = [UIFont fontWithName:ArkitechLight size:11.5f];
NSDictionary *mmmDict = [NSDictionary dictionaryWithObject:mmmFont forKey:NSFontAttributeName];
NSMutableAttributedString *mmmString = [[NSMutableAttributedString alloc]initWithString:[dateString substringFromIndex:2] attributes:mmmDict];
[ddStr appendAttributedString:mmmString];
calendarBtn.titleLabel.attributedText=ddStr;
答案 0 :(得分:3)
您需要使用此行设置属性标题
[calendarBtn setAttributedTitle: ddStr forState:UIControlStateNormal];
而不是
calendarBtn.titleLabel.attributedText=ddStr;
希望有所帮助:)