ZKTextField 0.0.1

ZKTextField 0.0.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Release上次发布2014年12月

未声明 维护。



  • Alex Zielenski

简介

ZKTextField 是对 NSTextField 的简单替换,它提供了高度可定制的功能。它不使用任何单元格,并将几乎所有的功能委托给不同的方法,以便子类可以轻松地覆盖它们并更改功能。最终没有更多的 -editWithFrame:… 或 -selectWithFrame:… 莫名奇妙的代码!

如何使用它

简单继承了 ZKTextField 并实现足够的方法,以便根据您的要求自定义您的文本字段。示例:

@implementation ZKAccountTextField
- (id)initWithFrame:(NSRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = [NSColor whiteColor];
        self.shouldShowFocus = NO;
    }
return self;
}

- (void)drawFrameWithRect:(NSRect)rect
{
    // 2px border
    [kEQAccountTextFieldCellNormalColor set];
    [self.currentClippingPath setLineWidth:2.0 * 2.0];
    [self.currentClippingPath stroke];
}

- (NSPoint)textOffsetForHeight:(CGFloat)textHeight
{
    // center vertically
    return NSMakePoint(12.0, round(NSMidY(self.bounds) - textHeight / 2));
}

- (CGFloat)textWidth
{
    // the size of our field minus the margin on both size
    return self.bounds.size.width - 12 * 2;
}

- (NSDictionary *)stringAttributes
{
    NSMutableDictionary *origAttrs = [[super stringAttributes].mutableCopy autorelease];
    [origAttrs setObject:[NSFont fontWithName:@"HelveticaNeue-Medium" size:12.0] forKey:NSFontAttributeName];
    [origAttrs setObject:[NSColor colorWithDeviceRed:0.5086 green:0.5047 blue:0.520 alpha:1.000] forKey:NSForegroundColorAttributeName];
    return origAttrs;
}

- (NSDictionary *)placeholderStringAttributes
{
    return self.stringAttributes;
}

- (NSDictionary *)selectedStringAttributes
{
    return [NSDictionary dictionaryWithObjectsAndKeys:
            [NSColor whiteColor], NSForegroundColorAttributeName,
            [[NSColor colorWithDeviceRed:0.5086 green:0.5047 blue:0.520 alpha:1.000] colorWithAlphaComponent:0.8], NSBackgroundColorAttributeName, nil];
}

- (NSColor *)insertionPointColor
{
    return [NSColor colorWithDeviceRed:0.5086 green:0.5047 blue:0.520 alpha:1.000];
}

- (CGFloat)minimumWidth
{
    return 269.0;
}

- (CGFloat)minimumHeight
{
    return 39.0;
}

- (CGFloat)maximumWidth
{
    return self.minimumWidth;
}

- (CGFloat)maximumHeight 
{
    return self.minimumHeight;
}

@end

这将获取到一个具有白色背景、自定义插入点、灰色选择背景、2px 灰色圆角且不显示焦点的文本字段。它看起来有点像这样

Preview

如果看不见的话

功能

你可能正在阅读这个问题,想想自己,“哇,这很棒!有什么缺点吗?它是如何工作的?”这是个好问题!的确有一个缺点!

ZKTextField 缺少一些 NSTextField 的功能,例如多行换行。到目前为止,ZKTextField 只支持“单行模式”或水平滚动。对于大多数人来说,这不会是问题;对于其他人,抱歉。

除了没有多行模式外,ZKTextField 还强制您的文本高度与所需的行高完全一致。为什么?因为很难将文本编辑器对齐和视图中的文本对齐。

您可以随意浏览此存储库并修复我造成的问题。真的,请请这么做。

ZKTextField 采用 MIT 许可。

以下是一些法律术语:

// ZKTextField
//
// Copyright (C) 2012 by Alex Zielenski.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.