首页 > 代码库 > 富文本控件
富文本控件
网络上的各种富文本控件总感觉和具体项目不太贴,所以我自己动手写了一个富文本控件,是用CoreText写的。现在开放出来供所有人借鉴和使用。我写了一些基本功能,你可以方便的任意加入自己想实现的功能。
你可以任意的使用、修改、扩展这个控件,请不要删除作者信息
使用时,别忘了引入CoreText库。
演示效果如图
调用方法代码如下:
1、显示图片
- (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]; }
2、显示文字(支持按行数计算高度和计算总文字高度)
- (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); }
3、显示表情
- (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; }
4、显示链接
- (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=0
富文本控件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。