Ios7 带有多行标题标签的UIButton,带有NSAttributedString不';无法在iOS 7中工作,但可以在iOS 8中工作

Ios7 带有多行标题标签的UIButton,带有NSAttributedString不';无法在iOS 7中工作,但可以在iOS 8中工作,ios7,uibutton,nsattributedstring,Ios7,Uibutton,Nsattributedstring,在我的项目中,我需要显示一个UIButton,该UIButton的标题标签中有两行文本,并带有NSAttribute字符串。它在iOS8中工作正常,但在iOS7中不工作,我可以在选中的按钮后看到两行文本 这是我正在使用的代码: calendarBtn= [UIButton buttonWithType:UIButtonTypeCustom]; calendarBtn.frame=CGRectMake(50 ,10, 45, 45); [calendarBtn addTarget:self act

在我的项目中,我需要显示一个UIButton,该UIButton的标题标签中有两行文本,并带有NSAttribute字符串。它在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;

您需要使用此行来设置属性标题

[calendarBtn setAttributedTitle: ddStr forState:UIControlStateNormal];
而不是

calendarBtn.titleLabel.attributedText=ddStr;
希望有帮助:)