UIButton的探秘

原文链接

sizeToFit()和sizeThatFits(_:)

sizeToFit()会调用sizeThatFits(_:)方法,将现在的frame作为参数。然后根据函数返回的结果更新view。

sizeToFit will simply call through to sizeThatFits: passing the view's current size as the argument. It will then update the view's frame based on the value it gets back. So all the important logic goes in sizeThatFits:, and this is the method you should override for your own custom controls.

UIButton标题的shadow效果与反转

        myButton?.titleLabel?.shadowOffset = CGSizeMake(2.0, 2.0)
        myButton?.setTitleShadowColor(UIColor.redColor(), forState: .Normal)
//        myButton?.showsTouchWhenHighlighted = true
        myButton?.reversesTitleShadowWhenHighlighted = true

加上选中态亮瞎眼的效果

        myButton?.showsTouchWhenHighlighted = true

UIButton的布局与UIEdgeInsets

有三个UIEdgeInsets

  1. contentEdgeInsets
    使用sizeThatFits(_:)计算时用到。
  2. titleEdgeInsets
    使用sizeThatFits(_:)计算时不会用到。
  3. imageEdgeInsets
    使用sizeThatFits(_:)计算时不会用到。

You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge.

举例说明

  • contentEdgeInsets

    left和right都是30,会使左边向中间移动30pt,右边向中间移动30pt。也就是左间距和右间距都是30pt。由于contentEdgeInsets参与sizeThatFits(_:)的计算,所以最终表现为整个button被拉宽。
  • titleEdgeInsets

    保持contentEdgeInsets不变,设置titleEdgeInsets的left为15。由于titleEdgeInsets在sizeThatFits(_:)中不参与,因此button没有被拉宽,只是titleLabel的左边向右(中心)移动了15pt,右边保持不变。导致字符串没有被显示全。

    设置titleEdgeInsets的right为-15,结果是titleLabel的右边向右移动了15pt,从而整体的显示范围不变。
  • imageEdgeInsets

图像在右,标题在左

button.transform = CGAffineTransformMakeScale(-1.0, 1.0);
button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
button.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);

链接

时间: 2024-12-14 18:50:14

UIButton的探秘的相关文章

自定义UIButton

#import <UIKit/UIKit.h> #import "UIView+SDExtension.h" @interface CookButton : UIButton @end #import "CookButton.h" @implementation CookButton - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame])

UIButton样式设置

btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @”search” forState: UIControlStateNormal]; //设置按钮上的自体的大小 //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法 //应该使用 btn.titleLabel.font = [UIFont systemFo

UIKit框架之UIButton详解

UIKit框架是iPhone应用程序开发中最基本的框架,也是用得最多.最重要的框架,今天要和大家分享的就是UIKit中的UIButton相关知识,一起来看看吧. 1.实例化: 1.1.init方式: 1 UIButton *button = [[UIButton alloc] initWithFrame:rect]; 1.2.类方法方式: 1 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 其中按钮类型枚

IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果

拖个button继承一下  几行代码 就搞定 自用效果还行 IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果,布布扣,bubuko.com

iOS 中UIButton的 settitle 和 titlelabel的使用误区

UIButton中设置Titl方法包括以下几种: - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state @property(nonatomic,readonly,retain) NSString *currentTitle; @property(n

iOS 强大的泛型,同样也可以对UIButton进行扩展

文章围绕这五点: 1. 泛型是什么 2. 为什么要用泛型 3. 泛型怎么用 4. 泛型进阶 5. 泛型的延伸使用 泛型(Generics)是什么? 引用Apple中Generics的描述: Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can writ

UIButton的imageEdgeInsets 和 titleEdgeInsets

我们知道,在UIButton中有一个UILabel和一个UIImageView,同时还有属性: titleEdgeInsets,imageEdgeInsets.介绍下 imageEdgeInsets 和 titleEdgeInsets 的用法. UIEdgeInsets 首先,titleEdgeInsets 和 imageEdgeInsets 都是 UIEdgeInsets类型.UIEdgeInsets 是一个结构体,定义如下: typedef struct UIEdgeInsets { CGF

iOS UIButton EdgeInsets

说一下系统的button,image 和 title的位置关系 默认image 和 title的位置关系: 随便画了草图,有点丑,不过不妨碍理解: 第一种:在button上只设置文字,这个时候,button的文字默认是剧中的. 第二种:在button上只设置图片,也是默认剧中的. 第三种:主要说的是这种,当同时设置图片和文字时,默认图片是剧中的,文字就会被排挤到button的右侧. 这个时候,如果我们想要改变文字和button的位置,就要使用EdgeInsets EdgeInsets UIEdg

JVM总结(一):概述--JVM对象探秘

这一节我们来讨论一下JVM对象建立过程. JVM对象探秘 对象的建立 对象的内存布局 对象的访问定位 JVM对象探秘 对象的建立 对象的建立过程   图一:对象建立过程 1.类加载检查. 当JVM检测到有一条new指令时,首先先检查该指令的参数是否在常量池中定位到一个类的符号引用,并检查这个符号引用所代表的类是否已被加载.解析和初始化过.如果存在的话,JVM将直接使用已有的信息对该类进行操作. 如果没有,则执行相应的类加载过程. 2.为新生对象分配内容. 不同的JVM垃圾收集器在内容分配的时候的