NSMutableAttributedString图文并茂

news/2024/7/20 21:31:19 标签: ios, 图文并茂, 图文混编

通过下面代码可以通过NSTextAttachment将图片以附件的形式插入到属性文字中来达到图文并茂效果;

class ViewController: UIViewController {
    
    @IBOutlet weak var demoLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let attrStr = NSAttributedString(string: "你好:",
        attributes: [NSForegroundColorAttributeName : UIColor.red])
        
        let attrStr1 = NSAttributedString(string: "Tony", 
        attributes: [NSForegroundColorAttributeName : UIColor.blue])
        
        // 图文并茂
        let attacment = NSTextAttachment()
        attacment.image = UIImage(named: "***")
        
        // 获取文字字体
        let fontHeight = demoLabel.font.lineHeight
        attacment.bounds = CGRect(x: 0, y: -4, width: fontHeight, height: fontHeight)
        let attrImageStr = NSAttributedString(attachment: attacment)
        
        let attrMStr = NSMutableAttributedString()
        attrMStr.append(attrStr)
        attrMStr.append(attrImageStr)
        attrMStr.append(attrStr1)
        
        demoLabel.attributedText = attrMStr
    }
}

常用属性:
NSFontAttributeName 字体
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字体颜色
NSBackgroundColorAttributeName 背景颜色
NSShadowAttributeName 阴影
NSStrikethroughStyleAttributeName 删除线格式
NSStrokeColorAttributeName 删除线颜色
NSStrokeWidthAttributeName 删除线宽度
NSUnderlineStyleAttributeName 下划线格式

其他链接:
https://blog.csdn.net/chennai1101/article/details/109638414
https://www.jianshu.com/p/6665c088bd01


http://www.niftyadmin.cn/n/589719.html

相关文章

uniapp+HbuilderX配置打包发布

一个用uniapp开发的跨平台老项目,需要修改点内容然后发布一个新版本。 研究了下配置证书、打包、发布等。 首先修改项目需求,修改版本和build 期间遇到一些问题,无法识别*.vue,需要安装node,此处不简述了,…

android studio 3.1 Android Device Monitor 新的启动方式

https://blog.csdn.net/littlefishvc/article/details/80521058

Swift与OC混编

Swift与OC的混编可以总结出以下几种场景 OC&Swift Mixed In The Same Target Project中OC 调用 SwiftPod中OC 调用 SwiftProject中Swift 调用 OCPod中Swift 调用 OC OC&Swift Mixed In The Different Target OC Project 调用 Swift PodSwift Project 调用 OC PodOC…

VBA 出现400错误

https://blog.csdn.net/xulianboblog/article/details/14230001

通过JavaScript脚本复制网页上的一个表格

<INPUT TYPE"button" value"复制测试表格" οnclick"CopyTable()"> <INPUT TYPE"button" value"将剪切板内容输出到编辑器中" οnclick"PastClipboardData()"><BR> 测试 <TABLE border"…

ld:1 duplicate symbol for architecture x86_64

项目引用.c ,报错 1 duplicate symbol for architecture x86_64 解决办法&#xff1a; 选中.c文件&#xff0c;在右侧出现的选项栏里&#xff0c;取消选中Target Membership的选项。

Android Studio 调试出现waiting for debugger的解决办法

https://blog.csdn.net/wx_jin/article/details/51480487

vs调试代码,变量值有乱码时,如何正确显示乱码的正确值

https://blog.csdn.net/rentingting0312/article/details/80308524