Skip to content

Commit 90c0041

Browse files
committed
feat rgb support / setNeedsDisplay moved to view
1 parent 69201de commit 90c0041

File tree

2 files changed

+84
-83
lines changed

2 files changed

+84
-83
lines changed

ios/StrokeTextView.swift

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,91 +30,132 @@ class StrokeTextView: RCTView {
3030

3131
@objc var width: NSNumber = 0 {
3232
didSet {
33-
self.label.customWidth = CGFloat(truncating: width)
33+
if width != oldValue {
34+
self.label.customWidth = CGFloat(truncating: width)
35+
label.setNeedsDisplay()
36+
}
3437
}
3538
}
3639

3740
@objc var text: String = "" {
3841
didSet {
39-
label.text = text
42+
if text != oldValue {
43+
label.text = text
44+
label.setNeedsDisplay()
45+
}
4046
}
4147
}
4248

4349
@objc var fontSize: NSNumber = 14 {
4450
didSet {
45-
label.font = label.font.withSize(CGFloat(truncating: fontSize))
51+
if fontSize != oldValue {
52+
label.font = label.font.withSize(CGFloat(truncating: fontSize))
53+
label.setNeedsDisplay()
54+
}
4655
}
4756
}
4857

4958
@objc var color: String = "#000000" {
5059
didSet {
51-
label.textColor = hexStringToUIColor(hexColor: color)
60+
if color != oldValue {
61+
label.textColor = colorStringToUIColor(colorString: color)
62+
label.setNeedsDisplay()
63+
}
5264
}
5365
}
5466

5567
@objc var strokeColor: String = "#FFFFFF" {
5668
didSet {
57-
label.outlineColor = hexStringToUIColor(hexColor: strokeColor)
69+
if strokeColor != oldValue {
70+
label.outlineColor = colorStringToUIColor(colorString: strokeColor)
71+
label.setNeedsDisplay()
72+
}
5873
}
5974
}
6075

6176
@objc var strokeWidth: NSNumber = 1 {
6277
didSet {
63-
label.outlineWidth = CGFloat(truncating: strokeWidth)
78+
if strokeWidth != oldValue {
79+
label.outlineWidth = CGFloat(truncating: strokeWidth)
80+
label.setNeedsDisplay()
81+
}
6482
}
6583
}
6684

6785
@objc var fontFamily: String = "Helvetica" {
6886
didSet {
69-
if let font = UIFont(name: fontFamily, size: CGFloat(truncating: fontSize)) {
70-
label.font = font
87+
if fontFamily != oldValue {
88+
if let font = UIFont(name: fontFamily, size: CGFloat(truncating: fontSize)) {
89+
label.font = font
90+
}
91+
label.setNeedsDisplay()
7192
}
7293
}
7394
}
7495

7596
@objc var align: String = "center" {
7697
didSet {
77-
if align == "left" {
78-
label.align = .left
79-
}else if align == "right" {
80-
label.align = .right
81-
}else{
82-
label.align = .center
98+
if align != oldValue {
99+
if align == "left" {
100+
label.align = .left
101+
} else if align == "right" {
102+
label.align = .right
103+
} else {
104+
label.align = .center
105+
}
106+
107+
label.setNeedsDisplay()
83108
}
84109
}
85110
}
86111

87112
@objc var ellipsis: Bool = false {
88113
didSet {
89-
label.ellipsis = ellipsis
114+
if ellipsis != oldValue {
115+
label.ellipsis = ellipsis
116+
label.setNeedsDisplay()
117+
}
90118
}
91119
}
92120

93121
@objc var numberOfLines: NSNumber = 0 {
94122
didSet {
95-
label.numberOfLines = Int(truncating: numberOfLines)
123+
if numberOfLines != oldValue {
124+
label.numberOfLines = Int(truncating: numberOfLines)
125+
label.setNeedsDisplay()
126+
}
96127
}
97128
}
98129

99-
private func hexStringToUIColor(hexColor: String) -> UIColor {
100-
var cString: String = hexColor.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
130+
private func colorStringToUIColor(colorString: String) -> UIColor {
131+
var string = colorString.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
101132

102-
if cString.hasPrefix("#") {
103-
cString.removeFirst()
104-
}
105-
106-
if cString.count != 6 {
107-
return UIColor.gray
133+
if string.hasPrefix("#") {
134+
if string.count == 4 {
135+
string = "#" + string.dropFirst().map { "\($0)\($0)" }.joined()
136+
}
137+
if string.count == 7 {
138+
var rgbValue: UInt64 = 0
139+
Scanner(string: String(string.dropFirst())).scanHexInt64(&rgbValue)
140+
return UIColor(
141+
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
142+
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
143+
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
144+
alpha: 1.0
145+
)
146+
}
147+
} else if string.hasPrefix("RGBA") {
148+
let components = string.dropFirst(5).dropLast(1).split(separator: ",").map { CGFloat(Double($0.trimmingCharacters(in: .whitespaces)) ?? 0) }
149+
if components.count == 4 {
150+
return UIColor(red: components[0] / 255.0, green: components[1] / 255.0, blue: components[2] / 255.0, alpha: components[3])
151+
}
152+
} else if string.hasPrefix("RGB") {
153+
let components = string.dropFirst(4).dropLast(1).split(separator: ",").map { CGFloat(Double($0.trimmingCharacters(in: .whitespaces)) ?? 0) }
154+
if components.count == 3 {
155+
return UIColor(red: components[0] / 255.0, green: components[1] / 255.0, blue: components[2] / 255.0, alpha: 1.0)
156+
}
108157
}
109158

110-
var rgbValue: UInt64 = 0
111-
Scanner(string: cString).scanHexInt64(&rgbValue)
112-
113-
return UIColor(
114-
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
115-
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
116-
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
117-
alpha: CGFloat(1.0)
118-
)
159+
return UIColor.gray
119160
}
120161
}

ios/StrokedTextLabel.swift

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,20 @@ class StrokedTextLabel: UILabel {
1010
self.numberOfLines = 0
1111
}
1212

13-
var outlineWidth: CGFloat = 0 {
14-
didSet {
15-
if outlineWidth != oldValue {
16-
updateTextInsets()
17-
setNeedsDisplay()
18-
}
19-
}
20-
}
21-
22-
var outlineColor: UIColor = .clear {
23-
didSet {
24-
if outlineColor != oldValue {
25-
setNeedsDisplay()
26-
}
27-
}
28-
}
13+
private var textInsets: UIEdgeInsets = .zero
2914

30-
var align: NSTextAlignment = .center {
31-
didSet {
32-
if align != oldValue {
33-
setNeedsDisplay()
34-
}
35-
}
36-
}
37-
38-
var customWidth: CGFloat = 0 {
39-
didSet {
40-
if customWidth != oldValue {
41-
setNeedsDisplay()
42-
}
43-
}
44-
}
45-
46-
var ellipsis: Bool = false {
47-
didSet {
48-
if ellipsis != oldValue {
49-
setNeedsDisplay()
50-
}
51-
}
52-
}
53-
54-
private var textInsets: UIEdgeInsets = .zero {
55-
didSet {
56-
if textInsets != oldValue {
57-
invalidateIntrinsicContentSize()
58-
}
59-
}
60-
}
61-
62-
private func updateTextInsets() {
15+
public func updateTextInsets() {
6316
let strokePadding = outlineWidth / 2
6417
textInsets = UIEdgeInsets(top: 0, left: strokePadding, bottom: 0, right: strokePadding)
6518
}
6619

20+
var outlineWidth: CGFloat = 0
21+
var outlineColor: UIColor = .clear
22+
var align: NSTextAlignment = .center
23+
var customWidth: CGFloat = 0
24+
var ellipsis: Bool = false
25+
26+
6727
override func drawText(in rect: CGRect) {
6828
let shadowOffset = self.shadowOffset
6929
let textColor = self.textColor
@@ -93,7 +53,7 @@ class StrokedTextLabel: UILabel {
9353
var contentSize = super.intrinsicContentSize
9454
if customWidth > 0 {
9555
contentSize.width = customWidth
96-
}else{
56+
} else {
9757
contentSize.width += (textInsets.left + textInsets.right)
9858
}
9959

0 commit comments

Comments
 (0)