UIButton小结

前言

本来没有打算写这篇文章的, 主要是因为在工作中遇到一些同事再用 有UIButton的时候, 有些很基本的,系统API提供的都不知道, 例如 如何让UIButton的文字居上,居左, 居右, 居下对其等一些基本点, 为此我特地写了一下UIButton小结

UIButton回顾

继承关系

NSObject -> UIResponder -> UIView -> UIControl -> UIButton
复制代码

API

初始化

遍历构造器

+ (instancetype)buttonWithType:(UIButtonType)buttonType;
复制代码

button类型

 typedef NS_ENUM(NSInteger, UIButtonType) {UIButtonTypeCustom = 0,                        //自定义风格UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), //系统样式,从iOS7开始使用UIButtonTypeDetailDisclosure,                  //蓝色小箭头按钮,主要做详细说明用UIButtonTypeInfoLight,                         //亮色感叹号UIButtonTypeInfoDark,                          //暗色感叹号UIButtonTypeContactAdd,                        //十字加号按钮UIButtonTypeRoundedRect = UIButtonTypeSystem,  //圆角矩形,从iOS7废弃,iOS6中可以使用};
复制代码

偏移量

内容偏移量:正值表示间隔值,负值表示超出参照物的距离。UIEdgeInsetsMake(top, left, bottom, right)有四个值需要设置,分别距离上左下右边的间隔。

// default is UIEdgeInsetsZero. On tvOS 10 or later, default is nonzero except for custom buttons.
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR;
复制代码

标题偏移量:和图片偏移量是相对的,比如:自定义一个按钮实现的效果是图片在左边,标题在右边,可以用这个属性,设置完标题偏移量,图片偏移量就是相对于标题的

@property(nonatomic) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsZero
复制代码

图片偏移量

@property(nonatomic) UIEdgeInsets imageEdgeInsets;  
复制代码

其他API

button的状态为高亮时,文本的阴影会反转 默认是NO

@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted;
复制代码

button的状态为高亮时,图像变暗 默认是YES

@property(nonatomic) BOOL adjustsImageWhenHighlighted;
复制代码

button的状态为禁用时,图像变暗。默认是YES

@property(nonatomic) BOOL adjustsImageWhenDisabled;
复制代码

button的状态为高亮时,发光。默认是NO

@property(nonatomic) BOOL showsTouchWhenHighlighted;
复制代码

系统的一些样式DetailDisclosure InfoLight InfoDark ContactAdd颜色会改变

@property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(5_0);
复制代码

button的状态。包括一些其他的控制的状态

typedef NS_OPTIONS(NSUInteger, UIControlState) {UIControlStateNormal       = 0,      //正常状态UIControlStateHighlighted  = 1 << 0, //高亮状态UIControlStateDisabled     = 1 << 1, //禁用状态UIControlStateSelected     = 1 << 2, //选中状态UIControlStateApplication  = 0x00FF0000,UIControlStateReserved     = 0xFF000000
};
复制代码
// 设置标题 default is nil. title is assumed to be single line
- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;  // 设置标题颜色 default if nil. use opaque white
- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;// 设置标题阴影颜色default is nil. use 50% black
- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // 设置图片default is nil. 
should be same size if different for different states
- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;  // 设置背景图片// default is nil
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // 设置富文本标题default is nil. title is assumed to be single line
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); 
复制代码
// 返回不同状态下标题
- (nullable NSString *)titleForState:(UIControlState)state;// 返回不同状态下标题颜色
- (nullable UIColor *)titleColorForState:(UIControlState)state;// 返回不同状态下标题阴影颜色
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;// 返回不同状态下图片
- (nullable UIImage *)imageForState:(UIControlState)state;// 返回不同状态下背景图片
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;// 返回不同状态下富文本标题
- (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);复制代码
// button的当前标题。当按钮状态改变时值自动改变,可以做判断,当前标题是全文则点击展开标题设置为收起,当前标题是收起则点击收起全文。
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;          // 当前标题颜色default is white(1,1)
@property(nonatomic,readonly,strong) UIColor  *currentTitleColor;        // 当前状态下标题阴影颜色
@property(nullable, nonatomic,readonly,strong) UIColor  *currentTitleShadowColor; 
// 当前状态下图片 切换不同图片,比如做单选,多选可以使用。
@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;             
@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;   
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); 
复制代码
@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0);
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);
复制代码
// 返回背景绘制区域
- (CGRect)backgroundRectForBounds:(CGRect)bounds;// 返回内容绘制区域。内容区域是显示图片和标题及他们特定对齐缩放等的范围
- (CGRect)contentRectForBounds:(CGRect)bounds;// 返回标题的绘制区域
- (CGRect)titleRectForContentRect:(CGRect)contentRect;// 返回图片的绘制区域
- (CGRect)imageRectForContentRect:(CGRect)contentRect;
复制代码

这个地方的API是UIControl的, 很多人并没有在意这个类, 然后用一些很笨的手段去解决对其方式

// button 内容垂直对其方式 default is center
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment; // button 内容水平对其方式 default is center
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
复制代码

转载于:https://juejin.im/post/5aa53fb86fb9a028dd4ddaf3

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278322.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Channel Allocation HDU1373

染色问题&#xff1a;相邻不能染同一种颜色 最少需要的颜色的数量最大团点的数量 #include<bits/stdc.h> using namespace std;#define N 27int n; int mp[N][N]; int ans; int alt[N][N]; int Max[N];bool dfs(int cur,int tot)//cur是s1集合的个数 {if(0cur){if(tot>…

satis原理浅析

什么是satis 我们一般是从packagist获取composer包的&#xff0c;但这些都是公开的。那如果我们想创建自己的私有库呢&#xff0c;比如企业就会有这方便的需要&#xff0c;那我们就可以用satis来创建自己的私有库。 Satis 是一个静态的 composer 资源库生成器。它像是一个超轻量…

HDU - 5686-Problem B (递推+高精)

度熊面前有一个全是由1构成的字符串&#xff0c;被称为全1序列。你可以合并任意相邻的两个1&#xff0c;从而形成一个新的序列。对于给定的一个全1序列&#xff0c;请计算根据以上方法&#xff0c;可以构成多少种不同的序列。 Input 这里包括多组测试数据&#xff0c;每组测试数…

c#写字板实现加粗功能_Windows 7中写字板和绘画中的新功能

c#写字板实现加粗功能WordPad and Paint are often overlooked accessories included in all versions of Windows since 95. They are still included in Windows 7 and now have a new look with some enhanced features. Here we will take a look at some of the new impro…

浏览器加载静态资源文件异常解决办法

2019独角兽企业重金招聘Python工程师标准>>> 1 使用chrome浏览器加载静态资源文件(css、js等)异常导致cssh和js文件不生效&#xff0c;具体报错如下: Resource interpreted as Stylesheet but transferred with MIME type text/html 原因应该是网页文档类型不一致导…

POJChallengeRound2 Guideposts 【单位根反演】【快速幂】

题目分析&#xff1a; 这题的目标是求$$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}G^i $$ 这个形式很像单位根反演。 单位根反演一般用于求&#xff1a;$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}f(x)^i $ 推理过程略&#xff0c;实际上也就是交换求和符号的事情。 接着就变…

用Emesene替换Windows Live Messenger

Tired of Windows Live Messenger bloat and wishing that there was a simpler and cleaner replacement that would let you use your live.com and hotmail.com accounts? Look no further, now you can have all that messenger goodness with Emesene! 厌倦了Windows Liv…

python爬虫笔记(七):实战(三)股票数据定向爬虫

目标分析及描述 #CrawBaiduStocksA.py import requests from bs4 import BeautifulSoup import traceback import redef getHTMLText(url):try:r requests.get(url)r.raise_for_status()r.encoding r.apparent_encodingreturn r.textexcept:return ""def getStockL…

myeclipse和maven的clean和build

转&#xff1a; 详解myeclipse和maven的clean和build 2018年04月20日 11:33:34 群星坠 阅读数&#xff1a;3529 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/qq_35603331/article/details/80002723MyEclipse是一个被广为…

三星Galaxy S20:如何开启黑暗模式

Justin Duino贾斯汀杜伊诺(Justin Duino)Samsung was one of the first Android manufacturers to add Dark Mode to its handsets. If you recently purchased a Galaxy S20, S20, or S20 Ultra, enabling the UI feature and setting it up on a schedule is extremely easy.…

nginx和apache限制IP地址访问的设置方法

一、nginx禁止IP地址访问1、在nginx配置文件中加入这个&#xff1a;2、重启nginx服务二、apache禁止IP地址访问1、更改vhosts.conf文件&#xff1a;NameVirtualHost 192.168.1.191 <VirtualHost 192.168.1.191:99>#DocumentRoot "/usr/local/kk-mail/data/www"…

wordweb在线编辑_使用WordWeb享受按需词典和词库功能

wordweb在线编辑Run across an unusual word or need a synonym for a word quickly? Usually that means opening a browser and doing the appropriate search. Now you can have all that word power goodness at your fingertips with WordWeb. 遇到一个不寻常的词还是需…

转://RMAN跨平台可传输表空间和数据库

参考链接&#xff1a; http://blog.itpub.net/23135684/viewspace-776048/ http://blog.sina.com.cn/s/blog_69e7b8d7010164xh.html https://www.2cto.com/database/201311/260446.html 这篇文章翻译自Oracle 11gR2官方文档。详细讨论了使用RMAN工具的CONVERT DATAFILE&#xf…

2139=数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

1 #include<stdio.h>2 #include<string.h>3 int map[1000][1000],visit[1000];4 int step,mark;5 int queue[1000];//用来储存已经遍历了的数据。6 void BFS(int k)7 {8 int i,o0,p0,temp,end0;//temp用来表示当前所在地。o表示下一步从哪个顶点向下出发。9 …

vnc数量限制_通过限制视觉效果在Vista上加速VNC

vnc数量限制This article was written by MetrotekGeek from Metrotek Solutions, a friend of the How-To Geek 本文由Metrotek Solutions的MetrotekGeek撰写&#xff0c;Metrotek Solutions是How-To Geek的朋友 As a computer field tech, I use the remote desktop program…

思科AP-什么是COS AP?

COS:Click OS 所有新的wave 2 AP都带有COS。它建立在IOS之上&#xff0c;但behaves 不同。 COS APs是Click OS APs&#xff08;较新的AP型号&#xff0c;Wave 2等&#xff09; 例如&#xff1a;18xx&#xff0c;28xx&#xff0c;38xx&#xff0c;48xx型号Click OS APs或COS AP。…

[转帖]外壳命名空间扩展

一般介绍 很多人一定用过ZipMagic&#xff0c;对它能把一个压缩文件映射成文件夹感到很奇怪&#xff0c;不知道它使用了什么技术&#xff0c;实际上它用到的技术就是实现了一个外壳的命名空间扩展&#xff08;Shell Namespace Extention&#xff09;。 文件夹和视图&#xff1a…

使Safari在Windows Vista上每20秒停止崩溃

The new Safari for Windows is a very slick browser that beats the pants off everything else in the speed department, but it crashes so much on Windows Vista that it’s virtually unusable. 新的Windows版Safari浏览器非常流畅&#xff0c;可以超越速度部门的所有…

js----与浏览列表有关的对象(浏览器对象)

document  location  history  navigator  screen   frame History 对象包含用户&#xff08;在浏览器窗口中&#xff09;访问过的 URL Location 对象包含有关当前 URL 的信息 Window 对象表示浏览器中打开的窗口 Navigator 对象包含有关浏览器的信息 转载于:https:/…

[svc]jdk+tomcat部署.jforum论坛部署

安装jdk和tomcat jdk1.7.0_13(系列)下载url 我这里用的最新的jdk. 去官网下载即可cd /usr/local/src/ tar xf jdk-8u162-linux-x64.tar.gz -C /usr/local/ ln -s /usr/local/jdk1.8.0_162 /usr/local/jdk tar xf apache-tomcat-8.5.29.tar.gz -C /usr/local/ ln -s /usr/local/…