利用drawinrect在control中画入图片和文字-创新互联
//
成都创新互联是一家专业提供茌平企业网站建设,专注与网站设计、做网站、H5建站、小程序制作等业务。10年已为茌平众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。// BtnControl.m
// ZCar
//
// Created by imac on 15/10/4.
// Copyright (c) 2015年 imac. All rights reserved.
//
#import "BtnControl.h"
@implementation BtnControl
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.layer.borderWidth = 1;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
-(void)setNameWithTitle:(NSString *)nameWithTitle
{
if (_nameWithTitle != nameWithTitle) {
_nameWithTitle = nameWithTitle;
[self setNeedsDisplay];
}
}
- (void)setImgName:(NSString *)imgName
{
if (_imgName != imgName) {
_imgName = imgName;
[self setNeedsDisplay];
}
}
- (void)setDic:(NSDictionary *)dic
{
if (_dic != dic) {
_dic = dic;
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
[self drawWithText:_nameWithTitle ImgName:_imgName titleColor:_dic];
}
- (void)drawWithText:(NSString *)title ImgName:(NSString *)imgName titleColor:(NSDictionary *)dic{
UIImage *img = [UIImage p_w_picpathNamed:imgName];
[img drawInRect:CGRectMake(self.width - 27, self.height - 12, 25, 10)];
int i = [self convertToInt:title];
float width = (self.width - i * 12) / 2;
[title drawInRect:CGRectMake(width, 10, self.width, self.height) withAttributes:dic];
}
- (int)convertToInt:(NSString*)strtemp {
int strlength = 0;
char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
if (*p) {
p++;
strlength ++;
}
else {
p ++;
}
}
return (strlength + 1) / 2;
}
@end
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
文章题目:利用drawinrect在control中画入图片和文字-创新互联
网站网址:http://azwzsj.com/article/ccgohj.html