美文网首页
swift5.0 UIlabel的基本使用

swift5.0 UIlabel的基本使用

作者: 要做个太阳 | 来源:发表于2020-04-21 18:08 被阅读0次

刚开始接触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

相关文章

网友评论

      本文标题:swift5.0 UIlabel的基本使用

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