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

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

作者: 狗头蘑菇 | 来源:发表于2018-01-29 15:01 被阅读0次

    大部分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,我给做成了免费的,功能只多不少,欢迎各位下载使用。

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/ssbxzxtx.html