【问题标题】:Replicate iOS contact button from Mail从邮件复制 iOS 联系人按钮
【发布时间】:2013-06-18 19:45:50
【问题描述】:

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

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

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

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

有什么想法吗?

【问题讨论】:

    标签: ios uibutton uikit


    【解决方案1】:

    我会将按钮或视图子类化,然后覆盖 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
    

    【讨论】:

    • 感谢您的回答!我知道这条路线是一种选择,但我想我只是想看看是否有更好/更简单和更优雅的解决方案来解决这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多