@SanMao
2015-08-06T00:35:28.000000Z
字数 3517
阅读 1406
UI
UIButton的背景图片的拉伸只能通过代码的方式拉伸(resizableImageWithCapInsets:),不能通过storyboard(UIImageView却可以)
使用
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 在界面上添加一个UIButton按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// UIButton *btn = [[UIButton alloc] init];
btn.frame = CGRectMake(135, 200, 100, 30);
// 正常状态下的图片
[btn setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
// 高亮状态下的图片
[btn setImage:[UIImage imageNamed:@"like_pressed"] forState:UIControlStateHighlighted];
// 正常状态下的背景图
[btn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
// 高亮状态下的背景图
[btn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
// 正常状态下的标题文字
[btn setTitle:@"点赞" forState:UIControlStateNormal];
// 高亮状态下的标题文字
[btn setTitle:@"100" forState:UIControlStateHighlighted];
// 标题文字颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
// 监听按钮的点击(凡是继承自UIControl的对象都能通过addTarget方法监听事件)
[btn addTarget:self action:@selector(buttonclick) forControlEvents:UIControlEventTouchUpInside];
// 新建一个UISwitch
UISwitch *s = [[UISwitch alloc] init];
s.center = CGPointMake(150, 300);
[self.view addSubview:s];
[s addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged];
}
- (void)switchClick:(UISwitch *)s {
NSLog(@"bianhua - %d",s.isOn);
}
@property(nonatomic) NSInteger numberOfLines; // 默认是1行,行数设置为0时自动换行
@property(nonatomic) NSTextAlignment textAlignment; // 对齐方式
UILabel实现包裹文本内容
(要使用自动布局)
// Some convenience methods to create colors. These colors will be as calibrated as possible.
// These colors are cached.
+ (UIColor *)blackColor; // 0.0 white
+ (UIColor *)darkGrayColor; // 0.333 white
+ (UIColor *)lightGrayColor; // 0.667 white
+ (UIColor *)whiteColor; // 1.0 white
+ (UIColor *)grayColor; // 0.5 white
+ (UIColor *)redColor; // 1.0, 0.0, 0.0 RGB
+ (UIColor *)greenColor; // 0.0, 1.0, 0.0 RGB
+ (UIColor *)blueColor; // 0.0, 0.0, 1.0 RGB
+ (UIColor *)cyanColor; // 0.0, 1.0, 1.0 RGB
+ (UIColor *)yellowColor; // 1.0, 1.0, 0.0 RGB
+ (UIColor *)magentaColor; // 1.0, 0.0, 1.0 RGB
+ (UIColor *)orangeColor; // 1.0, 0.5, 0.0 RGB
+ (UIColor *)purpleColor; // 0.5, 0.0, 0.5 RGB
+ (UIColor *)brownColor; // 0.6, 0.4, 0.2 RGB
+ (UIColor *)clearColor; // 0.0 white, 0.0 alpha
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
/* 需要执行动画的代码 */
[UIView commitAnimations];
* 方式2:block式
[UIView animateWithDuration:2.0 delay:1.0 options:kNilOptions animations:^{
/* 需要执行动画的代码 */
} completion:nil]
// 1s后,再执行动画(动画持续2s)
titleRectForContentRect:
和imageRectForContentRect:
方法,分别返回titleLabel和imageView的framelayoutSubviews
方法中设置
// 设置按钮内容的内边距(影响到imageView和titleLabel)
@property(nonatomic) UIEdgeInsets contentEdgeInsets;
// 设置titleLabel的内边距(影响到titleLabel)
@property(nonatomic) UIEdgeInsets titleEdgeInsets;
// 设置imageView的内边距(影响到imageView)
@property(nonatomic) UIEdgeInsets imageEdgeInsets;
// 只拉伸中间的1x1区域
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
* iOS5开始
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
UIControl
的控件,都可以通过addTarget:...
方法来监听事件