c_cpp 创建一个带有圆角矩形背景的UILabel,就像在iPhone Mail应用程序中一样。 (比使用UILabel.layer.cornerRadius快得多)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 创建一个带有圆角矩形背景的UILabel,就像在iPhone Mail应用程序中一样。 (比使用UILabel.layer.cornerRadius快得多)相关的知识,希望对你有一定的参考价值。
//
// RoundedRectLabel.m
//
#import "RoundedRectLabel.h"
#import <QuartzCore/QuartzCore.h>
@implementation CountLabel
@synthesize cornerRadius;
@synthesize rectColor;
- (void)dealloc {
[rectColor release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
- (void) setBackgroundColor:(UIColor *)color
{
self.rectColor = color;
[super setBackgroundColor:[UIColor clearColor]];
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if(cornerRadius == 0)
self.cornerRadius = 4;
[self drawRoundedRect:context rect:rect radius:cornerRadius color:rectColor];
[super drawRect:rect];
}
- (void) drawRoundedRect:(CGContextRef)c rect:(CGRect)rect radius:(int)corner_radius color:(UIColor *)color
{
int x_left = rect.origin.x;
int x_left_center = rect.origin.x + corner_radius;
int x_right_center = rect.origin.x + rect.size.width - corner_radius;
int x_right = rect.origin.x + rect.size.width;
int y_top = rect.origin.y;
int y_top_center = rect.origin.y + corner_radius;
int y_bottom_center = rect.origin.y + rect.size.height - corner_radius;
int y_bottom = rect.origin.y + rect.size.height;
/* Begin! */
CGContextBeginPath(c);
CGContextMoveToPoint(c, x_left, y_top_center);
/* First corner */
CGContextAddArcToPoint(c, x_left, y_top, x_left_center, y_top, corner_radius);
CGContextAddLineToPoint(c, x_right_center, y_top);
/* Second corner */
CGContextAddArcToPoint(c, x_right, y_top, x_right, y_top_center, corner_radius);
CGContextAddLineToPoint(c, x_right, y_bottom_center);
/* Third corner */
CGContextAddArcToPoint(c, x_right, y_bottom, x_right_center, y_bottom, corner_radius);
CGContextAddLineToPoint(c, x_left_center, y_bottom);
/* Fourth corner */
CGContextAddArcToPoint(c, x_left, y_bottom, x_left, y_bottom_center, corner_radius);
CGContextAddLineToPoint(c, x_left, y_top_center);
/* Done */
CGContextClosePath(c);
CGContextSetFillColorWithColor(c, color.CGColor);
CGContextFillPath(c);
}
@end
//
// RoundedRectLabel.h
//
#import <UIKit/UIKit.h>
@interface CountLabel : UILabel {
NSInteger cornerRadius;
UIColor *rectColor;
}
@property (nonatomic) NSInteger cornerRadius;
@property (nonatomic, retain) UIColor *rectColor;
- (void) setBackgroundColor:(UIColor *)color;
- (void) drawRoundedRect:(CGContextRef)c rect:(CGRect)rect radius:(int)corner_radius color:(UIColor *)color;
@end
以上是关于c_cpp 创建一个带有圆角矩形背景的UILabel,就像在iPhone Mail应用程序中一样。 (比使用UILabel.layer.cornerRadius快得多)的主要内容,如果未能解决你的问题,请参考以下文章
创建带有圆角矩形的进度指示器
每个单词周围的圆角矩形 iPhone
如何创建带有圆角背景的 EditText [关闭]
模糊的圆角矩形 UIBezierPath,UIButton 背景
从带有圆角的矩形中检测角点
如何在 iPhone 上绘制一个带有剪裁而不是圆角的矩形?