zoukankan      html  css  js  c++  java
  • swift富文本 swift一个label不同颜色字体

    • 此方法适用于textFiled的占位字符
    • textView
    • UILabel
    • UIButton需要文字的地方都可以
    func FuWenBen() -> NSMutableAttributedString {
            
            //定义富文本即有格式的字符串
            let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()
            
            //        左上角星星
            let xingxing = NSAttributedString.init(string: "*", attributes: [NSAttributedString.Key.backgroundColor : UIColor.black , NSAttributedString.Key.foregroundColor : UIColor.red , NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 20) ,NSAttributedString.Key.baselineOffset : (20)])
            
            let wo = NSAttributedString.init(string: "我", attributes: [NSAttributedString.Key.backgroundColor : UIColor.gray , NSAttributedString.Key.foregroundColor : UIColor.green , NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 30)])
            
            let shi : NSAttributedString = NSAttributedString(string: "是", attributes: [NSAttributedString.Key.foregroundColor  : UIColor.purple, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 12)])
            
            //帅逼
            let shuaibi : NSAttributedString = NSAttributedString(string: "帅逼", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lightGray, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 38)])
            
            //图片
            let smileImage  = UIImage.init(named: "tupian")
            let textAttachment : NSTextAttachment = NSTextAttachment()
            textAttachment.image = smileImage
            textAttachment.bounds = CGRect(x: 0, y: -4,  22, height: 22)
            
            attributedStrM.append(xingxing)
            attributedStrM.append(wo)
            attributedStrM.append(shi)
            attributedStrM.append(shuaibi)
            attributedStrM.append(NSAttributedString(attachment: textAttachment))
            
            return attributedStrM
        }
    

      

      效果如下:

  • 相关阅读:
    Asp.net(C#) windows 服务{用于实现计划任务,事件监控等}
    DLL反编译,DLL反编译成C#代码, 有些不良同学会用到哦!
    各种分享按钮代码
    ASP.NET 计划任务(不使用外接程序,.net内部机制实现)
    工厂模式{C#描述}
    FI:Customize FBL3N Layout
    简单工厂 工厂模式 抽象工厂C#
    URL, URI 和 URN 之间的区别转
    resize属性,使textarea在ff(火狐)中禁止拉伸
    UML类图几种关系的总结转
  • 原文地址:https://www.cnblogs.com/huangzhenwei/p/9760126.html
Copyright © 2011-2022 走看看