iOS 简易富文本使用教程

Overview

本文所使用的第三方富文本工具类由YouXianMing大神提供。
https://www.cnblogs.com/YouXianMing/p/3875542.html

注意事项

1、在使用UITextView的富文本时 allowsEditingTextAttributes 必须设为 YES 否则会出现无法改变字体大小的问题,是否存在其他问题待发现。

textView.allowsEditingTextAttributes = YES;

2、iOS10.3开始给文字添加删除线会不起作用,需要添加以下代码

[attributedString addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),NSBaselineOffsetAttributeName:@(0)} range:deleteLineRange];

可点击的富文本

    //协议
    UITextView * protocolTextView = [self protocol];
    [self.view addSubview:protocolTextView];
####pragma mark- 协议
- (UITextView *)protocol{

    UITextView * textView = [[UITextView alloc] init];

    NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];
    [ps setAlignment:NSTextAlignmentCenter];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"注册即表示您已阅读,并同意《服务条款》"];
    [attributedString addAttribute:NSLinkAttributeName
                                  value:@"fuwu://"
                                  range:[[attributedString string] rangeOfString:@"《服务条款》"]];

    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:kHeight(22)] range:NSMakeRange(0, attributedString.length)];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:ps range:NSMakeRange(0, attributedString.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:RGB_HEX(0x535353) range:NSMakeRange(0, attributedString.length)];

    textView.attributedText = attributedString;
    textView.linkTextAttributes = @{NSForegroundColorAttributeName: RGB_HEX(0x89a3d1),
                                    NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                    };

    textView.delegate = self;
    textView.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘
    textView.scrollEnabled = NO;

    return textView;
}

点击《服务条款》后就会执行以下方法,前提textView.delegate = self;

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    if ([[URL scheme] isEqualToString:@"fuwu"]) {

        //服务条款
        //具体的业务逻辑

        return NO;
    } 
    return YES;
}

不同的文字颜色

- (NSMutableAttributedString *)cashPriceAttributedWithString:(NSString *)price originalPrice:(NSString *)originalPrice {

    NSString * str = [NSString stringWithFormat:@"凭此券到店支付%@可抵%@",price,originalPrice];
    //数字
    NSRange priceRange = NSMakeRange(7, [price length]);
    NSRange originalPriceRange = NSMakeRange(7+[price length]+2, [originalPrice length]);

    NSMutableAttributedString * attributedString = [str createAttributedStringAndConfig:@[
                                                                                          [ConfigAttributedString foregroundColor:RGB_HEX(0xcbb693) range:priceRange],
                                                                                          [ConfigAttributedString foregroundColor:RGB_HEX(0xcbb693) range:originalPriceRange],
                                                                                          ]];
    return attributedString;

}

调整文字位置

图中的红点,不是图片是输入法符号里找出来的。
可能存在红点不和文字对齐的情况,所以要用到基线偏移 NSBaselineOffsetAttributeName

- (NSMutableAttributedString *)bargainAttributedString:(NSString *)string {

    NSString * str = [NSString stringWithFormat:@"● %@",string];
    NSRange range = NSMakeRange(0, 1);

    NSMutableAttributedString * attributedString = [str createAttributedStringAndConfig:@[
                                                                                          [ConfigAttributedString foregroundColor:RGB_HEX(0xCC5354) range:range],
                                                                                          [ConfigAttributedString font:[UIFont boldSystemFontOfSize:6.f] range:range],

                                                                                          ]];
    //NSBaselineOffsetAttributeName 设置基线偏移值。取值为NSNumber (float),正值上偏,负值下偏
    [attributedString addAttribute:NSBaselineOffsetAttributeName value:@(3) range:[str rangeOfString:@"●"]];

    return attributedString;
}

文字不同大小

图中左边¥和右边100大小不一样

//string = ¥100
- (NSMutableAttributedString *)priceAttributedString:(NSString *)string {

    //优惠价
    NSRange vipPriceRange = NSMakeRange(1, [string length]-1);
    NSMutableAttributedString * attributedString = [string createAttributedStringAndConfig:@[
                                                                                             [ConfigAttributedString font:[UIFont systemFontOfSize:42.f] range:vipPriceRange],
                                                                                             ]];
    return attributedString;
}

文字删除线

image

图中右下角原优惠价150带删除线

- (NSMutableAttributedString *)originalAttributedString:(NSString *)string {


    //删除线
    NSRange deleteLineRange = NSMakeRange(0, [string length]);

    NSMutableAttributedString * attributedString = [string createAttributedStringAndConfig:@[
                                                                                                [ConfigAttributedString strikethroughStyle:NSUnderlineStyleSingle range:deleteLineRange],

                                                                                             ]];

    [attributedString addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),NSBaselineOffsetAttributeName:@(0)} range:deleteLineRange];

    return attributedString;
}

博主大大

假如生活欺骗了你,不要悲伤,不要难过,反正明天也一样......

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

猜你喜欢