按钮文本长度
我们可以通过titleLabel属性,可以修改其标题,但是如果文本的长度过长显示不下就应该将文本换行展示出来:
[btn setTitle:@"当我们的文本过长时\n 我们需要换行显示" forState:UIControlStateNormal];
btn.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
我们处理的方式就是在文本中插入换行字符,这样就能在word wrap模式下实现强制回车了。
按钮添加动画
我们可以将图形放在按钮的前后或者是后面,但是要将图形的交互模式关掉,禁止用户的其他操作干扰到按钮,用户可以看到图形的动画效果。
按钮添加多种状态
在开发的过程中有的时候我们对于按钮有多种状态来满足需求,我们可以通过switch语句选择相关状态下的图案,这样就能够令按钮支持更多的图案和状态了。
按钮以动画效果响应用户
简单事例:
+ (instancetype) button
{
PushButton *button = [PushButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:button action:@selector(toggleButton:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:button action:@selector(zoomButton:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragInside | UIControlEventTouchDragEnter ];
[button addTarget:button action:@selector(relaxButton:) forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel | UIControlEventTouchUpOutside ];
return button;
}
- (void)toggleButton:(UIButton *)button
{
[self relaxButton:button];
}
- (void)zoomButton:(UIButton *)button
{
[UIView animateWithDuration:0.2f animations:^{
button.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
}];
}
- (void)relaxButton:(UIButton *)button
{
[UIView animateWithDuration:0.2f animations:^{
button.transform = CGAffineTransformIdentity;
}];
}