iOS button label 透明色

//
//  ViewController.m
//  7.13蓝牙
//
//  Created by computer on 16/7/13.
//  Copyright © 2016年 computer. All rights reserved.
//

#import "ViewController.h"
#import "BLEManager.h"//蓝牙中心控制管理界面
#import "musicVC.h"
#import "FMVC.h"
#import "line_inVC.h"
#import "PC_VC.h"
#import "BT_VC.h"

@interface ViewController ()
@property(nonatomic ,strong)UIButton *btnmusic;
@property(nonatomic ,strong)UIButton *btnFm;
@property(nonatomic ,strong)UIButton *btnline;
@property(nonatomic ,strong)UIButton *btnPC;
@property(nonatomic ,strong)UIButton *btnBT; @property(nonatomic ,strong)UILabel
*txtmusic; @property(nonatomic ,strong)UILabel *txtFM; @property(nonatomic ,strong)UILabel *txtline_in; @property(nonatomic ,strong)UILabel *txtPC; @property(nonatomic ,strong)UILabel *txtBT; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title=@"控制中心⌨️"; // 设置背景图片 UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0,64,fDeviceWidth,fDeviceHeight-64)]; imageView.image=[UIImage imageNamed:@"book_bg.jpg"]; [self.view insertSubview:imageView atIndex:0]; // 创建菜单按钮 [self setButton_name:self.btnmusic btn_x:fDeviceWidth*0.2 btn_y:fDeviceHeight/7+44 btn_w:fDeviceWidth*0.2 btn_h:fDeviceHeight*2/21 btnNormalimage:@"book_music.png" btnHighimage:@"LOGO_2.png" btnClick:@"btn1"]; [self setButton_name:self.btnFM btn_x:fDeviceWidth*0.6 btn_y:fDeviceHeight/7+44 btn_w:fDeviceWidth*0.2 btn_h:fDeviceHeight*2/21 btnNormalimage:@"book_FM.png" btnHighimage:@"LOGO_2" btnClick:@"btn2"]; [self setButton_name:self.btnline_in btn_x:fDeviceWidth*0.2 btn_y:fDeviceHeight*3/7+44 btn_w:fDeviceWidth*0.2 btn_h:fDeviceHeight*2/21 btnNormalimage:@"book_line_in.png" btnHighimage:@"LOGO_2" btnClick:@"btn3"]; [self setButton_name:self.btnPC btn_x:fDeviceWidth*0.6 btn_y:fDeviceHeight*3/7+44 btn_w:fDeviceWidth*0.2 btn_h:fDeviceHeight*2/21 btnNormalimage:@"book_PC.png" btnHighimage:@"LOGO_2" btnClick:@"btn4"]; [self setButton_name:self.btnBT btn_x:fDeviceWidth*0.2 btn_y:fDeviceHeight*5/7+44 btn_w:fDeviceWidth*0.2 btn_h:fDeviceHeight*2/21 btnNormalimage:@"book_BT.png" btnHighimage:@"LOGO_2" btnClick:@"btn5"]; [self setButton_name:self.btnBT btn_x:fDeviceWidth*0.6 btn_y:fDeviceHeight*5/7+44 btn_w:fDeviceWidth*0.2 btn_h:fDeviceHeight*2/21 btnNormalimage:@"book_switch.png" btnHighimage:@"LOGO_2" btnClick:@"btn6"]; // 创建按钮标签 [self setLabel_name:self.txtmusic lbl_x:fDeviceWidth*0.2 lbl_y:fDeviceHeight*5/21+44 lbl_w:fDeviceWidth*0.2 lbl_h:fDeviceHeight*1/21 lbltext:@"音乐模式" lbltextbgc:[UIColor colorWithWhite:0.f alpha:0] lbltxtcolor:[UIColor whiteColor] lblfont:[UIFont fontWithName:@"Arial" size:15.0] lblcornerRadius:0 lblmasksToBounds:YES lbltextAlignment:NSTextAlignmentCenter]; [self setLabel_name:self.txtFM lbl_x:fDeviceWidth*0.6 lbl_y:fDeviceHeight*5/21+44 lbl_w:fDeviceWidth*0.2 lbl_h:fDeviceHeight*1/21 lbltext:@"FM模式" lbltextbgc:[UIColor colorWithWhite:0.f alpha:0] lbltxtcolor:[UIColor whiteColor] lblfont:[UIFont fontWithName:@"Arial" size:15.0] lblcornerRadius:0 lblmasksToBounds:YES lbltextAlignment:NSTextAlignmentCenter]; [self setLabel_name:self.txtline_in lbl_x:fDeviceWidth*0.2 lbl_y:fDeviceHeight*11/21+44 lbl_w:fDeviceWidth*0.2 lbl_h:fDeviceHeight*1/21 lbltext:@"ling_in模式" lbltextbgc:[UIColor colorWithWhite:0.f alpha:0] lbltxtcolor:[UIColor whiteColor] lblfont:[UIFont fontWithName:@"Arial" size:15.0] lblcornerRadius:0 lblmasksToBounds:YES lbltextAlignment:NSTextAlignmentCenter]; [self setLabel_name:self.txtPC lbl_x:fDeviceWidth*0.6 lbl_y:fDeviceHeight*11/21+44 lbl_w:fDeviceWidth*0.2 lbl_h:fDeviceHeight*1/21 lbltext:@"PC模式" lbltextbgc:[UIColor colorWithWhite:0.f alpha:0] lbltxtcolor:[UIColor whiteColor] lblfont:[UIFont fontWithName:@"Arial" size:15.0] lblcornerRadius:0 lblmasksToBounds:YES lbltextAlignment:NSTextAlignmentCenter]; [self setLabel_name:self.txtBT lbl_x:fDeviceWidth*0.2 lbl_y:fDeviceHeight*17/21+44 lbl_w:fDeviceWidth*0.2 lbl_h:fDeviceHeight*1/21 lbltext:@"BT模式" lbltextbgc:[UIColor colorWithWhite:0.f alpha:0] lbltxtcolor:[UIColor whiteColor] lblfont:[UIFont fontWithName:@"Arial" size:15.0] lblcornerRadius:0 lblmasksToBounds:YES lbltextAlignment:NSTextAlignmentCenter]; [self setLabel_name:self.txtBT lbl_x:fDeviceWidth*0.6 lbl_y:fDeviceHeight*17/21+44 lbl_w:fDeviceWidth*0.2 lbl_h:fDeviceHeight*1/21 lbltext:@"切换模式" lbltextbgc:[UIColor colorWithWhite:0.f alpha:0] lbltxtcolor:[UIColor whiteColor] lblfont:[UIFont fontWithName:@"Arial" size:15.0] lblcornerRadius:0 lblmasksToBounds:YES lbltextAlignment:NSTextAlignmentCenter]; } #pragma mark - 创建按钮 -(void)setButton_name:(UIButton *)btnname btn_x:(int)btnX btn_y:(int)btnY btn_w:(int)btnW btn_h:(int)btnH btnNormalimage:(NSString *)norimgname btnHighimage:(NSString *)highimaname btnClick:btnClicksth{ // 初始化按钮 btnname=[UIButton buttonWithType:UIButtonTypeCustom]; // 设定按钮位置宽高 btnname.frame=CGRectMake(btnX,btnY,btnW,btnH); // 正常状态下按钮图片 [btnname setBackgroundImage:[UIImage imageNamed:norimgname] forState:UIControlStateNormal]; // 高亮就是按下按钮时的图片 [btnname setBackgroundImage:[UIImage imageNamed:highimaname] forState:UIControlStateHighlighted];
//    设定按钮背景色

 [rightbutton setBackgroundColor:[UIColor colorWithRed:163.0/255.0 green:36.0/255.0 blue:227.0/255.0 alpha:1.0]];

 

//    设定按钮文字,大小,颜色


    [btnname setTitle:btntxt forState: UIControlStateNormal];


    btnname.titleLabel.font = [UIFont systemFontOfSize: 14.0];

  [btnname setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    //边框宽度


    [btnname.layer setBorderWidth:1.0];


    btnname.layer.borderColor=[UIColor whiteColor].CGColor;

//切圆角

button.clipsToBounds=YES;

button.layer.cornerRadius=30;//这里的30是你想设置的圆角大小,比如是一个40*40的正方形,那个设置成20就是一个圆,以此类推

//    按钮事件方法
    if ([btnClicksth isEqualToString:@"btn1"]) {
        [btnname addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
    }else if ([btnClicksth isEqualToString:@"btn2"]){
         [btnname addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchUpInside];
    }else if ([btnClicksth isEqualToString:@"btn3"]){
        [btnname addTarget:self action:@selector(btn3Click:) forControlEvents:UIControlEventTouchUpInside];
    }else if ([btnClicksth isEqualToString:@"btn4"]){
        [btnname addTarget:self action:@selector(btn4Click:) forControlEvents:UIControlEventTouchUpInside];
    }else if ([btnClicksth isEqualToString:@"btn5"]){
        [btnname addTarget:self action:@selector(btn5Click:) forControlEvents:UIControlEventTouchUpInside];
    }else if ([btnClicksth isEqualToString:@"btn6"]){
        [btnname addTarget:self action:@selector(btn6Click:) forControlEvents:UIControlEventTouchUpInside];
    }else{
         [btnname addTarget:self action:@selector(btn0Click:) forControlEvents:UIControlEventTouchUpInside];
    }
    btnname.clipsToBounds=YES;
    btnname.layer.cornerRadius=15;//这里的30是你想设置的圆角大小,比如是一个40*40的正方形,那个设置成20就是一个圆,以此类推
//    在视图中加载出来按钮
    [self.view addSubview:btnname];
}
#pragma mark 创建文字
-(void)setLabel_name:(UILabel *)lblname lbl_x:(int)lblX lbl_y:(int)lblY lbl_w:(int)lblW lbl_h:(int)lblH lbltext:(NSString *)lbltxt lbltextbgc:(UIColor *)bgcolor lbltxtcolor:(UIColor *)lblcolor lblfont:(UIFont *)lblfont lblcornerRadius:(int)lblradius lblmasksToBounds:(BOOL)lblmTB lbltextAlignment:(NSTextAlignment)lblAlignment{
    //    初始化Label标签
    lblname=[[UILabel alloc]initWithFrame:CGRectMake(lblX, lblY, lblW,lblH)];
    lblname.text=lbltxt;//标签内容
    lblname.backgroundColor=bgcolor;//标签背景色彩
    lblname.textColor=lblcolor;//标签色彩
    lblname.font=lblfont;//标签类型大小
    lblname.layer.cornerRadius=lblradius;//设置myLabel的圆角半径为25,刚好是高度的一半。这样的话myLabel的左右两侧就是半圆。
    lblname.layer.masksToBounds=lblmTB;
    lblname.textAlignment=lblAlignment;//对齐方式为居中对齐
    [self.view addSubview:lblname];// 在视图中加载出来按钮
}
#pragma mark 隐藏顶上的信号
//-(void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//}
//
//-(void)viewWillDisappear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//
//   [self.navigationController setNavigationBarHidden:NO animated:YES];
//}

#pragma mark 按钮事件
-(void)btn0Click:(UIButton *)sender{
    NSLog(@"请设置菜单事件");
}
-(void)btn1Click:(UIButton *)sender{
    NSLog(@"你点击了音乐模式");
    musicVC *ShowmusicVC=[[musicVC alloc]init];
    [self.navigationController pushViewController:ShowmusicVC animated:NO];
}
-(void)btn2Click:(UIButton *)sender{
    NSLog(@"你点击了FM模式");
    FMVC *ShowFMVC=[[FMVC alloc]init];
    [self.navigationController pushViewController:ShowFMVC animated:NO];
}
-(void)btn3Click:(UIButton *)sender{
    NSLog(@"你点击了line_in模式");
    line_inVC *Showline_inVC=[[line_inVC alloc]init];
    [self.navigationController pushViewController:Showline_inVC animated:NO];
}
-(void)btn4Click:(UIButton *)sender{
    NSLog(@"你点击了PC模式");
    PC_VC *ShowPC_VC=[[PC_VC alloc]init];
    [self.navigationController pushViewController:ShowPC_VC animated:NO];
}
-(void)btn5Click:(UIButton *)sender{
    NSLog(@"你点击了BT模式");
    BT_VC *ShowBT_VC=[[BT_VC alloc]init];
    [self.navigationController pushViewController:ShowBT_VC animated:NO];
}
-(void)btn6Click:(UIButton *)sender{
    NSLog(@"你点击了切换模式");
    [KBLEManager choose_model];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
原文地址:https://www.cnblogs.com/gaozhang12345/p/7993005.html