0

我正在制作一个自定义电子邮件客户端,我想捕捉其他现有客户端建立的一些外观和感觉。我想制作一个看起来像小气泡的按钮,在默认邮件应用程序以及邮箱等应用程序中显示联系人姓名。

iOS 联系人气泡

有谁知道像这样渲染 UIView 或 UIButton 的最佳方法?我是否应该只对颜色进行采样并更改 alpha 值,然后使用类似以下方法的圆角:

button.layer.cornerRadius = someRadiusThatMakesItRound;
button.layer.masksToBounds = YES;

然后设置视图的边框颜色或者,我可以制作一个图像模板并拉伸它,但理想情况下,有一种方法可以用一些 UIKit 的东西本地渲染它。

有任何想法吗?

4

1 回答 1

0

我会将按钮或视图子类化,然后覆盖 init 和 drawRect 方法。这是我子类化的 UIView 的示例。

#import "CustomSectionView.h"
#import <QuartzCore/QuartzCore.h>

@interface CustomSectionView () {
    UIColor *headerColor;
    UIColor *linesColor;
}

@end

@implementation CustomSectionView

@synthesize titleLabel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        headerColor = [UIColor whiteColor];

        linesColor = [UIColor whiteColor];

        self.layer.cornerRadius = 6;
        self.layer.masksToBounds = YES;

        self.backgroundColor = [UIColor whiteColor];

        //title label
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 3, 110, 18)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = [UIFont fontWithName:@"GillSans-Bold" size:15];
        [self addSubview:titleLabel];

    }
    return self;
}

- (id)initWithFrame:(CGRect)frame headerColor:(UIColor *)color linesColor:(UIColor *)color1
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        if (color == nil) {
            color = [UIColor whiteColor];
        }
        headerColor = color;

        if (color1 == nil) {
            color1 = [UIColor clearColor];
        }
        linesColor = color1;

        self.layer.cornerRadius = 6;
        self.layer.masksToBounds = YES;

        self.backgroundColor = [UIColor whiteColor];

        //title label
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 3, 110, 18)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = [UIFont fontWithName:@"GillSans-Bold" size:15];
        [self addSubview:titleLabel];

    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, linesColor.CGColor);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 0, self.frame.size.height);
    CGContextStrokePath(context);

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, linesColor.CGColor);
    CGContextMoveToPoint(context, self.frame.size.width, 0);
    CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
    CGContextStrokePath(context);

    CGRect rectangle = CGRectMake(0,0,self.frame.size.width,25);
    CGContextSetFillColorWithColor(context, headerColor.CGColor);
    CGContextFillRect(context, rectangle);
}

@end
于 2013-06-18T20:07:25.747 回答