码迷,mamicode.com
首页 > 移动开发 > 详细

【IOS】UILabel 属性及方法

时间:2015-11-24 22:09:53      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

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

 

【IOS】UILabel 属性及方法

标签:

原文地址:http://www.cnblogs.com/afluy/p/4992926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!