2016-1-18UIlabel学习,正则表达式

1.如何计算某个字符串的长度

1⃣️确定一个容器的size

a.width或者height一方固定

b.变化的变量要足够大(300,20000)

2⃣️确定计算的font

3⃣️调用boundingRectWithSize

NSString *str = @"The NSString class and its mutable subclass, NSMutableString";

    

    UIFont *font = [UIFont fontWithName:@"Du Bellay" size:20];

    NSDictionary *attrDic = @{NSFontAttributeName:font};

    

    CGSize bigSize = CGSizeMake(300, 3000);

    

    CGSize realSize = [str boundingRectWithSize:bigSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil].size;

    

    

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, realSize.width, realSize.height)];

    label.backgroundColor = [UIColor blackColor];;

    label.text = str;

    label.font = font;

    label.textColor = [UIColor colorWithRed:222/255.0 green:59/255.0 blue:17/255.0 alpha:1];

    //对齐方式

    label.textAlignment = NSTextAlignmentLeft;

    //设置label显示多少行 0表示多行 n>0 则显示n行,如果显示不完,用‘...’表示

    label.numberOfLines = 0;

    //设置这行方式

    label.lineBreakMode = NSLineBreakByWordWrapping;

    //投影

    //label.shadowOffset = CGSizeMake(-1, -1);

    //label.shadowColor = [UIColor redColor];

    [self.view addSubview:label];

二.设置按钮看是否能够与用户进行交互

- (void)testButton{

    //UIButton

    /*

     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

     btn.frame = CGRectMake(10, 100, 100, 100);

     [btn setTitle:@"Cancel" forState:UIControlStateNormal];

     [btn setTitle:@"放手" forState:UIControlStateHighlighted];

     

     //设置按钮是否能够和用户进行交互

     //btn.enabled = NO;

     [btn setTitle:@"此刻无法点击" forState:UIControlStateDisabled];

     

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

     [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

     

     [btn setBackgroundImage:[UIImage imageNamed:@"Unlock_DotLock1_Selected"] forState:UIControlStateNormal];

     [btn setBackgroundImage:[UIImage imageNamed:@"Unlock_DotLock_Wrong1"] forState:UIControlStateHighlighted];

     [self.view addSubview:btn];

     */

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setFrame:CGRectMake(10, 100, 100, 100)];

    [btn setImage:[UIImage imageNamed:@"Unlock_DotLock1_Selected"] forState:UIControlStateNormal];

    [btn setImage:[UIImage imageNamed:@"Unlock_DotLock_Wrong1"] forState:UIControlStateHighlighted];

    [btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchDragEnter];

    [self.view addSubview:btn];

}

三.正则表达式

1.提取@的字符串

//创建一个正则表达式@

//NSString *regex = @"@\\w+";

    //NSString *regex = @"#\\w+#";

    //NSString *regex = @"http(s)?://[0-9a-zA-Z./_]+";

    //电话号码

    //NSString *regex = @"(\\()?(\\d){3}(\\))?(-)?(\\d){8}";

综合

- (void)testRTLabel{

    RTLabel *label = [[RTLabel alloc] initWithFrame:CGRectMake(10, 100, 300, 0)];

    label.delegate = self;

    

    //1.确定显示的内容

    NSString *orgString = @"@赵鹏123:正则表达式好学么,@王权:给你一个网站:http://www.baidu.com/image/2323_303.jpg,#学习#,电话号码:(025)-69716261";

    

    //计算尺寸

    label.text = orgString;

    CGSize size = [label optimumSize];

    label.frame = CGRectMake(10, 100, size.width, size.height);

    

    //显示内容

    //使用正则表达式查找需要点击的内容

    NSString *regex = @"(@\\w+)|(#\\w+#)|(http(s)?://[0-9a-zA-Z./_]+)";

    

    //使用RegexKitLite解析

    NSArray *results = [orgString componentsMatchedByRegex:regex];

    

    for (NSString *keyword in results) {

        //将text里面的内容进行替换

        //@赵鹏123

        //<a href='http://www.baidu.com'>@赵鹏123</a>

        NSString *newKeyWord = [NSString stringWithFormat:@"<a href='http://www.hkstv.hk:8080/adver/picture/2014/5/0c64828b-8e37-4d11-8a58-880382981731.jpg'>%@</a>", keyword];

        

        orgString = [orgString stringByReplacingOccurrencesOfString:keyword withString:newKeyWord];

    }

 

    label.text = orgString;

    [self.view addSubview:label];

}

 

posted @ 2016-01-18 19:59  liuzhicen  阅读(197)  评论(0编辑  收藏  举报