改变NSButton字(title)的颜色

NSButton颜色设置
本文介绍了一种使用NSAttributedString为NSButton设置标题颜色的方法,并提供了解决内存泄漏问题的方案。
转自我的简书:http://www.jianshu.com/p/a9e86b79a2d4

NSButton不能像UIButton那样简单的修改title的颜色,或者说NSButton不能像UIButton那样做很多事,使用起来真的很不方便。
经过大量研究测试,终于发现一种修改文字颜色的相对来说比较简单的方式-用NSAttributedString;不说了,代码如下:

// 创建段落样式,主要是为了设置居中
NSMutableParagraphStyle pghStyle = [[NSMutableParagraphStyle alloc] init];
pghStyle.alignment = NSTextAlignmentCenter;
// 创建Attributes,设置颜色和段落样式
NSDictionary dicAtt = @{NSForegroundColorAttributeName: [NSColor whiteColor], NSParagraphStyleAttributeName: pghStyle};
// 创建NSAttributedString赋值给NSButton的attributedTitle属性
btn.attributedTitle = [[NSAttributedString alloc] initWithString:@"解绑" attributes:dicAtt];

4行代码即可,比起一来就说什么重写drawRect的简单多了!

但是,经过实际操作发现该方法会导致内存泄漏,实在不知是什么原因导致的,私下猜测是Apple的bug吧。但是也不是没有解决方法,经过大量测试,发现以下方法可以解决内存泄漏的问题。

// 创建段落样式,主要是为了设置居中
    NSMutableParagraphStyle *pghStyle = [[NSMutableParagraphStyle alloc] init];
    pghStyle.alignment = NSTextAlignmentCenter;
    // 创建Attributes,设置颜色和段落样式
    NSDictionary *dicAtt = @{NSForegroundColorAttributeName: [NSColor whiteColor], NSParagraphStyleAttributeName: pghStyle};
    // 创建NSAttributedString赋值给NSButton的attributedTitle属性;必需从NSButton.attributedTitle创建,否则会有内存泄漏;
    // 给NSButton先赋值一个字符串,为的是后面替换,如果NSButton的title是空字符串的话,也会内存泄漏
    btn.title = @" ";  // 这里的字符串有一个空格
    // 用NSButton.attributedTitle属性创建一个NSMutableAttributedString对象
    NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc] initWithAttributedString:btn.attributedTitle];
    // 替换文字
    [attTitle replaceCharactersInRange:NSMakeRange(0, 1) withString:@"解绑"];
    // 添加属性
    [attTitle addAttributes:dicAtt range:NSMakeRange(0, 2)];
    // 赋值给NSButton.attributedTitle属性,不会再有内存泄漏
    btn.attributedTitle = attTitle;

经过大量测试,发现只有这种方法不会内存泄漏,我也是醉了。

帮我分析一下下面的自定义,我想用这个当我的侧边栏收起按钮:// // TPBButton.h // MacVms // // Created by LSL on 2/17/25. // #import <Foundation/Foundation.h> #import "TPBDesign.h" NS_ASSUME_NONNULL_BEGIN typedef NS_ENUM(NSInteger, TPBButtonStyle) { /// 默认,圆角矩形,radius为TPBDesign.button.roundRectCornerRadius TPBButtonStyleRoundRect = 0, /// 圆角,radius为height的一半 TPBButtonStyleOval, /// iOS系统样式,无圆角 TPBButtonStyleSquare, }; // 按钮当前交互状态,用于管理样式表现 typedef NS_ENUM(NSInteger, TPBButtonStatus) { /// 默认状态 TPBButtonStatusNormal, /// 悬浮状态 TPBButtonStatusHover, }; typedef void(^TPBButtonStatusChangeCallback)(TPBButtonStatus status); @class TPBButton; @interface TPBButton : TPBBaseControl /// 按钮边框样式 @property (nonatomic, assign) TPBButtonStyle style; /// 设置Loading时是否禁用按钮 @property (nonatomic, assign) BOOL isEnabledWhenLoading; /// 按钮圆角,如果设置该值且值大于0,则会覆盖按钮样式的默认值 @property (nonatomic, assign) CGFloat cornerRadius; /// 按钮四周是否有填充 @property (nonatomic, assign) CGFloat hasPadding; /// 按钮Title @property (nonatomic, nullable, copy) NSString *title; /// 按钮Title体 @property (nonatomic, nullable, strong) NSFont *titleFont; /// 边框颜色 @property (nonatomic, nullable, strong) NSColor *strokeColor; /// 按钮是否能触发选中 @property (nonatomic, assign) BOOL selectable; /// 按钮当前是否被选中(selectable = yes时才生效,否则恒为NO) @property (nonatomic, assign) BOOL selected; /* 各种状态下的图标、文颜色、背景色设置 */ /// 按钮Title颜色 @property (nonatomic, nullable, strong) NSColor *titleColor; /// 按钮图标 @property (nonatomic, nullable, strong) NSImage *iconImage; /// 按钮背景色 @property (nonatomic, nullable, strong) NSColor *fillColor; /// loading动画的颜色 @property (nonatomic, nullable, strong) NSColor *loadingColor; /// 禁用时图标 @property (nonatomic, nullable, strong) NSImage *disableIconImage; /// 禁用时图标色 @property (nonatomic, nullable, strong) NSColor *disableIconImageColor; /// 禁用背景色 @property (nonatomic, nullable, strong) NSColor *disableFillColor; /// 禁用文色 @property (nonatomic, nullable, strong) NSColor *disableTitleColor; /// 悬浮时图标 @property (nonatomic, nullable, strong) NSImage *hoverIconImage; /// 悬浮时图标色 @property (nonatomic, nullable, strong) NSColor *hoverIconImageColor; /// 悬浮时文色 @property (nonatomic, nullable, strong) NSColor *hoverTitleColor; /// 悬浮时按钮颜色 @property (nonatomic, nullable, strong) NSColor *hoverColor; /// 选中时图标 @property (nonatomic, nullable, strong) NSImage *selectedIconImage; /// 选中时图标色 @property (nonatomic, nullable, strong) NSColor *selectedIconImageColor; /// 选中时文色 @property (nonatomic, nullable, strong) NSColor *selectedTitleColor; /// 选中时按钮颜色 @property (nonatomic, nullable, strong) NSColor *selectedFillColor; /// 点击事件回调 @property (nonatomic, nullable, copy) TPBAppVoidCallback onClickCallback; @property (nonatomic, nullable, copy) void(^onClickCallWithEvent)(NSEvent *); /// 状态改变回调 @property (nonatomic, nullable, copy) TPBButtonStatusChangeCallback statusChangeCallback; @property (nonatomic, nullable, copy) TPBButtonStatusChangeCallback focusChangeBlk; /// 返回按钮当前是否是加载状态 @property (nonatomic, readonly, assign) BOOL isLoading; - (instancetype)init; - (instancetype)initWithStyle:(TPBButtonStyle)style; - (instancetype)initWithStyle:(TPBButtonStyle)style title:(nullable NSString *)title; + (instancetype)buttonWithStyle:(TPBButtonStyle)style; + (instancetype)buttonWithStyle:(TPBButtonStyle)style title:(nullable NSString *)title; /// 进入Loading状态 - (void)startLoading; /// 退出Loading状态 - (void)stopLoading; @end @interface TPBButton (TPBExpanded) /// 纯文按钮 + (TPBButton *)cardButtonWithTitle:(nullable NSString *)title; /// 纯图标按钮 + (TPBButton *)iconButtonWithImage:(nullable NSImage *)image; /// 文 + 边框按钮 + (TPBButton *)lineButtonWithTitle:(nullable NSString *)title; @end NS_ASSUME_NONNULL_END
最新发布
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值