Setting Multiline Title To NSButton In Cocoa App

不打扰是莪最后的温柔 提交于 2019-12-13 08:17:42

问题


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


来源:https://stackoverflow.com/questions/51593418/setting-multiline-title-to-nsbutton-in-cocoa-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!