您的位置:首页 > 其它

关于button

2016-04-27 16:27 120 查看
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"*

@implementation UIImage (Extension)

+ (UIImage *)imageWithColor:(UIColor *)color
{
CGFloat imageW = 3;
CGFloat imageH = 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();

return image;
}

@end

//image分类  .m文件代码
*强调内容*
*#import <UIKit/UIKit.h>*

@interface UIImage (Extension)

/**
*  生成的图片的rect默认为100,100
*/
+ (UIImage *)imageWithColor:(UIColor *)color;
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  button 布局