网络上的各类富文本控件总感受和具体项目不太贴,因此我本身动手写了一个富文本控件,是用CoreText写的。如今开放出来供全部人借鉴和使用。我写了一些基本功能,你能够方便的任意加入本身想实现的功能。网络
你能够任意的使用、修改、扩展这个控件,请不要删除做者信息url
使用时,别忘了引入CoreText库。spa
演示效果如图code
调用方法代码以下:orm
一、显示图片图片
- (void)showImage { UIFont *font = [UIFont systemFontOfSize:17]; SJRichTextView *richTextView = [[SJRichTextView alloc] initWithFrame:CGRectMake(100, 50, 200, 80)]; richTextView.textMaxWidth = 150; richTextView.textColor = [UIColor blackColor]; richTextView.font = font; NSString *imageText = [SJRichTextInterpreter imgTextWithUrl:@"demoImage.png" size:CGSizeMake(50, 50)]; richTextView.text = [NSString stringWithFormat:@"图片演示:%@", imageText]; [self.view addSubview:richTextView]; }
二、显示文字(支持按行数计算高度和计算总文字高度)string
- (void)showText { UIFont *font = [UIFont systemFontOfSize:17]; SJRichTextView *richTextView = [[SJRichTextView alloc] initWithFrame:CGRectMake(100, 150, 200, 71)]; richTextView.textMaxWidth = 150; richTextView.textColor = [UIColor blackColor]; richTextView.font = font; richTextView.text = @"文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示文本演示"; [self.view addSubview:richTextView]; CGFloat minHeight = [richTextView heightForLineNumber:4]; NSLog(@"四行文字的高度是:%f", minHeight); CGFloat maxHeight = [richTextView heightForMaxLine]; NSLog(@"总文字高度是:%f", maxHeight); }
三、显示表情it
- (void)showEmotion { UIFont *font = [UIFont systemFontOfSize:17]; SJRichTextView *richTextView = [[SJRichTextView alloc] initWithFrame:CGRectMake(100, 300, 200, 80)]; richTextView.textMaxWidth = 150; richTextView.textColor = [UIColor redColor]; richTextView.font = font; richTextView.emotionIconWidth = font.lineHeight; richTextView.emotionIconHeight = font.lineHeight; richTextView.richTextViewDataSource = self; richTextView.text = @"表情演示:[惟美]"; [self.view addSubview:richTextView]; } #pragma mark SJRichTextViewDataSource - (NSString *)imagePathWithEmotionString:(NSString *)emotionString { if ([emotionString isEqualToString:@"惟美"]) { NSString *imagePath = [NSString stringWithFormat:@"%@/%@", [NSBundle mainBundle].resourcePath, @"demoImage.png"]; return imagePath; } return nil; }
四、显示连接io
- (void)showURL { UIFont *font = [UIFont systemFontOfSize:15]; SJRichTextView *richTextView = [[SJRichTextView alloc] initWithFrame:CGRectMake(50, 400, 300, 80)]; richTextView.textMaxWidth = 300; richTextView.textColor = [UIColor redColor]; richTextView.font = font; richTextView.richTextViewDelete = self; NSString *urlText = [SJRichTextInterpreter urlTextWithUrl:@"http://www.baidu.com" DisplayString:@"百度"]; richTextView.text = [NSString stringWithFormat:@"右边的文字是连接,点点看:%@", urlText]; [self.view addSubview:richTextView]; } #pragma mark SJRichTextViewDelegate - (void)touchUrl:(NSString *)url { NSURL *goURL = [NSURL URLWithString:url]; if (goURL == nil) { return; } [[UIApplication sharedApplication] openURL:goURL]; }
demo项目下载地址:http://pan.baidu.com/share/home?uk=4012188959#category/type=0class