美文网首页
富文本——图文混排

富文本——图文混排

作者: T92 | 来源:发表于2016-10-28 10:20 被阅读176次
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //富文本是字符串的一种,可以对同一个字符串中的字符设置不同的样式
        
        //设置文字
        //setText()
        
        //设置段落
        //setParagraph()
        
        //插入图片(图文混排)
        setImage()
    }

    func setText(){
        let label1 = UILabel(frame: CGRectMake(10,10,300,50))
        label1.font = UIFont.systemFontOfSize(20)
        self.view.addSubview(label1)
        
        let str1 = "¥300/位"
        //初始化富文本
        let attStr = NSMutableAttributedString(string: str1)
        
        //方式1:分别设置单一属性
        /*
        //大小
        attStr.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(30), range: NSMakeRange(0,4))
        //颜色
        attStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0,4))
        */
        
        //方式2:设置多个属性(修改范围要一致)
        attStr.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(30),NSForegroundColorAttributeName:UIColor.redColor()], range: NSMakeRange(0,4))
        
        //赋值
        label1.attributedText = attStr
    }
    
    func setParagraph(){
        
        let label = UILabel(frame: CGRectMake(0,10,self.view.frame.width,400))
        label.numberOfLines = 0
        
        //设置折行方式
        label.lineBreakMode = .ByCharWrapping
        self.view.addSubview(label)
        
        let str = "MD5是一个安全的散列算法,输入两个不同的明文不会得到相同的输出值,根据输出值,不能得到原始的明文,即其过程不可逆;所以要解密MD5没有现成的算法,只能用穷举法,把可能出现的明文,用MD5算法散列之后,把得到的散列值和原始的数据形成一个一对一的映射表,通过比在表中比破解密码的MD5算法散列值,通过匹配从映射表中找出破解密码所对应的原始明文。"
        //初始化富文本
        let attrStr = NSMutableAttributedString(string: str)
        
        //初始化段落样式
        let paragraphStyle = NSMutableParagraphStyle()
        //行间距
        paragraphStyle.lineSpacing = 20.0
        //段落间距
        paragraphStyle.paragraphSpacing = 10.0
        //设置首行缩进的距离
        paragraphStyle.firstLineHeadIndent = 40
        //设置除首行之外的行缩进距离
        paragraphStyle.headIndent = 20.0
        //将段落样式赋值给富文本
        attrStr.addAttributes([NSParagraphStyleAttributeName:paragraphStyle], range: NSMakeRange(0,(str as NSString).length))
        
        label.attributedText = attrStr
    }
    
    func setImage(){
        
        let label = UILabel(frame: CGRectMake(0,10,self.view.frame.width,400))
        label.numberOfLines = 0
        label.lineBreakMode = .ByWordWrapping
        self.view.addSubview(label)
        
        //初始化富文本(高逼格方式)
        label.text = "MD5是一个安全的散列算法,输入两个不同的明文不会得到相同的输出值"
        //复制一份
        let attStr = label.attributedText?.mutableCopy()
        
        //插入图片
        let image = UIImage(named: "sm.png")
        //初始化图文混排的类
        let attrchment = NSTextAttachment()
        //设置大小(图片的大小)
        attrchment.bounds = CGRectMake(0, 0, 30, 30)
        //将图片赋值给图文混排的类
        attrchment.image = image
        //通过图文混排的类初始化为一个新的富文本字符串
        let attStr2 = NSAttributedString(attachment: attrchment)
        
        //将图片的富文本插入到原来的富文本
        attStr?.insertAttributedString(attStr2, atIndex: 0)
        
        //赋值
        label.attributedText = attStr as! NSMutableAttributedString
    }
}

</br>
</br>

NSBaselineOffsetAttributeName设置偏移量达到文字图片水平居中


</br>

+ (NSAttributedString *)mixImageAndTextWithImageName:(NSString *)imageName text:(NSString *)text{
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = [UIImage imageNamed:imageName];
    NSAttributedString *imageAtt = [NSAttributedString attributedStringWithAttachment:attach];
//    NSAttributedString *textAtt = [[NSAttributedString alloc] initWithString:text];
    NSAttributedString *textAtt = [[NSAttributedString alloc] initWithString:text attributes:@{NSBaselineOffsetAttributeName:@5}];
    NSMutableAttributedString *mutableAtt = [[NSMutableAttributedString alloc] init];
    [mutableAtt appendAttributedString:imageAtt];
    [mutableAtt appendAttributedString:textAtt];
    return mutableAtt;
}

将两个字符串按样式拼接

NSString *str1 = [NSString stringWithFormat:@"¥%@  ",[cellModel.gprice toYuan]];
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str1];
    [att addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(1, str1.length - 1)];
    
    NSString *str2 = [NSString stringWithFormat:@"¥%@ ",[cellModel.gmkprice toYuan]];
    NSMutableAttributedString *att2 = [[NSMutableAttributedString alloc] initWithString:str2];
    [att2 addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, str2.length )];
    [att2 addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, str2.length)];
    
    [att appendAttributedString:att2];
    self.priceLabel.attributedText = att;

相关文章

网友评论

      本文标题:富文本——图文混排

      本文链接:https://www.haomeiwen.com/subject/alasuttx.html