1. 程式人生 > >ios程式碼調整button圖片image文字title位置

ios程式碼調整button圖片image文字title位置

自定義一個button,要調整 button中的image(注意,不是backgroundImage) 和 title 文字的位置,只需要重寫 Button類獨對應的兩個方法即可:

首先,我們來建立一個 SuperButton繼承自 UIButton

#import <UIKit/UIKit.h>

@interface SuperButton : UIButton

@end
實現檔案
#import "SuperButton.h"
#import "UtilsFunctions.h"
@interface SuperButton ()
{
    CGRect boundingRect;

}
@end
@implementation SuperButton
//自定義的初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {

        [self setTitle:@"專案介紹" forState:UIControlStateNormal];
        [self.titleLabel setFont:[UIFont boldSystemFontOfSize:font]];
        [self setBackgroundImage:[UIImage
imageNamed:@"[email protected]"] forState:UIControlStateNormal]; [self setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal]; boundingRect=[self.titleLabel.text boundingRectWithSize:CGSizeMake(320,font) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont
systemFontOfSize:font]} context:nil]; } return self; }

1.重寫方法,改變 圖片的位置 在 titleRect..方法後執行

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageX=self.frame.size.width/2+boundingRect.size.width/2;
    UIScreen *s=[UIScreen mainScreen];
    CGRect rect=s.bounds;
    CGFloat imageY=contentRect.origin.y+14;
    CGFloat width=24;
    CGFloat height=24;
    return CGRectMake(imageX, imageY, width, height);

}

2.改變title文字的位置,構造title的矩形即可

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{

    CGFloat imageX=(self.frame.size.width-boundingRect.size.width)/2;
    CGFloat imageY=contentRect.origin.y+10;
    CGFloat width=220;
    CGFloat height=25;
    return CGRectMake(imageX, imageY, width, height);

}

@end

我們只要重寫 上述的兩個方法,就可以實現對 button按鈕中的圖片和文字的位置的調整
注意: 1.ios7和ios8系統上 上述兩個方法 執行的次數會有差異,可以設定標誌位,或者自定義一個 button(不要整合button)
2.程式碼是經過刪減的,大家關鍵是重寫上面的兩個方法,重新繪製矩形,即可

相關推薦

ios程式碼調整button圖片image文字title位置

自定義一個button,要調整 button中的image(注意,不是backgroundImage) 和 title 文字的位置,只需要重寫 Button類獨對應的兩個方法即可: 首先,我們來建立一個 SuperButton繼承自 UIButton

UIButton的圖片文字相對位置調整

通常,如果直接設定UIButton的圖片和文字,預設的兩者相對位置可能不是我們想要的,那麼需要進行調整。 需要用到的函式如下: UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, C

UIButton上圖片文字位置調整

      UIButton 上預設是圖片在左文字在右,而大多數情況這樣預設的的顯示形式都不能滿足我們的需求,接下來我就這個問題分享一下我的心得。       預設情況下,不設定的效果,都是居中實現     UIButton *button = [UIButton buttonWithType:UIButt

Android Button圖片文字都居左,且有一點間距

<Button         android:id="@+id/message"         android:layout_width="200dip"                 android:layout_height="wrap_content"         android:dra

iOS經典講解之UIButton改變圖片文字位置

作者:劉新林 UIButton的image和titleLabel是靠在一起居中顯示的,預設image在左,titleLabel在右,但是有些情況要求兩者交換位置顯示,可以通過 @property(nonatomic) UIEdgeInsets i

Android程式碼中設定圖片文字自定義顏色,Linearlayout等控制元件設定背景

平時專案中遇到的問題,在此記錄警示自己,也可使別人少走彎路。 ————–> 1. 程式碼中設定圖片,使用iv.setImageResource(R.mipmap.yishenghuo2);

一句話搞定UIButton內圖片文字位置

為了解決UIButton的圖片和文字位置問題,往往大費周章的去試。浪費時間,這裡寫了一個分類直接一句話就可以完成你想要的四個位置。程式碼可點選獲取程式碼連結 注意:先保證UIButton的大小足夠裝下

iOS Button 按鈕 設定圖片文字的右邊

     UIButton * cityBtn=[UIButtonbuttonWithType:UIButtonTypeCustom];     [cityBtn setTitle:@"上海"forState:UIControlStateNormal];     [c

iOS UILable 文字添加圖片文字前面,中間,後面添加圖)

str nbsp tab 添加 tac nsa end agen mut 1,實例化一個UILable 2, // 創建一個富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString

gd庫處理圖片(將正方形轉換圓形) (調整 頭像大小)(文字圖片合併)(建立畫布)

/* 正方形轉換圓形圖片 * $url 小圖 (絕對路徑) * $background 背景圖 (絕對路徑) * */ function circular($url,$background){ $w = $h = 56;/* 頭像大小 */ $c = imagecolora

textview中點選效果實現,比如點選textview中實現圖片文字的顏色變化(類似於button

<TextView android:drawableTop="@drawable/bg_text_view" android:id="@+id/home_toolbar_settings" an

iOS WKWebView 禁止長按(超連結、圖片文字...)彈出效果

方法一://設定代理 self.wkWebView.navigationDelegate = self; #pragma mark - wkWebViewDelegate //禁止連結跳轉 - (v

iOS:UIButton圖片image不被拉伸(ContentMode)

有時使用UIButton即想美觀又想簡便,因此可以用以下方式既能使UIButton的背景圖片(準確的將是UIButton的imageView的image)可以像imageView那樣可控制 [bt s

html 提交表單,圖片文字一起提交,圖片存入伺服器,圖片地址和表單資訊存入資料庫,帶後端php程式碼

html <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no

iOS 開發中tabBarItem不顯示文字,只顯示圖片,且圖片居中顯示

//第一頁 HomePageViewController *homePageVC = [[HomePageViewControlleralloc] init]; UINavigationCon

iOS獲取網頁上資料(圖片文字、視訊)

獲取網頁上所有圖片、獲取所有html、獲取網頁title、獲取網頁內容文字。。。 .h 檔案 程式碼: //網頁 //NSString *strPath = [NSString stringWithFormat:@"http://www.bai

iOS開發——純程式碼介面(UIViewController和文字類控制元件)

一、新增檢視控制器(UIViewController) 建立一個ViewController類繼承UIViewController ViewController.m做如下修改 - (void)viewDidLoad { [super viewDid

ios UITextView 插入圖片文字自適應高度

1,首先自定義一個textView繼承UITextview 2.重寫textview 的contentsize方法 - (void)setContentSize:(CGSize)contentSize { CGSize oriSize = self.contentS

iOS中用UIWebView的loadHTMLString後圖片文字失調解決方法

iOS中用UIWebView的loadHTMLString後圖片和文字失調,圖片過大,超過螢幕,文字太小;或者圖片太小,文字太大,總之就是不協調。 我們的需求是讓圖片的大小跟著螢幕的變化而變化,就是動態的去適應螢幕;那麼文字的字型就是我們自己可以控制,可大可小

Android Button可同時設定圖片文字

其實有一個大家一直沒有注意發現的問題就是Button是繼承與TextView,是TextView衍生出來的,自然就有些方法就可以呼叫。 下面這兩個方法就是本文的重點: context.getDrawa