iOS 富文本如何添加图片

//联系人:石虎 QQ:1224614774 昵称:嗡嘛呢叭咪哄

                       QQ群:807236138  群称:iOS 技术交流学习群

一、概念

    1.添加图片效果图

    2.富文本添加图片代码

    3.富文本总结

    4.直接拷贝代码就可以用

 

二、添加图片效果图

图1:

图2:

三、富文本添加图片代码

//  ViewController.m
//  测试富文本
//
//  Created by joyshow on 2018/7/10.
//  Copyright © 2018年 石虎. All rights reserved.


#import "ViewController.h"
@interface ViewController ()
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //1.设置标签

    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    titleLabel.backgroundColor = [UIColor yellowColor];
    titleLabel.text = @"石虎祝所有人步步高升,成为技术大神";
    titleLabel.textColor = [UIColor redColor];
    [self.view addSubview:titleLabel];


    //2.初始化富文本对象
     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleLabel.text];
    //2.1修改富文本中的不同文字的样式
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];//字体颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(7, 6)];//字体颜色
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(0, 6)];//字体大小

    
    //3.初始化NSTextAttachment对象
    NSTextAttachment *attchment = [[NSTextAttachment alloc]init];
    attchment.bounds = CGRectMake(0, 0, 40, 40);//设置frame
    attchment.image = [UIImage imageNamed:@"release_homework"];//设置图片


    //4.创建带有图片的富文本
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)];
    [attributedString insertAttributedString:string atIndex:0];//插入到第几个下标
    [attributedString appendAttributedString:string];   //添加到尾部


    //5.用label的attributedText属性来使用富文本
    titleLabel.attributedText = attributedString;

}

@end

 

四、富文本总结

这是富文本的所有属性

属性Name干啥的类型
NSFontAttributeName字号UIFont 默认12
NSParagraphStyleAttributeName段落样式NSParagraphStyle
NSForegroundColorAttributeName前景色UIColor
NSBackgroundColorAttributeName背景色UIColor
NSObliquenessAttributeName字体倾斜NSNumber
NSExpansionAttributeName字体加粗NSNumber 比例 0就是不变 1增加一倍
NSKernAttributeName字间距CGFloat
NSUnderlineStyleAttributeName下划线1或0
NSUnderlineColorAttributeName下划线颜色UIColor
NSStrikethroughStyleAttributeName删除线1或0
NSStrikethroughColorAttributeName删除线颜色UIColor
NSStrokeColorAttributeNamesame as ForegroundColorUIColor
NSStrokeWidthAttributeName字体描边CGFloat
NSLigatureAttributeName连笔字 没看出效果1或0
NSShadowAttributeName阴影NSShawdow
NSTextEffectAttributeName设置文本特殊效果,目前只有图版印刷效果可用NSString
NSAttachmentAttributeName设置文本附件,常用插入图片NSTextAttachment
NSLinkAttributeName链接NSURL (preferred) or NSString
NSBaselineOffsetAttributeName基准线偏移NSNumber
NSWritingDirectionAttributeName文字方向 分别代表不同的文字出现方向等等,我想你一定用不到它 - -@[@(1),@(2)]
NSVerticalGlyphFormAttributeName水平或者竖直文本 在iOS没卵用,不支持竖版1竖直 0水平

谢谢!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值