btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//設(shè)置按鈕上的自體的大小
//[btn setFont: [UIFont systemFontSize: 14.0]];??? //這種可以用來設(shè)置字體的大小,但是可能會(huì)在將來的SDK版本中去除改方法
//應(yīng)該使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
//最后將按鈕加入到指定視圖superView
[superView addSubview: btn];
==========================================================
tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];
這樣初始化的button心俗,文字默認(rèn)顏色是白色的傲武,所有如果背景也是白色的話,是看不到文字的城榛,
btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//設(shè)置文字位置揪利,現(xiàn)設(shè)為居左,默認(rèn)的是居中
[btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字
有些時(shí)候我們想讓UIButton的title居左對(duì)齊狠持,我們?cè)O(shè)置
btn.textLabel.textAlignment = UITextAlignmentLeft
是沒有作用的疟位,我們需要設(shè)置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是問題又出來,此時(shí)文字會(huì)緊貼到做邊框喘垂,我們可以設(shè)置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距離做邊框保持10個(gè)像素的距離甜刻。
=======================================================
設(shè)置UIButton上字體的顏色設(shè)置UIButton上字體的顏色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
增加一個(gè) UIButton 上設(shè)置不同顏色的字體,也可設(shè)置其大小等
NSString* aStr =@"本人是個(gè)大帥哥";
NSMutableAttributedString*str = [[NSMutableAttributedStringalloc] initWithString:[NSStringstringWithFormat:@"%@",aStr]];?
[str addAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Arial-BoldItalicMT"size:16.0] ?range:NSMakeRange(0,3)];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColorgreenColor]range:NSMakeRange(0,3)];
UIButton* butn = [UIButtonbuttonWithType:UIButtonTypeCustom];? ?
butn.backgroundColor = [UIColororangeColor];
butn.frame =CGRectMake(100,200,200,30);?
[butn setAttributedTitle:str forState:UIControlStateNormal];? ?
[butn addTarget:selfaction:@selector(click) forControlEvents:UIControlEventTouchUpInside];? ?
[self.view addSubview:butn];
設(shè)置 button 顯示網(wǎng)絡(luò)圖片自適應(yīng)的方法為:
[buttonsd_setImageWithURL:[NSURLURLWithString:pictureModel.url]forState:UIControlStateNormalplaceholderImage:[UIImageimageNamed:@"Placeholder_240x240.png"]];
[button.imageViewsetContentMode:UIViewContentModeScaleAspectFill];
感覺雖然這都是一些簡(jiǎn)單東西,但有時(shí)候有一些小細(xì)節(jié)注意不到,記錄下來希望能幫到有需要的人.