開發視頻直播app,遇到了這么一個問題,需要實現下面的UI
剛開始看到這圖片的時候,感覺沒什么,一個圖片,兩個label添加到cell.contentView,再讓單元格高度自適應,so easy。好吧,事實證明圖樣圖森破,問題如下:
沒錯,消息label的位置會從紅線開始,也就代表著文字“對方的哥哥發”不會從單元格的最左邊開始,會從紅線位置開始,基于這個原因,我產生了一個想法:
? ? ? ? ? ? cell.contentView add一個label,label上加載等級圖片,label的開始前面填寫空格,為了讓圖片不遮蓋文字
但是我沒有按照這個思路走,因為我又想起來以前看過的一個名詞,富文本,于是開始接觸了解,并實現。
1,制作富文本,將一段文字中不通的字顯示不同的顏色,大小等。
如圖,實現“干”字顏色和大小的變化
self.chatLabel.frame=self.contentView.bounds;
//富文本
NSMutableAttributedString* attributedStr = [[NSMutableAttributedString alloc]initWithString:@"干:一個人的夜,我的心,應該放在那里"];
[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.0f] range:NSMakeRange(0,1)];//range代表“干”字的位置
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor]range:NSMakeRange(0,1)];
self.chatLabel.attributedText= attributedStr;
[self.contentView addSubview:self.chatLabel];
效果:
2,制作富文本,將圖片和文字混排
NSMutableAttributedString* attributedStr = [[NSMutableAttributedString alloc]initWithString:@"干:一個人的夜,我的心,應該放在那里"attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
NSTextAttachment* attachment = [[NSTextAttachment alloc]initWithData:nil ofType:nil];
UIImage*image = [UIImage imageNamed:@"Image_1"];
attachment.image= image;
attachment.bounds=CGRectMake(0,0,50,20);
NSAttributedString*text = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedStr insertAttributedString:text atIndex:0];
self.chatLabel.attributedText= attributedStr;
[self.contentView addSubview:self.chatLabel];
效果:
3,制作富文本,定制需求,圖片的高度不想每個都要設置一遍
創建一個繼承自NSTextAttachment的類,重寫-(CGRect)attachmentBoundsForTextContainer:(NSTextContainer*)textContainerproposedLineFragment:(CGRect)lineFragglyphPosition:(CGPoint)positioncharacterIndex:(NSUInteger)charIndex方法
。h
#import
@interface myTextAttachment?:?NSTextAttachment
@end
。m
#import "myTextAttachment.h"
@implementationmy TextAttachment
-(CGRect)attachmentBoundsForTextContainer:(NSTextContainer*)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
{
return CGRectMake(0,0,?lineFrag.size.height,?lineFrag.size.height);
}
@end
然后在自定義cell中這樣寫
NSMutableAttributedString* attributedStr = [[NSMutableAttributedString alloc]initWithString:@"干:一個人的夜,我的心,應該放在那里" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
myTextAttachment*attachment = [[myTextAttachment alloc]init];
UIImage*image = [UIImage imageNamed:@"Image_1"];
attachment.image= image;
NSAttributedString*text = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedStr insertAttributedString:text atIndex:0];
self.chatLabel.attributedText= attributedStr;
[self.contentView addSubview:self.chatLabel];
4,制作富文本,替換掉一些特殊字符
如果想動態改變圖片怎么辦?例如
圖片中的等級,18,49,18都是不一樣的,怎么搞?這樣搞
NSMutableAttributedString* attributedStr = [[NSMutableAttributedString alloc]initWithString:@"[icon]干:一個人的夜,我的心,應該放在那里"];//注意,[icon]表示要替換的字符,把icon替換成圖片
NSTextAttachment* attachment = [[NSTextAttachment alloc]init];
UIImage* image = [UIImageimageNamed:@"Image_1"];
attachment.image= image;
NSAttributedString* text = [NSAttributedString attributedStringWithAttachment:attachment];
NSRange range = [[attributedStr string]rangeOfString:@"[icon]"];
[attributedStr replaceCharactersInRange:range withAttributedString:text];
self.chatLabel.attributedText= attributedStr;
[self.contentView addSubview:self.chatLabel];
效果圖:
和上面沒差,只是這個是用替換字符串實現的
補充點知識:
1.為某一范圍內文字設置多個屬性
- (void)setAttributes:(NSDictionary*)attrs range:(NSRange)range;
為某一范圍內文字添加某個屬性
- (void)addAttribute:(NSString*)name value:(id)value range:(NSRange)range;
為某一范圍內文字添加多個屬性
- (void)addAttributes:(NSDictionary*)attrs range:(NSRange)range;
移除某范圍內的某個屬性
- (void)removeAttribute:(NSString*)name range:(NSRange)range;
2.常見的屬性及說明
NSFontAttributeName字體
NSParagraphStyleAttributeName段落格式
NSForegroundColorAttributeName字體顏色
NSBackgroundColorAttributeName背景顏色
NSStrikethroughStyleAttributeName刪除線格式
NSUnderlineStyleAttributeName下劃線格式
NSStrokeColorAttributeName刪除線顏色
NSStrokeWidthAttributeName刪除線寬度
NSShadowAttributeName陰影
好吧,下面是你們想要的Demo
Demo地址富文本Demo