swift5.0 UIlabel的基本使用

刚开始接触swift 的 UI部分开发的时候,没有找到快捷有效的资料,因此在自己学习的时候做了下笔记,以便于后面来学习swift的同学来参考(因为只是作为自己的笔记使用,可能有的地方写的不太严谨,还请大家多多包涵和指出~)

    let labelMy :UILabel = UILabel(frame: CGRect(x: 10, y: 100, width: 500, height: 50))
    self.view .addSubview(labelMy)
    labelMy.text = "很高兴见到你,我是一条字符串呀"
   // labelMy.tintColor = UIColor.purple
    labelMy.textColor = UIColor.white
    labelMy.font = UIFont.boldSystemFont(ofSize: 12)
    labelMy.backgroundColor = UIColor.orange
    labelMy.textAlignment = NSTextAlignment.left
    labelMy.numberOfLines = 2
    //设UIlabel文字的阴影效果
    labelMy.shadowColor = UIColor.purple
    labelMy.shadowOffset = CGSize.init(width: 5, height: 2)

  //UIlabel的富文本设置
    let attributeString  = NSMutableAttributedString.init(string: "很高兴见到你,我是一条字符串呀")
  
   attributeString.addAttribute(NSAttributedString.Key.font, value:  UIFont.init(name: "HelveticaNeue-Bold", size: 20) ?? UIFont.boldSystemFont(ofSize: 20), range: NSRange.init(location: 2, length: 3))

//设置特定位置文字的颜色    
    attributeString.addAttribute(NSMutableAttributedString.Key.foregroundColor, value: UIColor.black, range: NSMakeRange(3, 4))
    
    //设置特定文字背景颜色
  attributeString.addAttribute(NSMutableAttributedString.Key.backgroundColor, value: UIColor.blue, range: NSMakeRange(6, 7))
    
    labelMy.attributedText = attributeString
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容