diff --git a/.slather.yml b/.slather.yml new file mode 100644 index 00000000..6f9016c2 --- /dev/null +++ b/.slather.yml @@ -0,0 +1,3 @@ +coverage_service: coveralls +xcodeproj: Example/Espressos.xcodeproj +source_directory: TTTAttributedLabel diff --git a/.travis.yml b/.travis.yml index 07fa91e9..8de9b184 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,13 @@ osx_image: xcode61 language: objective-c -before_install: gem install cocoapods xcpretty -N +before_install: gem install cocoapods xcpretty slather -N cache: cocoapods podfile: Example/Podfile env: - LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8 script: - - set -o pipefail && xcodebuild -workspace 'Example/Espressos.xcworkspace' -scheme 'Espressos' -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 6" clean test | xcpretty -c + - set -o pipefail && xcodebuild -workspace 'Example/Espressos.xcworkspace' -scheme 'Espressos' + -sdk iphonesimulator -destination "platform=iOS Simulator,name=iPhone 6" + GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES GCC_GENERATE_TEST_COVERAGE_FILES=YES clean test | xcpretty -c - pod lib lint --quick +after_success: slather diff --git a/Example/TTTAttributedLabelTests/TTTAttributedLabelTests.m b/Example/TTTAttributedLabelTests/TTTAttributedLabelTests.m index 144ba238..0508ec1b 100644 --- a/Example/TTTAttributedLabelTests/TTTAttributedLabelTests.m +++ b/Example/TTTAttributedLabelTests/TTTAttributedLabelTests.m @@ -77,6 +77,8 @@ - (void)setUp { - (void)tearDown { [super tearDown]; + + label = nil; } #pragma mark - Logic tests @@ -85,11 +87,39 @@ - (void)testInitializable { XCTAssertNotNil(label, @"Label should be initializable"); } +- (void)testContentSize { + label.text = TTTAttributedTestString(); + expect([label intrinsicContentSize]).to.equal([label sizeThatFits:CGSizeZero]); + label.text = kTestLabelText; + expect([label intrinsicContentSize]).to.equal([label sizeThatFits:CGSizeZero]); +} + +- (void)testHighlighting { + label.text = TTTAttributedTestString(); + [label setHighlighted:YES]; + expect(label.highlighted).to.beTruthy(); +} + - (void)testAttributedTextAccess { label.text = TTTAttributedTestString(); XCTAssertTrue([label.attributedText isEqualToAttributedString:TTTAttributedTestString()], @"Attributed strings should match"); } +- (void)testLinkTintColor { + label.tintColor = [UIColor whiteColor]; + + label.inactiveLinkAttributes = @{ kTTTBackgroundFillColorAttributeName : (id)[UIColor grayColor].CGColor }; + label.activeLinkAttributes = @{ kTTTBackgroundFillColorAttributeName : (id)[UIColor redColor].CGColor }; + label.text = TTTAttributedTestString(); + [label addLinkToURL:testURL withRange:NSMakeRange(0, 4)]; + + // Set active + label.tintAdjustmentMode = UIViewTintAdjustmentModeNormal; + label.tintColor = [UIColor redColor]; + + expect([label.attributedText attribute:kTTTBackgroundFillColorAttributeName atIndex:0 effectiveRange:NULL]).to.beNil(); +} + - (void)testDerivedAttributedString { label.font = [UIFont italicSystemFontOfSize:15.f]; label.textColor = [UIColor purpleColor]; @@ -149,6 +179,13 @@ - (void)testMultilineLabelSizing { UIFont *font = [testString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL]; XCTAssertGreaterThan(size.height, font.pointSize, @"Text should size to more than one line"); + + size = [TTTAttributedLabel sizeThatFitsAttributedString:testString + withConstraints:kTestLabelSize + limitedToNumberOfLines:2]; + + font = [testString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL]; + XCTAssertGreaterThan(size.height, font.pointSize, @"Text should size to more than one line"); } - (void)testContainsLinkAtPoint { @@ -347,6 +384,18 @@ - (void)testComplexAttributedString { FBSnapshotVerifyView(label, nil); } +#pragma mark - UIAccessibility + +- (void)testAccessibilityElement { + label.text = TTTAttributedTestString(); + [label addLinkToURL:testURL withRange:NSMakeRange(0, 4)]; + + expect(label.isAccessibilityElement).to.beFalsy(); + expect(label.accessibilityElementCount).will.equal(2); + expect([label accessibilityElementAtIndex:0]).toNot.beNil(); + expect([label indexOfAccessibilityElement:nil]).to.equal(NSNotFound); +} + #pragma mark - TTTAttributedLabelLink tests - (void)testDesignatedInitializer { @@ -546,6 +595,24 @@ - (void)testLongDatePressCallsDelegate { [TTTDelegateMock verify]; } +- (void)testLongDateTimeZonePressCallsDelegate { + label.text = TTTAttributedTestString(); + + NSDate *date = [NSDate date]; + [label addLinkToDate:date timeZone:[NSTimeZone defaultTimeZone] duration:1 withRange:NSMakeRange(0, 4)]; + TTTSizeAttributedLabel(label); + + [[TTTDelegateMock reject] attributedLabel:label didSelectLinkWithDate:date]; + [[TTTDelegateMock expect] attributedLabel:label didLongPressLinkWithDate:date + timeZone:[NSTimeZone defaultTimeZone] + duration:1 + atPoint:CGPointMake(5, 5)]; + + TTTSimulateLongPressOnLabelAtPointWithDuration(label, CGPointMake(5, 5), 0.6f); + + [TTTDelegateMock verify]; +} + - (void)testAddressPressCallsDelegate { label.text = TTTAttributedTestString(); @@ -649,4 +716,98 @@ - (void)testLongTextCheckingPressCallsDelegate { [TTTDelegateMock verify]; } +#pragma mark - UIPasteboard + +- (void)testCopyingLabelText { + label.text = kTestLabelText; + + [label copy:nil]; + + expect([UIPasteboard generalPasteboard].string).to.equal(label.text); +} + +- (void)testPerformActions { + expect([label canPerformAction:@selector(copy:) withSender:nil]).to.beTruthy(); + expect([label canPerformAction:@selector(paste:) withSender:nil]).to.beFalsy(); +} + +#pragma mark - NSCoding + +- (void)testEncodingLabel { + label.text = TTTAttributedTestString(); + + NSData *encodedLabel = [NSKeyedArchiver archivedDataWithRootObject:label]; + + TTTAttributedLabel *newLabel = [NSKeyedUnarchiver unarchiveObjectWithData:encodedLabel]; + + expect(newLabel.text).to.equal(label.text); +} + +#pragma mark - TTTAttributedLabelLink + +- (void)testAddSingleLink { + TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label + textCheckingResult: + [NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(0, 1) URL:testURL]]; + + [label addLink:link]; + + expect(label.links.count).to.equal(1); +} + +- (void)testEncodingLink { + TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label + textCheckingResult: + [NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(0, 1) URL:testURL]]; + + NSData *encodedLink = [NSKeyedArchiver archivedDataWithRootObject:link]; + + TTTAttributedLabelLink *newLink = [NSKeyedUnarchiver unarchiveObjectWithData:encodedLink]; + + expect(newLink.result.URL).to.equal(link.result.URL); +} + +- (void)testLinkAccessibility { + TTTAttributedLabelLink *link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label + textCheckingResult: + [NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(0, 1) URL:testURL]]; + + expect(link.accessibilityValue).to.equal(testURL.absoluteString); + + link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label + textCheckingResult: + [NSTextCheckingResult phoneNumberCheckingResultWithRange:NSMakeRange(0, 1) phoneNumber:@"415-555-1212"]]; + + expect(link.accessibilityValue).to.equal(@"415-555-1212"); + + NSDate *date = [NSDate date]; + + link = [[TTTAttributedLabelLink alloc] initWithAttributesFromLabel:label + textCheckingResult: + [NSTextCheckingResult dateCheckingResultWithRange:NSMakeRange(0, 1) date:date]]; + + expect(link.accessibilityValue).to.equal([NSDateFormatter localizedStringFromDate:date + dateStyle:NSDateFormatterLongStyle + timeStyle:NSDateFormatterLongStyle]); +} + +#pragma mark - Deprecated Methods + +- (void)testLeading { + // Deprecated +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + [label setLeading:1.f]; +#pragma clang diagnostic pop + expect(label.lineSpacing).to.equal(1.f); +} + +- (void)testDataDetectorTypes { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + label.dataDetectorTypes = NSTextCheckingTypeLink; + expect(label.dataDetectorTypes).will.equal(NSTextCheckingTypeLink); +#pragma clang diagnostic pop +} + @end diff --git a/README.md b/README.md index 3232b288..63373f59 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # TTTAttributedLabel -[![Build Status](https://travis-ci.org/TTTAttributedLabel/TTTAttributedLabel.svg)](https://travis-ci.org/TTTAttributedLabel/TTTAttributedLabel) +[![Build Status](https://travis-ci.org/TTTAttributedLabel/TTTAttributedLabel.svg)](https://travis-ci.org/TTTAttributedLabel/TTTAttributedLabel) [![Coverage Status](https://coveralls.io/repos/TTTAttributedLabel/TTTAttributedLabel/badge.svg)](https://coveralls.io/r/TTTAttributedLabel/TTTAttributedLabel) **A drop-in replacement for `UILabel` that supports attributes, data detectors, links, and more**