富文本控件

  網絡上的各類富文本控件總感受和具體項目不太貼,因此我本身動手寫了一個富文本控件,是用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

相關文章
相關標籤/搜索