UILabel使用

来源:互联网 发布:淘宝企业店铺换公司 编辑:程序博客网 时间:2024/06/10 01:32
UILable 字体加粗
加粗;
[UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];

加粗并且倾斜
[UILabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:20]];

UILabel自适应高度 换行
//初始化label  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];  //设置自动行数与字符换行  [label setNumberOfLines:0];  label.lineBreakMode = NSLineBreakByCharWrapping;   // 测试字串  NSString *s = @"这是一个测试!!!adsfsaf时发生发勿忘我勿忘我勿忘我勿忘我勿忘我阿阿阿阿阿阿阿阿阿阿阿阿阿00000000阿什顿。。。";  UIFont *font = [UIFont fontWithName:@"Arial" size:12];  //使用这中方法需要font相同label.font=font;//设置一个行高上限  CGSize size = CGSizeMake(320,2000);  //计算实际frame大小,并将label的frame变成实际大小  CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];  [label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];


关键字标红

NSString *keyword = @"开源";NSString *result = @"开源中国社区";// 设置标签文字NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc]initWithString:result];// 获取标红的位置和长度NSRange range = [result rangeOfString:keyword];// 设置标签文字的属性[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];// 显示在Label上label.attributedText = attrituteString;//设置标签文本(此属性是iOS6.0之后才出现,如若不是必要,不建议使用此属性)NSString *text = @"其实没什么";    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];    [attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];    myLabel.attributedText = attributeString;




0 0
原创粉丝点击