ios24--change the text and picture of a button

Source: Internet
Author: User
Tags uikit

////VIEWCONTROLLER.M//09-uibutton Adjustment of internal child controls////Created by Xiaomage on 15/12/30.//2015 little brother. All rights reserved.//#import "ViewController.h"#import "XMGButton.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //1.1 Create buttonUIButton *button =[UIButton Buttonwithtype:uibuttontypecustom]; //1.2 Set FrameButton.frame = CGRectMake ( -, -, the, -);//not set Fram invisible//1.3 Setting the background colorButton.backgroundcolor =[Uicolor Purplecolor]; //1.4 Setting Text[Button Settitle:@"Normal button"Forstate:uicontrolstatenormal]; //1.5 Setting the content picture[Button Setimage:[uiimage imagenamed:@"Miniplayer_btn_playlist_normal"] Forstate:uicontrolstatenormal]; //1.6 Changing positionButton.imageView.backgroundColor =[Uicolor Yellowcolor]; Button.titleLabel.backgroundColor=[Uicolor Bluecolor]; //Note: In the outside of the button, the inside of the button will be overwritten .    /*button.titleLabel.frame = CGRectMake (0, 0, 100, 70);    Button.imageView.frame = CGRectMake (100, 0, 70, 70); */[Button Titlerectforcontentrect:cgrectmake (0,0, -, -)]; [Button Imagerectforcontentrect:cgrectmake ( -,0, -, -)]; [Self.view Addsubview:button];//no wires, just through addsubview. }@end
// //   XMGButton.h//  09-uibutton Adjustment of internal child controls //#import <UIKit/UIKit.h>@interface  xmgbutton:uibutton@end
////xmgbutton.m//09-uibutton Adjustment of internal child controls//#import "XMGButton.h"@implementationXmgbutton-(Instancetype) initWithFrame: (cgrect) frame{if(self =[Super Initwithframe:frame]) {       //Center TextSelf.titleLabel.textAlignment =Nstextalignmentcenter; //Change the content mode of a pictureSelf.imageView.contentMode =Uiviewcontentmodecenter; }    returnSelf ;}#pragmaMark-Way One/** * Rewrite two methods: Do not call Super, is to rewrite the * contentrect: Content size, content including (ImageView and label)*//*-(CGRect) Titlerectforcontentrect: (cgrect) contentrect{return cgrectmake (0, 0, 100, 70);}  -(CGRect) Imagerectforcontentrect: (cgrect) contentrect{return CGRectMake (100, 0, 70, 70);} */#pragmaMark-Way Two-(void) layoutsubviews{[Super Layoutsubviews]; //set the location of child controlsSelf.titleLabel.frame = CGRectMake (0,0, -, -); Self.imageView.frame= CGRectMake ( -,0, -, -);}@end

ios24--change the text and picture of a button

Related Article

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.