1、同一块文本中设置不同字体、颜色,及换行
代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *text = @"百家姓:\n赵钱孙李,周吴郑王。\n冯陈褚卫,蒋沈韩杨。\n朱秦尤许,何吕施张。\n孔曹严华,金魏陶姜。\n戚谢邹喻,柏水窦章。\n云苏潘葛,奚范彭郎。\n鲁韦昌马,苗凤花方。\n俞任袁柳,酆鲍史唐。\n费廉岑薛,雷贺倪汤。\n滕殷罗毕,郝邬安常。";
CGFloat labelHeight = [self heightOfLabelWithText:text font:[UIFont systemFontOfSize:12.0] width:self.view.frame.size.width-20];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.view.frame.size.width-20, labelHeight)];
label.font = [UIFont systemFontOfSize:12.0];
label.textColor = [UIColor darkGrayColor];
label.attributedText = [self configLabelTextWithText:text label:label boundaryLength:4];
[self.view addSubview:label];
}
#pragma mark - 计算高度
- (CGFloat)heightOfLabelWithText:(NSString *)text font:(UIFont *)font width:(CGFloat)width{
UILabel *testLabel = [[UILabel alloc] init];
testLabel.font = font;
CGRect oldFrame = testLabel.frame;
oldFrame.size.width = width;
[testLabel setFrame:oldFrame];
testLabel.attributedText = [self configLabelTextWithText:text label:testLabel boundaryLength:4];
[testLabel sizeToFit];
return testLabel.frame.size.height;
}
#pragma mark - 设置具体文本属性
- (NSAttributedString *)configLabelTextWithText:(NSString *)labelText label:(UILabel *)label boundaryLength:(NSInteger)boundaryLenght{
NSString *text = labelText; //[NSString stringWithFormat:@"%@",[labelText stringByReplacingOccurrencesOfString:@"\\n" withString:@"\r\n" ]];
label.numberOfLines = 0;
NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName:[[UIColor orangeColor] colorWithAlphaComponent:0.5]} range:NSMakeRange(0, boundaryLenght)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:(8.0-(label.font.lineHeight - label.font.pointSize))];
[paragraphStyle setLineBreakMode:label.lineBreakMode];
[paragraphStyle setAlignment:label.textAlignment];
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
return attributedText;
}
代码实现效果:

2、文本中添加图片
只修改configLabelTextWithText
方法,代码如下:
- (NSAttributedString *)configLabelTextWithText:(NSString *)labelText label:(UILabel *)label boundaryLength:(NSInteger)boundaryLenght{
NSString *text = labelText; // [NSString stringWithFormat:@"%@",[labelText stringByReplacingOccurrencesOfString:@"\\n" withString:@"\r\n" ]];
label.numberOfLines = 0;
NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0],NSForegroundColorAttributeName:[[UIColor orangeColor] colorWithAlphaComponent:0.5]} range:NSMakeRange(0, boundaryLenght)];
//创建NSTextAttachment
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"NaturePatterns02.jpg"];
attachment.bounds = CGRectMake(0, (label.font.capHeight-10)/2.0, 16, 10);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:(8.0-(label.font.lineHeight - label.font.pointSize))];
[paragraphStyle setLineBreakMode:label.lineBreakMode];
[paragraphStyle setAlignment:label.textAlignment];
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedText insertAttributedString:attachmentString atIndex:boundaryLenght+4];
[attributedText insertAttributedString:attachmentString atIndex:text.length-20];
return attributedText;
}
代码运行效果:

3、部分代码解释:
-
1> NSTextAttachment-bounds属性
label添加图片中,attachment.bounds
属性,宽高不用多说,至于纵坐标y值,如果设置为0,图片不一定会和文字对齐,会有一定的偏移,那么如何设置y值呢?
请点击这里查看答案
你会看到有个人这么说:
image.png
没错就是采用:(label.font.capHeight-image.size.height)/2.0
-
2> NSMutableParagraphStyle-lineSpacing属性
lineSpacing属性要想符合UI的要求请参考我之前的文章->iOS-文字行高
-
3> 具体细节参考
原博主文章-UILabel&UITextView文本嵌入图片处理
网友评论