在 Cocoa 应用程序中将多行标题设置为 NSButton [英] Setting Multiline Title To NSButton In Cocoa App

查看:21
本文介绍了在 Cocoa 应用程序中将多行标题设置为 NSButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解将标题设置为 NSButton 的 NSButton 指南,没有冒犯,但在我的情况下应该满足要求.我想用两行显示 NSButton 标题.

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

推荐答案

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

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

这篇关于在 Cocoa 应用程序中将多行标题设置为 NSButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆