UILabel的text显示不同的颜色,高度自适应

self.title = @"For iOS 6 & later";

NSMutableAttributedString*str = [[NSMutableAttributedStringalloc] initWithString:@"Using NSAttributed String"];

[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor blueColor] range:NSMakeRange(0,5)];

[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor] range:NSMakeRange(6,12)];

[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor greenColor] range:NSMakeRange(19,6)];

[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"Arial-BoldItalicMT"size:30.0] range:NSMakeRange(0, 5)];

[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"HelveticaNeue-Bold"size:30.0] range:NSMakeRange(6, 12)];

[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"Courier-BoldOblique"size:30.0] range:NSMakeRange(19, 6)];

attrLabel.attributedText = str;


UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, 100)];

label.numberOfLines= 0;

label.lineBreakMode=NSLineBreakByWordWrapping;

label.text=@"尊贵而不失奢华,典雅却不失自然!温馨和浪漫的生活,我们与你一同创造!";

label.backgroundColor= [UIColorredColor];

label.font= [UIFontsystemFontOfSize:20];

CGSizesize = [labelsizeThatFits:CGSizeMake(label.frame.size.width,MAXFLOAT)];

NSLog(@"%f=====%f",size.height,size.width);

label.frame=CGRectMake(0, 50,self.view.frame.size.width, size.height);

[self.viewaddSubview:label];

让UILabel具有链接功能,点击后调用safari打开网址

UILabel*labelGovUrl = [[UILabelalloc]initWithFrame:CGRectMake(73.0,330.0,180.0,40.0)];

labelGovUrl.autoresizingMask= (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);

labelGovUrl.text=@"侬侬官网 >";

labelGovUrl.backgroundColor= [UIColorclearColor];

labelGovUrl.textColor= [UIColorwhiteColor];

labelGovUrl.font= [UIFontfontWithName:@"Helvetica-Bold"size:14];

labelGovUrl.userInteractionEnabled=YES;

labelGovUrl.tag= K_NNGOV_WEBSITE_LABEL_URL;

UITapGestureRecognizer*tapGesture =

[[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(openURL:)]autorelease];

[labelGovUrladdGestureRecognizer:tapGesture];

[self.viewaddSubview:labelGovUrl];

[labelGovUrlrelease];

-(void)openURL:(UITapGestureRecognizer*)gesture{

NSInteger tag = gesture.view.tag;

NSString*url =nil;

if(tag == K_NNWEIBO_LABEL_URL) {

url =@"http://t.qq.com/yourgame/";

}

if(tag == K_NNGOV_WEBSITE_LABEL_URL){

url =@"http://www.zjnn.cn/";

}

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:url]];

}


让label自适应里面的文字,自动调整宽度和高度的

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。

[label setNumberOfLines:0];

NSString *s = @"string......";

UIFont *font = [UIFont fontWithName:@"Arial" size:12];

CGSize size = CGSizeMake(320,2000);

CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];

[self.view addSubView:label];

这样就可以对s赋值让其自动调整其大小了。

你可能感兴趣的:(UILabel的text显示不同的颜色,高度自适应)