大家可以先看这个图片右上角的文字效果,首先我们先不要着急做,先想着怎么做,之后在我脑海里面,我想出来了三种解决方案
1.UIButton实现,实现思路是让UI切一张背景图片,通过内部改变UIButton的文字旋转方向
2.UILabel加UIImage实现,跟UIButton思路一样
3.UILabel实现,通过给UILabel绘制一个不规则的矩形框,然后填充颜色,之后旋转UILabel方向实现
说完思路,下面我们开始自己造轮子了,为了简单起见,我直接上代码(以第一种思路)
//声明UIButton并指定其位置和长宽
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(100,100,45,38)];
//设置背景颜色
// button.backgroundColor = [UIColor yellowColor];
//button上的文字
[buttonsetTitle:@"剩30天"forState:UIControlStateNormal];
//文字颜色
[buttonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
//设置文本在button中显示的位置,这里为居中。
button.titleLabel.textAlignment =NSTextAlignmentCenter;
//文字字体大小
button.titleLabel.font = [UIFontsystemFontOfSize:8];
//文字旋转角度
button.titleLabel.transform =CGAffineTransformMakeRotation(M_PI_4);
//文本自适应
button.titleLabel.adjustsFontSizeToFitWidth =YES;
[button.titleLabelsizeToFit];
//设置button的背景图片
[buttonsetBackgroundImage:[UIImageimageNamed:@"label"]forState:UIControlStateNormal];
//改变button内部文字的位置(可以自己调整,调整适合自己的项目)
button.titleEdgeInsets =UIEdgeInsetsMake(-13,5, -5, -10);
//将button添加到view上
[self.viewaddSubview:button];
@end
最终的效果