Skip to content

Commit 54167f0

Browse files
authored
Adds support for label.text and native fields on LCLabel that are inhereted from UILabel (#33)
Bumps podspec Adds a tiny xctassertEquality
1 parent 42a8bb2 commit 54167f0

File tree

6 files changed

+191
-10
lines changed

6 files changed

+191
-10
lines changed

LCLabel.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'LCLabel'
3-
s.version = '0.6.0'
3+
s.version = '1.1.0'
44
s.summary = 'LCLabel is a TextKit 2 based UILabel'
55
s.description = "LCLabel is a TextKit 2 based UILabel that mimics a the behaviour of UITextView"
66
s.homepage = 'https://github.com/mustiikhalil/LCLabel'
14.8 KB
Loading
14.8 KB
Loading
5.27 KB
Loading

Sources/LCLabel/LCLabel.swift

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,78 @@ final public class LCLabel: UILabel {
8787
refreshView()
8888
}
8989
}
90-
/// Currently doesnt support fonts
90+
91+
public override var text: String? {
92+
get {
93+
renderedStorage?.string
94+
}
95+
set {
96+
currentlySelectedLink = nil
97+
// Removes the current text container since we are resetting it
98+
if let text = newValue {
99+
let paragraphStyle = NSMutableParagraphStyle()
100+
paragraphStyle.alignment = textAlignment
101+
renderedStorage = NSTextStorage(
102+
string: text,
103+
attributes: [
104+
.font: font ?? .systemFont(ofSize: 17),
105+
.foregroundColor: textColor ?? .black,
106+
.paragraphStyle: paragraphStyle,
107+
])
108+
isHidden = false
109+
} else {
110+
renderedStorage = nil
111+
isHidden = true
112+
}
113+
renderedStorage?.addLayoutManager(layoutManager)
114+
setupRenderStorage()
115+
refreshView()
116+
}
117+
}
91118
public override var font: UIFont! {
92-
get { nil }
93-
set { }
119+
get {
120+
super.font
121+
}
122+
set {
123+
super.font = newValue
124+
if let value = newValue {
125+
renderedStorage?.addAttribute(
126+
.font,
127+
value: value,
128+
range: textRange)
129+
}
130+
refreshView()
131+
}
94132
}
95-
/// Currently doesnt support textColor
96133
public override var textColor: UIColor! {
97-
get { nil }
98-
set { }
134+
get {
135+
super.textColor
136+
}
137+
set {
138+
super.textColor = newValue
139+
if let value = newValue {
140+
renderedStorage?.addAttribute(
141+
.foregroundColor,
142+
value: value,
143+
range: textRange)
144+
}
145+
refreshView()
146+
}
99147
}
100-
/// Currently doesnt support textAlignment
101148
public override var textAlignment: NSTextAlignment {
102-
get { .natural }
103-
set { }
149+
get {
150+
super.textAlignment
151+
}
152+
set {
153+
super.textAlignment = newValue
154+
let paragraphStyle = NSMutableParagraphStyle()
155+
paragraphStyle.alignment = textAlignment
156+
renderedStorage?.addAttribute(
157+
.paragraphStyle,
158+
value: paragraphStyle,
159+
range: textRange)
160+
refreshView()
161+
}
104162
}
105163
/// Number of lines allowed
106164
public override var numberOfLines: Int {
@@ -271,6 +329,10 @@ final public class LCLabel: UILabel {
271329
private var currentlySelectedLink: URL?
272330
private var _accessibilityTraits: UIAccessibilityTraits
273331

332+
private var textRange: NSRange {
333+
NSRange(location: 0, length: renderedStorage?.length ?? 0)
334+
}
335+
274336
private func setupUI() {
275337
textContainer.lineFragmentPadding = 0
276338
textContainer.lineBreakMode = .byTruncatingTail

Tests/LCLabelTests/LCLabelTests.swift

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ final class LCLabelTests: XCTestCase {
2727
matching: label,
2828
as: .image,
2929
snapshotDirectory: path)
30+
XCTAssertEqual(attStr.string, label.accessibilityIdentifier)
3031
guard let message = failure else { return }
3132
XCTFail(message)
3233
}
@@ -363,6 +364,64 @@ final class LCLabelTests: XCTestCase {
363364
XCTFail(message)
364365
}
365366

367+
func testTextString() {
368+
let label = LCLabel(frame: .zero)
369+
label.frame = CGRect(x: 0, y: 0, width: 300, height: 40)
370+
label.centeringTextAlignment = .center
371+
label.numberOfLines = 1
372+
label.backgroundColor = .black
373+
label.text = "Welcome"
374+
label.textColor = .red
375+
label.textAlignment = .center
376+
label.font = .systemFont(ofSize: 10)
377+
let failure = verifySnapshot(
378+
matching: label,
379+
as: .image,
380+
snapshotDirectory: path)
381+
guard let message = failure else { return }
382+
XCTFail(message)
383+
}
384+
385+
func testMultiLineTextString() {
386+
let label = LCLabel(frame: .zero)
387+
label.frame = CGRect(x: 0, y: 0, width: 300, height: 40)
388+
label.centeringTextAlignment = .center
389+
label.numberOfLines = 2
390+
label.backgroundColor = .black
391+
label
392+
.text =
393+
"Welcome to a very long long text that should be added to two different lines"
394+
label.textColor = .red
395+
label.textAlignment = .center
396+
label.font = .systemFont(ofSize: 10)
397+
let failure = verifySnapshot(
398+
matching: label,
399+
as: .image,
400+
snapshotDirectory: path)
401+
guard let message = failure else { return }
402+
XCTFail(message)
403+
}
404+
405+
func testMultiLinePreSetupTextString() {
406+
let label = LCLabel(frame: .zero)
407+
label.frame = CGRect(x: 0, y: 0, width: 300, height: 40)
408+
label.centeringTextAlignment = .center
409+
label.numberOfLines = 2
410+
label.backgroundColor = .black
411+
label.textColor = .red
412+
label.font = .systemFont(ofSize: 10)
413+
label.textAlignment = .center
414+
label
415+
.text =
416+
"Welcome to a very long long text that should be added to two different lines"
417+
let failure = verifySnapshot(
418+
matching: label,
419+
as: .image,
420+
snapshotDirectory: path)
421+
guard let message = failure else { return }
422+
XCTFail(message)
423+
}
424+
366425
func testStringWithoutValidations() {
367426
let attStr = NSMutableAttributedString(
368427
string: "LCLabel",
@@ -613,6 +672,66 @@ final class LCLabelTests: XCTestCase {
613672
XCTFail(message)
614673
}
615674

675+
func testURLHitTest() {
676+
let attStr = NSMutableAttributedString(
677+
string: "LCLabel",
678+
attributes: [
679+
.foregroundColor: UIColor.white,
680+
.font: UIFont.systemFont(ofSize: 14),
681+
])
682+
attStr.append(NSAttributedString(
683+
string: "\n@LCLabel",
684+
attributes: [
685+
.foregroundColor: UIColor.white,
686+
.font: UIFont.systemFont(ofSize: 10),
687+
.link: URL(string: "lclabel://welcome")!,
688+
]))
689+
let rect = CGRect(x: 0, y: 0, width: 300, height: 40)
690+
let label = LCLabel(frame: .zero)
691+
label.isUserInteractionEnabled = true
692+
label.frame = rect
693+
label.centeringTextAlignment = .center
694+
label.numberOfLines = 2
695+
label.linkStyleValidation = .ensure
696+
label.linkAttributes = [
697+
.foregroundColor: UIColor.green,
698+
.font: UIFont.systemFont(ofSize: 12),
699+
]
700+
label.attributedText = attStr
701+
label.drawText(in: rect)
702+
XCTAssertEqual(label.hitTest(CGPoint(x: 20, y: 25), with: nil), label)
703+
}
704+
705+
func testStringHitTest() {
706+
let attStr = NSMutableAttributedString(
707+
string: "LCLabel",
708+
attributes: [
709+
.foregroundColor: UIColor.white,
710+
.font: UIFont.systemFont(ofSize: 14),
711+
])
712+
attStr.append(NSAttributedString(
713+
string: "\n@LCLabel",
714+
attributes: [
715+
.foregroundColor: UIColor.white,
716+
.font: UIFont.systemFont(ofSize: 10),
717+
.link: "lclabel://welcome",
718+
]))
719+
let rect = CGRect(x: 0, y: 0, width: 300, height: 40)
720+
let label = LCLabel(frame: .zero)
721+
label.isUserInteractionEnabled = true
722+
label.frame = rect
723+
label.centeringTextAlignment = .center
724+
label.numberOfLines = 2
725+
label.linkStyleValidation = .ensure
726+
label.linkAttributes = [
727+
.foregroundColor: UIColor.green,
728+
.font: UIFont.systemFont(ofSize: 12),
729+
]
730+
label.attributedText = attStr
731+
label.drawText(in: rect)
732+
XCTAssertEqual(label.hitTest(CGPoint(x: 20, y: 25), with: nil), label)
733+
}
734+
616735
// MARK: Private
617736

618737
private func createLabel(

0 commit comments

Comments
 (0)