Setting Multiline Title To NSButton In Cocoa App

535 Views Asked by At

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 -:

result I wanted result I wanted

1

There are 1 best solutions below

0
On BEST ANSWER

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