且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Cocoa App中将多行标题设置为NSButton

更新时间:2023-12-03 15:21:28

我已经满足了要求,通过对NSButton进行子类化,然后检查标题的字符串长度来相应地将字符串划分为两行来显示.

I have fulfilled the requirement by subclassing the NSButton and then checked the title's string length to divide string accordingly to show it in two line.

NSArray *arrStr = [str componentsSeparatedByString:@" "];
NSString *strTemp = @"";
    if(arrStr.count>1){
        strTemp = [NSString stringWithFormat:@"%@\n%@", arrStr.firstObject, [str stringByReplacingOccurrencesOfString:arrStr.firstObject withString:@""]];
}
    else{
        strTemp = str;
}            
NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:strTemp];