
button.backgroundColor = kRandomColor
button.tag = i + addTag
button.frame = CGRectMake(kScreenWidth / 3 * i, 0, kScreenWidth / 3, 50)
[button setTitle:titleArray[i] forState:UIControlStateNormal]
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]
[button setImage:image forState:UIControlStateNormal]
//再将文字和图片进行偏移处理
//这里+3和-3是需要文字和图片有一定的间隔
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width, 0, image.size.width)]
[button setImageEdgeInsets:UIEdgeInsetsMake(0, button.titleLabel.bounds.size.width+3, 0, -button.titleLabel.bounds.size.width-3)]
//自己去将图片进行修改
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size)
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)]
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext()
// 使当前的context出堆栈
UIGraphicsEndImageContext()
// 返回新的改变大小后的图片
return scaledImage
}
如图所示,在按钮中插入图片插入的图片使用background的方式,no-repeat设置图片不重复,同时设置距离left和bottom的距离,以此来调整图片在按钮中的位置。其次,记得设置按钮的width,因为botton会根据里面的文字撑大,但是因为使用背景图片所以bottom大小不会变。最后,设置text-algin:right使按钮中的文字靠右。
需要准备的材料分别有:电脑、浏览器、html编辑器。
1、首先,打开html编辑器,新建html文件,例如:index.html,编写问题基础代码。
2、在index.html中的<button>标签中,加入样式代码:style="background: url(small2.png)width:150pxheight:60px"。
3、浏览器运行index.html页面,此时按钮被成功添加了图片。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)