• UIButton同时设置图片和文字 及 相关的title设置


    1>同时设置图片和文字

     UIButton *_backButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [_backButton setFrame:CGRectMake(1286428)];

        [_backButton setTitle:@"返回" forState:UIControlStateNormal];        //设置button在没有选中的时候显示的字体

        _backButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];         //设置button显示字体的大小

        [_backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"]forState:UIControlStateNormal];    //设置button背景显示图片

        [self.view addSubview:_backButton];

     

    2>button的title的设置

    btn.frame = CGRectMake(x, y, width, height);

    [btn setTitle: @"search" forState: UIControlStateNormal];

    //设置按钮上的自体的大小

    //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

    //应该使用

    btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];

    [btn seBackgroundColor: [UIColor blueColor]];

    //最后将按钮加入到指定视图superView

    [superView addSubview: btn];

    ==========================================================

    tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];

    这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,

    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中

    [btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字

    有些时候我们想让UIButton的title居左对齐,我们设置

    btn.textLabel.textAlignment = UITextAlignmentLeft

    是没有作用的,我们需要设置

    btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

    但是问题又出来,此时文字会紧贴到做边框,我们可以设置

    btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

    使文字距离做边框保持10个像素的距离。

    =======================================================

    设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:

    [btn.titleLabel setTextColor:[UIColorblackColor]];

    btn.titleLabel.textColor=[UIColor redColor];

    而是用:

    [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

  • 相关阅读:
    数据库版本管理工具flyway
    spring webapp的配置文件放置在项目外的方法
    logback
    linux as4 bind9 设置进程中的一些小效果
    设置/勾销Debian的屏保
    Linux内存:内存管理的天禀
    用YUM晋级CentOS体系中PHP和MySQL
    solaris的故事
    Solaris 的防火墙ipfilter设置
    mysql安置设置文件的成绩
  • 原文地址:https://www.cnblogs.com/yuanyuandachao/p/3784319.html
Copyright © 2020-2023  润新知