[Cocoa]_[初级]_[NSButton之改变title标题的颜色]
来源:互联网 发布:软件设计师官网 编辑:程序博客网 时间:2024/05/21 16:22
场景:用于布局控件,根据界面的需要设置不同颜色的title。之前一直进入一个误区,直接自定义NSButton控件,其实这个方法是不好的,会使title重绘多次,复用性不高。
后来发现直接自定义NSButtonCell,可以只对title进行处理就可以了,而不必影响NSButton的其他属性。
下面是自定义NSButtonCell的方法。
MqjColorTitleButtonCell.h#import <Cocoa/Cocoa.h>@interface MqjColorTitleButtonCell : NSButtonCell@endMqjColorTitleButtonCell.m
<pre name="code" class="objc">#import "MqjColorTitleButtonCell.h"@implementation MqjColorTitleButtonCell- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView{ NSSize titleSize = [title size]; //居中显示 CGFloat titleY = frame.origin.y + (frame.size.height - titleSize.height)/2; NSRect rectTitle = frame; rectTitle.origin.y = titleY; NSMutableAttributedString *titleStr =[[NSMutableAttributedString alloc] initWithAttributedString:title]; [titleStr addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0, titleStr.length)]; [titleStr drawInRect:rectTitle]; [titleStr release]; return frame;}@end
直接把NSButton
中的NSBUttonCell的Class改为MqjColorTitleButton,然后其他操作不变。 0 0
- [Cocoa]_[初级]_[NSButton之改变title标题的颜色]
- [Cocoa]_[初级]_[鼠标移动到NSButton上面和移开NSButton时,控件背景颜色的变化]
- [Cocoa]_[初级]_[如何绘制带颜色的文本]
- [Cocoa]_[初级]_[NSThread的使用]
- [Cocoa]_[初级]_[Cocoa入门级界面开发之界面初级程序编辑的步骤和实现方法]
- [Cocoa]_[初级]_[NSTableView之自定义NSImageButtonCell]
- [Cocoa]_[初级]_[自定义的界面上绘制图片]
- [Cocoa]_[初级]_[vcf文件的生成和导出]
- [Cocoa]_[初级]_[自定义NSTableHeaderView]
- [Cocoa]_[初级]_[NSTableView添加背景图片]
- [Cocoa]_[初级]_[如何复制NSView]
- [Cocoa]_[初级]_[文件拖放操作]
- [Cocoa]_[初级]_[Cocoa中对GIF动画分解成一帧帧的图片]
- [Cocoa]_[初级]_[Cocoa设计模式总结]
- 375_改变图片的填充颜色
- [Cocoa]_[初级]_[使用异步线程加载图片文件实现进度条的更新]
- [Cocoa]_[初级]_[NSString 、NSImage、NSData、std::string之间的转换]
- [Cocoa]_[初级]_[NSTableView--数据操作和表格操作要注意的问题]
- Fiddler 教程
- The Exchange of Items (141 - ZOJ Monthly, July 2015 - E 最小费用最大流)
- Native library not found! Please copy ibbdpush_v2_3.so into your project!
- oracle 创建表性别字段取'男'或者'女',默认为'男'
- 嵌入式系统基础知识------之嵌入式系统的性能评价(1.5 )
- [Cocoa]_[初级]_[NSButton之改变title标题的颜色]
- 交换排序
- JavaScript 基础 二
- Ubuntu下adb安装
- linux中的file命令简介
- HDFS SnapShot学习
- LeetCode(61) Rotate List
- Scala并发编程react、loop代码实战详解
- HTML5基础05----HTML5格式化及使用