IOS UIButton Set Picture text vertical arrangement

Source: Internet
Author: User

In the actual development of iOS projects, we often need to change the style of the control system, we define one, the same when we find that the system comes with some of the methods are not good to make, we also think of rewriting this method. This article mainly records the author set UIButton picture text vertical arrangement method, finally solved in the picture and the text vertical arrangement case, if the text length changes causes the picture position to change the question, for this question online has the more practice, I did not say more, This record of this detail is only to deepen the impression and facilitate later review. If you have any flaws, please forgive me.

Scenario One: By adjusting the button picture and the text padding

Uiedgeinsets

typedef struct UIEDGEINSETS {    cgfloat top, left, bottom, right;  Specify amount to inset (positive) for each of the edges. Values can is negative to ' outset '} uiedgeinsets;

There are three settings for edgeinsets in UIButton : Contentedgeinsets, Titleedgeinsets, imageedgeinsets

@property (nonatomic)          uiedgeinsets contentedgeinsets ui_appearance_selector;//default is Uiedgeinsetszero@property (nonatomic)          uiedgeinsets titleedgeinsets;                Default is Uiedgeinsetszero@property (nonatomic)          BOOL         reversestitleshadowwhenhighlighted;//default is NO. If YES, shadow reverses to shift between Engrave and Emboss Appearance@property (nonatomic)          uiedgeinsets imageedgeins ETS;                Default is Uiedgeinsetszero

Uiedgeinsetsmake

The four parameters inside indicate the distance from the upper boundary, the left boundary, the lower boundary, the right boundary, the default is zero, the title/image is in the center of the button

Uikit_static_inline uiedgeinsets Uiedgeinsetsmake (cgfloat top, cgfloat left, CGFloat bottom, cgfloat right) {    Uiedgeinsets insets = {top, left, bottom, right};    return insets;}
@interfaceUIButton (Uibuttonext)- (void) Centerimageandtitle: (float) space; - (void) Centerimageandtitle; @end   @implementationUIButton (Uibuttonext)- (void) Centerimageandtitle: (float) Spacing {//get the size of the elements here for readabilityCgsize imageSize =self.imageView.frame.size; Cgsize titlesize=self.titleLabel.frame.size; //get the height they would take up as a unitCGFloat totalheight = (imagesize.height + titlesize.height +spacing); //raise the image and push it right to center itSelf.imageedgeinsets = Uiedgeinsetsmake (-(Totalheight-imagesize.height),0.0,0.0, -titlesize.width); //lower the text and push it left to center itSelf.titleedgeinsets = Uiedgeinsetsmake (0.0,-ImageSize.Width,-(Totalheight-titlesize.height),0.0); }   - (void) Centerimageandtitle {Const intDefault_spacing =6.0f;   [Self centerimageandtitle:default_spacing]; }  @end

后面经过测试,如果button的文字长度变更,会导致图片位置变化,经过多次修改UIEdgeInsets的值也没有达到期望效果,最终我们采用下述方案:自定义按钮继承系统的UIButton,重写layoutSubviews方法,布局子控件,调整按钮内ImageView's center and Titlelabel frame solve the above problems.  方案二

-(void) layoutsubviews {[Super layoutsubviews]; //Center ImageCgpoint Center =Self.imageView.center; Center.x= self.frame.size.width/2; Center.y= self.imageview.frame.size.height/2; Self.imageView.center=Center; //Center TextCGRect Newframe =[self titlelabel].frame; Newframe.origin.x=0; NEWFRAME.ORIGIN.Y= Self.imageView.frame.size.height +5; NewFrame.size.width=Self.frame.size.width; Self.titleLabel.frame=Newframe; Self.titleLabel.textAlignment=Uitextalignmentcenter;}

Here the author takes the scenario two examples as follows:

#import "BSQuickLoginButton.h"@implementationBsquickloginbutton- (void) layoutsubviews{[Super Layoutsubviews]; //adjust the position and size of the picture (ImageView)Cgpoint Center =Self.imageView.center; Center.x= self.frame.size.width/2; Center.y= self.imageview.frame.size.height/2; Self.imageView.center=Center; //adjust the position and size of text (titlelable)CGRect Newframe =Self.titleLabel.frame; Newframe.origin.x=0; NEWFRAME.ORIGIN.Y=Self.imageView.frame.size.height; NewFrame.size.width=Self.frame.size.width; NewFrame.size.height= Self.frame.size.height-Self.imageView.frame.size.height; Self.titleLabel.frame=Newframe; //make text centeredSelf.titleLabel.textAlignment =Nstextalignmentcenter;}@end

One small detail to note here is that we cannot directly get the property assignment setting for the button's frame, we need to set a temporary tempframe, and then assign a value to the frame of the button.

: The expression cannot be specified as a non-assignable error

In addition, in development we often write self.imageView.frame.size.height a kind of tedious repetition of code, here we can take a lazy trick: to control (UIView) write a classification as follows:
#import "Uiview+extension.h"@implementationUIView (Extension)- (void) SetX: (cgfloat) x{CGRect frame=Self.frame; Frame.origin.x=x; Self.frame=frame;}-(cgfloat) x{returnself.frame.origin.x;}- (void) Sety: (cgfloat) y{CGRect frame=Self.frame; FRAME.ORIGIN.Y=y; Self.frame=frame;}-(cgfloat) y{returnSELF.FRAME.ORIGIN.Y;}- (void) SetWidth: (cgfloat) width{CGRect frame=Self.frame; Frame.size.width=width; Self.frame=frame;}-(cgfloat) width{returnself.frame.size.width;}- (void) SetHeight: (cgfloat) height{CGRect frame=Self.frame; Frame.size.height=height; Self.frame=frame;}-(cgfloat) height{returnself.frame.size.height;}- (void) Setcenterx: (cgfloat) centerx{Cgpoint Center=Self.center; Center.x=CenterX; Self.center=Center;}-(cgfloat) centerx{returnself.center.x;}- (void) Setcentery: (cgfloat) centery{Cgpoint Center=Self.center; Center.y=CenterY; Self.center=Center;}-(cgfloat) centery{returnself.center.y;}@end

At the same time, overriding the set and get methods of each property of a frame will no longer be affected by the detail problems that cannot be directly assigned above, the scheme code can be simplified as follows:

- (void) awakefromnib{self.titleLabel.textAlignment=Nstextalignmentcenter;}- (void) layoutsubviews{[Super Layoutsubviews]; //adjust the position and size of the pictureSELF.IMAGEVIEW.Y =0; Self.imageView.centerX= Self.width *0.5; //adjust the position and size of textself.titlelabel.x =0; Self.titlelabel.y=Self.imageView.height; Self.titleLabel.width=Self.width; Self.titleLabel.height= Self.height-self.titlelabel.y;}

In the project actual combat, the author through the custom button, realizes the following login interface Quick Login button picture and the text vertical arrangement style demand, the effect is as follows

Contrast:

IOS UIButton Set Picture text vertical arrangement

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.