【IOS】UILabel 属性及方法

 label.lineBreakMode = NSLineBreakByTruncatingTail;   //文尾省略号
 label.font = [UIFont fontWithName:@"AvenirNext-Bold" size:12];  //字体
 label.numberOfLines = 1;  //行数
 label.textAlignment = NSTextAlignmentLeft; /NSTextAlignmentCenter  //字体位置

 //混合样式的文字
 UIColor* textColor =  [UIColor whiteColor];
 NSString *text = [NSString stringWithFormat:@"%d/%d",index,count];
 NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];

 [str addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0,1)];

 [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AvenirNext-Bold" size:30.0] range:NSMakeRange(0, 1)];
 [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AvenirNext-DemiBold" size:15.0] range:NSMakeRange(1, 1)];
 [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AvenirNext-DemiBold" size:10.0] range:NSMakeRange(2, (text.length - 2))];
 label.attributedText = str;

 //配合设置label的size,可以设置背景为圆角矩形
 label.layer.masksToBounds = YES;
 label.layer.cornerRadius = 2; //圆角半径
 label.layer.borderWidth = 0;

 // 
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font {
    CGRect rect = [string boundingRectWithSize:CGSizeMake(320, 8000)//限制最大的宽度和高度
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
    
    return rect.size;
}

 

posted @ 2015-11-24 21:13  skyko  阅读(204)  评论(0编辑  收藏  举报