//lable字体大小自适应宽度
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 100)];
lable.numberOfLines = 0;
lable.backgroundColor = [UIColor blueColor];
lable.text = @"abchdh";
lable.font = [UIFont systemFontOfSize:200];
lable.adjustsFontSizeToFitWidth = YES;
lable.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
[self.view addSubview:lable];
//富文本的设置
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已抢购 %@ 个",model.getNum]];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, getNumString.length -1)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, getNumString.length - 6)];
[_getNumLabel setAttributedText:attributedString];
//指定table,textView的行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5;// 字体的行间距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:13],
NSParagraphStyleAttributeName:paragraphStyle
};
_contentTextView.attributedText = [[NSAttributedString alloc] initWithString:str attributes:attributes];
//动态lable高度
- (CGSize)contentSize:(UILabel *)lable {
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = lable.lineBreakMode;
paragraphStyle.alignment = lable.textAlignment;
NSDictionary * attributes = @{NSFontAttributeName : lable.font,
NSParagraphStyleAttributeName : paragraphStyle};
CGSize contentSize = [lable.text boundingRectWithSize:CGSizeMake(lable.frame.size.width, MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attributes
context:nil].size;
return contentSize;
}
//指定区域内文字加横线
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 200, 50)];
[self.view addSubview:label];
label.text = @"12.89";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor grayColor]; // 横线的颜色跟随label字体颜色改变
NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",label.text]];
[newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
label.attributedText = newPrice;