• iOS上如何让按钮(UIbutton)文本左对齐展示


    1. // button.titleLabel.textAlignment = NSTextAlignmentLeft; 这句无效  
    2.       button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;  
    3.       button.titleEdgeInsets = UIEdgeInsetsMake(01000);  

    在做UI界面的时候一直困惑与如何让button的文字左对齐展示,今天重新遇到。决定彻底解决问题

    首先我们会想到以下的代码

    button.titleLabel.textAlignment = NSTextAlignmentLeft; 这行代码是没有效果的。这仅仅是让标签中的文本左对齐,但

    并没有改变标签在button中的对齐方式。


    所以。我们首先要使用

    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 这行代码,把按钮的内容(控件)

    的对齐方式改动为水平左对齐,可是这们会紧紧靠着左边,不好看,

    所以我们还能够改动属性:

    button.titleEdgeInsets = UIEdgeInsetsMake(0800);

    这行代码能够让button的内容(控件)距离左边8个像素。依据自己须要调整。也可为负数(--8)这样就好看多了。希望对大家也有帮助

    2015.7.31改动

    以下是button和文字上下对齐展示

    int origin_Y;
        origin_Y=64;
        for (int i=0; i<4; i++) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, origin_Y,50, 50)];
            btn.backgroundColor = [UIColor greenColor];
            [btn setImage:[UIImage imageNamed:@"bbc"] forState:UIControlStateNormal];
            [btn setTitle:@"測试" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.titleLabel.backgroundColor = [UIColor blueColor];
            //btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
            btn.imageEdgeInsets = UIEdgeInsetsMake(5,10,21,0);
            btn.titleEdgeInsets = UIEdgeInsetsMake(30,-(btn.frame.size.width-10*2), 0, 0);
            [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:btn];
            origin_Y+=(btn.frame.size.height+10);
        }
    


    如有问题欢迎不吝赐教!

  • 相关阅读:
    通过JDBC连接HiveServer2
    HDP-2.6.1安装
    VMWare虚拟机NAT模式静态IP联网配置
    基于ansj_seg和nlp-lang的简单nlp工具类
    修改ES使用root用户运行
    使用MapReduce将HDFS数据导入到HBase(三)
    HBase表操作
    使用SpringMVC解决Ajax跨域问题
    SpringBoot之Web开发——webjars&静态资源映射规则
    thymeleaf+springboot找不到html,只返回了字符串
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7224850.html
Copyright © 2020-2023  润新知