首页 > 代码库 > iOS UIButton按钮

iOS UIButton按钮

系统字体

+ View Code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
    NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
    fontNames = [[NSArray alloc] initWithArray:
            [UIFont fontNamesForFamilyName:
            [familyNames objectAtIndex:indFamily]]];
    for (indFont=0; indFont<[fontNames count]; ++indFont)
    {
        NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
    }
}

 

Button

  1.

?
1
2
3
4
5
6
7
//iOS 7下给UIButton加边框
[testBtn.layer setMasksToBounds:YES];
[testBtn.layer setCornerRadius:8.0]; //设置矩圆角半径
[testBtn.layer setBorderWidth:1.0];   //边框宽度
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
[testBtn.layer setBorderColor:colorref];//边框颜色

  2.

?
1
2
3
4
5
6
7
8
9
10
//遍历button改标题
- (void)changeName:(UIButton *)btn{
    for (NSInteger i = 0; i < 10; i ++) {
        if (btn.tag == 10) {
            [btn setTitle:@"我的名字不一样" forState:UIControlStateNormal];
        }else{
            [btn setTitle:@"一样的" forState:UIControlStateNormal];
        }
    }
}

 3.关于UIButton,既可以采用setBackgroundImage来设置底部图片,同时也可以采用 setImage方法;

两者还是有一定区别的;
首先setBackgroundImage,image会随着button的大小而改变,图片自动会拉伸来适应button的大小,这个时候当然可以设置button的title,image不会挡住title;
相反的的setImage,图片不会进行拉伸,原比例的显示在button上,此时再设置title,title将无法显示,因此可以根据需求选中方法;

4.

UIButton点击后改变背景的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
//初始设置:
UIImage *bgImg1 = [UIImage imageNamed:@"Selected.png"];
UIImage *bgImg2 = [UIImage imageNamed:@"Unselected.png"];
[btn setImage:bgImg2 forState:UIControlStateNormal];
[btn setImage:bgImg1 forState:UIControlStateSelected];
 
//然后在button的Action方法改变button状态:
- (IBAction) buttonTouch:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
}

一个uiview上多个button,如何判断是哪个按钮被点击了

UIButton *button = (UIButton *)sender;
1.根据button.tag来区分

2.或者根据button的title 

 

/****************Label****************************/

 

label中显示不同颜色的字以及不同字体,字体高亮,Diy label 

http://www.cocoachina.com/bbs/read.php?tid=69385&keyword=label

 

 

 

改变UILabel里的一段文字的颜色

1.core text     2.用NSAttributedString  ,...setAttributedText:

 

给UILabel的文本添加阴影效果

 Lable.shadowColor = color;
 Lable.shadowOffset = CGSizeMake(0, -1.0);

 

下拉框的实现 UITableView + UIButton + UILabel   

 

http://www.cocoachina.com/bbs/read.php?tid=110202&keyword=label

UIlabel旋转90度

lb.transform=CGAffineTransformMakeRotation(M_PI/2); 

button取标题值赋值给label

label.text = button.currenTitle 

-(IBAction)buttonClicked:(id)sender{
    UIButton *button = (UIButton*)sender;    //得到你点击的button
    NSString *buttonTitle = button.titleLabel.text;    //得到button的title并赋值给string类型的buttonTitle
    (比如你的label名字就叫label的话)
    label.text = buttonTitle;    //给label赋值
}

 

如何将UILabel中的文字变链接?  FromCOCOchina

自己定义一个类urlLabel

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#import <Foundation/Foundation.h>
@class URLLabel;
@protocol MyLabelDelegate <NSObject>
@required
- (void)myLabel:(URLLabel *)myLabel touchesWtihTag:(NSInteger)tag;
@end
 
@interface URLLabel : UILabel {
    id <MyLabelDelegate> delegate;
}
@property (nonatomic, assign) id <MyLabelDelegate> delegate;
- (id)initWithFrame:(CGRect)frame;
@end
 
 
 
#import "URLLabel.h"
 
#define FONTSIZE 13
#define COLOR(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
@implementation URLLabel
@synthesize delegate;
// 设置换行模式,字体大小,背景色,文字颜色,开启与用户交互功能,设置label行数,0为不限制
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame])
    {
        [self setLineBreakMode:UILineBreakModeWordWrap|UILineBreakModeTailTruncation];
        [self setFont:[UIFont systemFontOfSize:FONTSIZE]];
        [self setBackgroundColor:[UIColor clearColor]];
        [self setTextColor:COLOR(59,136,195,1.0)];
        [self setUserInteractionEnabled:YES];
        [self setNumberOfLines:0];
    }
    return self;
}
// 点击该label的时候, 来个高亮显示
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self setTextColor:[UIColor purpleColor]];
}
// 还原label颜色,获取手指离开屏幕时的坐标点, 在label范围内的话就可以触发自定义的操作
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self setTextColor:COLOR(59,136,195,1.0)];
    UITouch *touch = [touches anyObject];
    CGPoint points = [touch locationInView:self];
    if (points.x >= self.frame.origin.x && points.y >= self.frame.origin.y && points.x <= self.frame.size.width && points.y <= self.frame.size.height)
    {
        [delegate myLabel:self touchesWtihTag:self.tag];
    }
}
- (void)dealloc {
    [super dealloc];
}

  

完整的超链接的UILabel教程、

http://www.cocoachina.com/bbs/read.php?tid-60903.html

UILabel 触摸操作

http://www.cocoachina.com/bbs/read.php?tid=48942&keyword=label

 

 

label实时显示textfield的输入

UILabel *textLabel;
UITextField *textField;

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textLabel.text = [textField.text stringByAppendingString:string];
return YES;
}