Outline UILabel text in UILabel Subclass

后端 未结 6 1894
你的背包
你的背包 2020-12-31 09:52

I\'m trying hard to find a way to simply add an outline/stroke/contour to my UILabel text. Talking about a stroke around the letters of the text not around the background of

6条回答
  •  醉梦人生
    2020-12-31 10:39

    This code works for me.

    Swift 3

    let strokeTextAttributes = [
      NSStrokeColorAttributeName : UIColor.black,
      NSForegroundColorAttributeName : UIColor.white,
      NSStrokeWidthAttributeName : -4.0,
      NSFontAttributeName : UIFont.boldSystemFont(ofSize: 30)
    ] as [String : Any]
    
    myLabel.attributedText = NSMutableAttributedString(string: "Test me i have color.", attributes: strokeTextAttributes)
    



    Swift 4.2 & 5.1

    let strokeTextAttributes = [
      NSAttributedString.Key.strokeColor : UIColor.red,
      NSAttributedString.Key.foregroundColor : UIColor.white,
      NSAttributedString.Key.strokeWidth : -4.0,
      NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 30)]
      as [NSAttributedString.Key : Any]
    
    labelOutLine.attributedText = NSMutableAttributedString(string: "Your outline text", attributes: strokeTextAttributes)
    

提交回复
热议问题