iOS code adjust button picture Image text title position

Source: Internet
Author: User

To customize a button, to adjust the position of the image (note, not backgroundimage) and title text in the button, just rewrite the two methods that correspond to the button class alone:

First, let's create a Superbutton that inherits from UIButton

  superbutton.h//  superbutton////  Created by Yang bin on 14/12/25.//  Copyright (c) 2014 Yang bin. All rights reserved.//#import <UIKit/UIKit.h> @interface superbutton:uibutton@end

Implementation file

////superbutton.m//superbutton////Created by Yang bin on 14/12/25.//Copyright (c) 2014 Yang Bin .        All rights reserved.//#import "SuperButton.h" #import "UtilsFunctions.h" @interface Superbutton () {CGRect boundingrect; } @end @implementation superbutton//Custom Initialization Method-(Instancetype) initWithFrame: (cgrect) frame{self = [Super INITWITHFR    Ame:frame];        if (self) {[Self settitle:@] Project Introduction "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. Rewrite the method and change the position of the picture  in  titlerect. Method after execution-(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. Change the position of the title text, construct the title rectangle can-(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
We can  adjust the position of the picture and text in button buttons simply by rewriting the two methods mentioned above.
Note: The two methods on the 1.IOS7 and iOS8 systems run differently, you can set the flag bit, or customize a button (do not integrate the button)
      2. The code is truncated, the key is to rewrite the above two methods, redraw the rectangle, you can


iOS code adjust button picture Image text title position

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.