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.pswp.cn/news/278322.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278322.shtml
英文地址,請注明出處:http://en.pswp.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/…