swift - NSAttributedString Shadow and Stroke on iOS? -
when use stroke , shadow, sort of double-stroke. how can fix this?
playground code:
import uikit var shadow = nsshadow() shadow.shadowcolor = uicolor.black shadow.shadowoffset = cgsize(width: 0, height: 3) class customlabel: uilabel { override func drawtext(in rect: cgrect) { let attributes: [string: any] = [nsstrokewidthattributename: -2.0, nsstrokecolorattributename: uicolor.black, nsforegroundcolorattributename: uicolor.white, nsshadowattributename: shadow, nsfontattributename: uifont(name: "avenirnext-bold", size: 50)] self.attributedtext = nsattributedstring(string: self.text ?? "", attributes: attributes) super.drawtext(in: rect) } } let label = customlabel(frame: cgrect(x: 0, y: 0, width: 200, height: 100)) label.backgroundcolor = uicolor.orange label.text = "hello"
result:
i figured out. if apply shadow label's calayer, , disable background-color, works expected:
import uikit class customlabel: uilabel { required init?(coder adecoder: nscoder) { super.init(coder: adecoder) } override init(frame: cgrect) { super.init(frame: frame) self.layer.shadowcolor = uicolor.black.cgcolor self.layer.shadowoffset = cgsize(width: 0, height: 3) self.layer.shadowopacity = 1.0 self.layer.shadowradius = 0.0 } override func drawtext(in rect: cgrect) { let attributes: [string: any] = [nsstrokewidthattributename: -2.0, nsstrokecolorattributename: uicolor.black, nsforegroundcolorattributename: uicolor.white, nsfontattributename: uifont(name: "avenirnext-bold", size: 50)] self.attributedtext = nsattributedstring(string: self.text ?? "", attributes: attributes) super.drawtext(in: rect) } } let label = customlabel(frame: cgrect(x: 0, y: 0, width: 200, height: 100)) label.text = "hello"
Comments
Post a Comment