关于button

2016-06-17  本文已影响111人  AlenChen

1.button内有图片和文字两个属性,可以通过重写以下两句代码来更换他们的位置

-(CGRect)imageRectForContentRect:(CGRect)contentRect-(CGRect)titleRectForContentRect:(CGRect)contentRect


2.关于button的文字对齐,使用button.titleLabel.textAlignment = NSTextAlignmentLeft;这行代码是没有效果的,这只是让标签中的文本左对齐,但

并没有改变标签在按钮中的对齐方式。

所以,我们首先要使用

button.contentHorizontalAlignment= UIControlContentHorizontalAlignmentLeft;button.titleEdgeInsets= UIEdgeInsetsMake(0,10,0,0);

这行代码可以让按钮的内容(控件)距离左边10个像素,这样就好看多了。


3.关于点击改变背景颜色(绘制颜色图片,替代点击状态下的背景图片),具体代码如下:

//添加image分类  .h文件代码*#import"UIImage+Extension.h"*@implementationUIImage(Extension)+ (UIImage*)imageWithColor:(UIColor*)color{CGFloatimageW =3;CGFloatimageH =3;// 1.开启基于位图的图形上下文UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageW, imageH),NO,0.0);// 2.画一个color颜色的矩形框[color set];    UIRectFill(CGRectMake(0,0, imageW, imageH));// 3.拿到图片UIImage*image = UIGraphicsGetImageFromCurrentImageContext();// 4.关闭上下文UIGraphicsEndImageContext();returnimage;}@end//image分类  .m文件代码*强调内容**#import*@interfaceUIImage(Extension)/**

*  生成的图片的rect默认为100,100

*/+ (UIImage*)imageWithColor:(UIColor*)color;@end

上一篇下一篇

猜你喜欢

热点阅读