MRSubtleButton 1.2.1

MRSubtleButton 1.2.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released最后发布2015年9月

Marc Ransome维护。




源自NSView的淡色调渐变按钮。

以下是一些使用自定义字体和渐变的示例

Button preview

使用方法

使用CocoaPods可以将MRSubtleButton集成到现有项目中。只需将必要的依赖项添加到您的Podfile中,如下所示:

platform :osx, '10.7'
pod 'MRSubtleButton'
...

运行pod install以进行安装。

接下来,将Xcode中的对象库中的自定义视图拖动到您的UI中,并将其类更改为MRSubtleButton

为按钮创建一个出口,并给按钮设置一个标题:[button setTitle:@"Hello World!"]

事件响应

在您的控制器中实现MRSubtleButtonDelegate协议,并提供以下方法(记得在控制器中包含#import <MRSubtleButton.h>

- (void)subtleButtonEvent:(NSEvent *)event with:(id)sender;

将按钮的代理设置为您的控制器对象:[button setDelegate:self]

通过检查代理方法中的事件对象的type来确定已发生的事件类型,并进行相应处理。

- (void)subtleButtonEvent:(NSEvent *)event with:(id)sender
{
  if ([event type] == NSLeftMouseDown) {
    // the left mouse button was pressed
  }
  else if ([event type] == NSLeftMouseUp) {
    // the left mouse button was released
  }
}

如果您有多个具有相同代理的按钮,可以通过指针比较来确定哪个按钮生成了该事件。

if (sender == [self button])
{
  // your button event implementation
}

自定义颜色和字体

按钮的渐变和字体属性(颜色和大小)可以进行调整。设置按钮的渐变与指定开始和结束颜色一样简单(渐变从按钮的底部边缘开始)

NSColor *start = [NSColor colorWithCalibratedRed:205.0f/255.0f green:183.0f/255.0f blue:158.0f/255.0f alpha:1.0f];
NSColor *end = [NSColor colorWithCalibratedRed:255.0f/255.0f green:239.0f/255.0f blue:213.0f/255.0f alpha:1.0f];

[[self button] setGradientWithStartColor:start endColor:end];

淡色调渐变效果最佳,结束颜色只需比开始颜色亮几个色调。

按钮的高亮渐变——点击按钮时会短暂显示——可以使用以下方法进行调整:

(void)setHighlightGradientWithStartColor:(NSColor *)startColor endColor:(NSColor *)endColor;

调整按钮的字体属性同样简单

NSFont *buttonFont = [NSFont fontWithName:@"Helvetica" size:18.0f];
NSColor *buttonFontColor = [NSColor colorWithCalibratedRed:139.0f/255.0f green:136.0f/255.0f blue:120.0f/255.0f alpha:1.0f]; 

[[self button] setFontAttributesWithFont:buttonFont color:buttonFontColor];

[[self button] setTitle:@"Customised button"];

标题对齐

使用setTitleAlignment:和三个常量之一(MRLeftTitleAlignmentMRRightTitleAlignmentMRCenterTitleAlignment)来控制标题文本对齐方式

[[self button] setTitleAlignment:MRCenterTitleAlignment];

注意事项

MRSubtleButton的源代码使用了自动引用计数,并在针对10.7到10.10的部署目标进行了测试。

许可证

MRSubtleButton根据MIT许可证提供。

联系方式

请发送邮件至 [email protected] 或发推文至@marcransome