[iOS]富文本编辑器

产品设计要求前端优化让用户能提交和展示更加个性化的内容,还好在网络上有找到类似工具。
工具ZSSRichTextEditor

 效果:
[iOS]富文本编辑器_第1张图片

#import "GATextViewController.h"
#import "ZSSDemoViewController.h"
#import "UILabel+CopyText.h"

@interface GATextViewController ()

@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (strong, nonatomic) NSString *htmlString;


@end

@implementation GATextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILongPressGestureRecognizer *tapText = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapTextCopyAction:)];
    [_showLabel addGestureRecognizer:tapText];
    
    _htmlString = @"今天我不回家吃饭,你自己解决晚餐,注意关掉空调。  ";
    [self setTopicLabelWithLabel:_showLabel Text:_htmlString];
}

// 复制
- (void)tapTextCopyAction:(UILongPressGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
        [_showLabel showMemuController:_showLabel.bounds];
    }
}

// 编辑
- (IBAction)tapTextLabelAction:(id)sender {
    [self enterEditTextWithContext:_htmlString];
}

// 进入文字编辑页面
- (void)enterEditTextWithContext:(NSString *)context {
    ZSSDemoViewController *vc = [[ZSSDemoViewController alloc] init];
    vc.context = context;
    __weak typeof(self) weakSelf = self;
    [vc setEditBlock:^(NSString *newContext) {
        weakSelf.htmlString = newContext;
        [self setTopicLabelWithLabel:weakSelf.showLabel Text:weakSelf.htmlString];
    }];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

// 设置文本
- (void)setTopicLabelWithLabel:(UILabel *)theLabel Text:(NSString *)text {
    if (!theLabel) {
        return;
    }
    if (![self judgeStringIsNull:text]) {
        text = @"";
    }
    text = [NSString stringWithFormat:@"
%@
",text]; NSMutableAttributedString *attrStr = [theLabel.attributedText mutableCopy]; [attrStr replaceCharactersInRange:NSMakeRange(0, attrStr.length) withString:@""]; NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; // 设置行间距 [paragraph setLineSpacing:5]; // 设置段间距 [paragraph setParagraphSpacingBefore:5]; [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, attrStr.length)]; [attrStr readFromData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil]; theLabel.attributedText = attrStr; theLabel.numberOfLines = 0; // 自动换行,必须设置该属性,否则需自行添加高度 theLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20; } // 判断字符串是否不全为空 - (BOOL)judgeStringIsNull:(NSString *)string { if (![[string class] isSubclassOfClass:[NSString class]]) { return NO; } if ([[string class] isSubclassOfClass:[NSNumber class]]) { return YES; } BOOL result = NO; if (string != nil && string.length > 0) { for (int i = 0; i < string.length; i ++) { NSString *subStr = [string substringWithRange:NSMakeRange(i, 1)]; if (![subStr isEqualToString:@" "] && ![subStr isEqualToString:@""]) { result = YES; } } } return result; } @end

不要下载,备份Demo:https://download.csdn.net/download/u012881779/11329348

你可能感兴趣的:([iOS]学习笔记)