zoukankan      html  css  js  c++  java
  • UIButton文字和图片偏移

    UIButton 设置偏移

    @propery  titleEdgeInsets

    @propery  imageEdgeInsets

    @propery  contentEdgeInsets

    @propery  contentHorizontalAlignment

    初始化Button

    1    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    2     NSString *title = @"音乐";
    3     UIImage  *image = [UIImage imageNamed:@"sp_music_list_sc"];
    4     [button setTitle:title forState:UIControlStateNormal];
    5     [button setImage:image forState:UIControlStateNormal];
    6     button.frame = CGRectMake(20, 100, 100, 40);
    7     button.backgroundColor = [UIColor grayColor];

    设置文字偏移

    1   CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:10.f]];
    2     CGSize imageSize = image.size;   
    3     button.titleEdgeInsets = UIEdgeInsetsMake(15, -imageSize.width, 0, 0);

    设置图片偏移

    1 button.imageEdgeInsets = UIEdgeInsetsMake(-15,titleSize.width+10,0,0);

    设置整体偏移 (文字和图片都会偏移)

    1 button.contentEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 0);

    设置对齐方式 (UIButton)

    1 //对齐方式  设置content是title和image一起变化
    2     button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  • 相关阅读:
    课上练习,判断大小。
    软件工程个人作业03。
    构建之法阅读笔记一。
    软件工程个人作业02。
    SSM常见面试题
    Bean的作用域和初始化时机(singleton&prototype)
    Spring中的AOP
    Spring核心概念和打印机案例
    MyBatis中的常用注解
    MyBatis中的延迟加载策略
  • 原文地址:https://www.cnblogs.com/guojunzi/p/4512748.html
Copyright © 2011-2022 走看看