ios 控件徽章_ios-将徽章图标添加到圆角矩形按钮?

您需要使用resizableImageWithCapInsets来实现此目的,而无需使用photoshop图像库.有一些很棒的线程(here和here)说明了其用法.

这是我刚刚想给您的一个示例:

//Create a label (width/height not important at this stage)

UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];

yourLabel.text = @"7+";

[yourLabel sizeToFit];

CGRect labelFrame = yourLabel.frame;

//Here we create a UIImage that is resizable, but will not resize the areas concerned with the cap insets you've defined

UIImage *badgeImage = [[UIImage imageNamed:@"badge.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];

UIImageView *badgeImageView = [[UIImageView alloc]initWithImage:badgeImage];

badgeImageView.contentMode = UIViewContentModeScaleToFill;

badgeImageView.backgroundColor = [UIColor clearColor];

labelFrame.size.width += 5; //This is the 'padding' on the right and left (added together)

//If your badge edges are completely circular then you don't want to change the height, but if they're not then go ahead in the same way with the width. If your badge has a static height, you'll need to make sure the font size doesn't exceed this height; better start off with a small font-size

badgeImageView.frame = labelFrame; //The badge is now the right width with padding taken into account

//Center the label on the badge image view

yourLabel.center = CGPointMake(badgeImageView.frame.size.width/2, badgeImageView.frame.size.height/2);

//Finally we add the label to the badge image view

[badgeImageView addSubview:yourLabel];

//Add your badge to the main view

[self.view addSubview:badgeImageView];

[badgeImageView release];

[yourLabel release];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值