iOS开发,左右排列有图片有文字的按钮最简单做法

大部分iOS开发者都会碰到这种需求,一个按钮中即有图标又有文字,点击按钮修改文字的功能。

实例图 一般做法都是重写一个uibutton,然后重写

  • (CGRect)titleRectForContentRect:(CGRect)contentRect;
  • (CGRect)imageRectForContentRect:(CGRect)contentRect;

两个方法,设置image和文字的布局。

但是这种方法有很强的局限性,如果要按钮文字就非常尴尬了。

下面小编教大家一招,轻松搞定这种需求。

1,新建一个按钮

UIButton *bottomBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 100, 50)]; 

 [bottomBtn setBackgroundColor:[UIColor whiteColor]];

[self.view addSubview:bottomBtn];

2,在按钮上添加一个label

UILabel *centerLabel = [[UILabel alloc] initWithFrame:tbHeaderView.bounds];

centerLabel.font = dbwFonts(14); centerLabel.textAlignment = NSTextAlignmentCenter; [bottomBtn addSubview:centerLabel];

3,给label添加富文本

NSMutableAttributedString *fromAttributedStr = [[NSMutableAttributedString alloc]initWithString:@"筛选 "];

    NSTextAttachment *imgAttach = [[NSTextAttachment alloc] init];

    imgAttach.image = [UIImage imageNamed:@"箭头下"];

    imgAttach.bounds = CGRectMake(0, -1.5, 13, 13);

    NSAttributedString *imgAttributeStr = [NSAttributedString attributedStringWithAttachment:imgAttach];

    [fromAttributedStr appendAttributedString:imgAttributeStr];

    centerLabel.attributedText = fromAttributedStr;

轻松搞定!

推荐一个全手势识别超炫酷操作的的任务管理软件《好运清单》,免费小巧,功能强大,原版是大名鼎鼎的clear,苹果商店卖30元,我给做成了免费的,功能只多不少,欢迎各位下载使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值