zoukankan      html  css  js  c++  java
  • iOS UIButton按钮 UILabel 文本

    系统字体

    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.

    //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.

    //遍历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点击后改变背景的方法

    //初始设置:
    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 

    button字体颜色

    UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button1 setTitleColor:[UIColor blueColorforState:UIControlStateNormal];

    /****************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

    #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;
    }

    Label自适应

    //Try to call this function after you have set the Label "width", "font" and "text": 
    
    -(void)resizeLabel:(UILabel *)aLabel 
    { 
    aLabel.lineBreakMode = UILineBreakModeWordWrap; 
    aLabel.numberOfLines = 9999; 
    
    CGSize aSize = [aLabel.text sizeWithFont:aLabel.font constrainedToSize:CGSizeMake(aLabel.frame.size.width, 9999.0f) lineBreakMode:UILineBreakModeWordWrap]; 
    
    aLabel.frame = CGRectMake(aLabel.frame.origin.x, aLabel.frame.origin.y, aSize.width, aSize.height+20.0f); 
    }
    

      

  • 相关阅读:
    1CSS与文档
    14交互活动:XHTML表单
    13开始制作表格:表格和列表
    12布置元素:布局和排版
    11高级网站构建:div和span
    10与元素亲密接触:盒元素(the box model)
    tp5.1 与vue ajax请求跨域的问题
    小程序父子组件互相传参,互相调用方法
    PHP/js数组与字符串的操作,字符串转数组,数组转字符串,去掉字符串最后一个字符,判断二维数组是否为空等
    easyui datagrid分页栏位置问题
  • 原文地址:https://www.cnblogs.com/hl666/p/3702363.html
Copyright © 2011-2022 走看看