Setting Multiline Title To NSButton In Cocoa App

2019-09-20 10:15发布

问题:

I understand the NSButton guidelines for setting title to NSButton, No offence but the requirement should be fulfilled in my case. I want to show NSButton title in two lines.

NSButton *btn = [[NSButton alloc] init];
[btn setTitle:@"multiple line text if longer title"];

the result I wanted was kind of below -:

回答1:

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];