zoukankan      html  css  js  c++  java
  • UIButton和UINavigationItem设置图片和文字位置

    1.UIButton设置文字位置

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

    btn.textLabel.textAlignment = UITextAlignmentLeft

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

    btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

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

    btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);//使文字距离做边框保持10个像素的距离。

    如果是图片+文字:

    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = buttonRect;
    [button setTitle:@"title" forState:UIControlStateNormal];
    [button setImage:buttonImage forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0.0, WIDTH(button.titleLabel) + 10.0, 0.0, 0.0);
    
    

    2.自定义UINavigationItem包含图片和文字

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    

    // 设置frame只能控制按钮的大小 btn.frame= CGRectMake(0, 0, 40, 44); [btn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:btn]; UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; /** * width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-5时,间距正好调整 * 为0;width为正数时,正好相反,相当于往左移动width数值个像素 */ negativeSpacer.width = -5; self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
     
  • 相关阅读:
    购买成熟软件产品后的二次开发的问题
    outlook2010如何导入csv的通讯录?
    导入Excel数据时对数据校验提示方法
    系统开发中存储过程使用的优势和劣势
    FCKeditor.Net_2.5的使用
    [正则表达式]如何高亮显示搜索关键字
    国外网站模板网址集锦
    _NET 下 FCKeditor_2_5_1上传图片的配置
    用属性模拟多继承机制
    FCKeditor 2.6在ASP.NET中的配置方法
  • 原文地址:https://www.cnblogs.com/liuluoxing/p/5783443.html
Copyright © 2011-2022 走看看