Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension of UIlabel for adding line spacing with the method of n… #438

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ All notable changes to this project will be documented in this file.

## Unreleased


1. **Double**
- `**?(lhs:Double, rhs:Double)` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/443) by *Khalian*

2. **UILabel**
- `public convenience init(font: UIFont, color: UIColor, alignment: NSTextAlignment)` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/441) by *icefall*

3. **UILabelExtensions**
- `setLineSpacing(lineSpacing : CGFloat)` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/438) by *osama10*

## [Release 1.10]

### New platform added.
Expand Down Expand Up @@ -60,7 +63,7 @@ All notable changes to this project will be documented in this file.
- `validateEmail()` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/429) by *Vic-L*
- `validateDigits()` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/429) by *Vic-L*

### Deprecated/Renamed extensions
### Deprecated/Renamed extensions
1. **UIViewController**
- `public func hideKeyboardWhenTappedAround(cancelTouches: Bool = false)` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/395) by *lfarah*

Expand Down
103 changes: 59 additions & 44 deletions EZSwiftExtensionsTests/UILabelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,72 @@
// Created by Goktug Yilmaz on 8/25/16.
// Copyright © 2016 Goktug Yilmaz. All rights reserved.
//

#if os(iOS) || os(tvOS)

import XCTest
@testable import EZSwiftExtensions
class UILabelTests: XCTestCase {

func testInit() {

let label = UILabel(x: 0, y: 0, w: 200, h: 50)
Copy link
Collaborator

@Khalian Khalian Aug 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any changes in these parts? Please make the code change idempotent to only the changes you are making. This looks like an indentation change which is unnecessary. The code already was properly indented.

Another important part of making this change idempotent is to ensure it appears as one single commit in the PR. I do not want to manually merge 12 commits with a couple of merge commits in it.

let expected = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
let label2 = UILabel(x: 0, y: 0, w: 200, h: 50, fontSize: 20)

import XCTest
@testable import EZSwiftExtensions
class UILabelTests: XCTestCase {

let label3 = UILabel(font: UIFont.systemFont(ofSize: 32), color: .red, alignment: .left)
func testInit() {

let label = UILabel(x: 0, y: 0, w: 200, h: 50)
let expected = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
let label2 = UILabel(x: 0, y: 0, w: 200, h: 50, fontSize: 20)

let label3 = UILabel(font: UIFont.systemFont(ofSize: 32), color: .red, alignment: .left)

XCTAssertEqual(label.frame, expected.frame)
XCTAssertEqual(label2.font.pointSize, 20)
XCTAssertEqual(label.font.pointSize, 17)

XCTAssertEqual(label3.font, UIFont.systemFont(ofSize: 32))
XCTAssertEqual(label3.textColor, .red)
XCTAssertEqual(label3.textAlignment, .left)
}

XCTAssertEqual(label.frame, expected.frame)
XCTAssertEqual(label2.font.pointSize, 20)
XCTAssertEqual(label.font.pointSize, 17)
func testSet() {

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
label.set(text: "EZSwiftExtensions✅", duration: 1)
XCTAssertEqual(label.text, "EZSwiftExtensions✅")

label.text = ""
label.set(text: "EZSwiftExtensions🚀", duration: 0)
XCTAssertEqual(label.text, "EZSwiftExtensions🚀")

label.text = ""
label.set(text: "EZSwiftExtensions❤️", duration: 1)
XCTAssertEqual(label.text, "EZSwiftExtensions❤️")
}

XCTAssertEqual(label3.font, UIFont.systemFont(ofSize: 32))
XCTAssertEqual(label3.textColor, .red)
XCTAssertEqual(label3.textAlignment, .left)
}

func testSet() {
func testSetLineSpacing(){
let textForTesting = "I am testing test Set line spacing method"
var paragraphStyle : NSMutableParagraphStyle?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick : too many empty lines here are there that do not serve a purpose.

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
label.text = textForTesting
label.setLineSpacing(lineSpacing: 1.5)

label.attributedText?.enumerateAttribute(NSParagraphStyleAttributeName , in: NSMakeRange(0, (label.attributedText?.length)!), options: [.longestEffectiveRangeNotRequired]) { value, range, isStop in
if let value = value {
paragraphStyle = value as! NSMutableParagraphStyle
}
}
XCTAssertEqual(paragraphStyle?.lineHeightMultiple, 1.5)
}

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
label.set(text: "EZSwiftExtensions✅", duration: 1)
XCTAssertEqual(label.text, "EZSwiftExtensions✅")
var waitExpectation: XCTestExpectation?

label.text = ""
label.set(text: "EZSwiftExtensions🚀", duration: 0)
XCTAssertEqual(label.text, "EZSwiftExtensions🚀")
func wait(duration: TimeInterval) {
waitExpectation = expectation(description: "wait")
Timer.scheduledTimer(timeInterval: duration, target: self,
selector: #selector(UILabelTests.onTimer), userInfo: nil, repeats: false)
waitForExpectations(timeout: duration + 3, handler: nil)
}

label.text = ""
label.set(text: "EZSwiftExtensions❤️", duration: 1)
XCTAssertEqual(label.text, "EZSwiftExtensions❤️")
func onTimer() {
waitExpectation?.fulfill()
}
}

var waitExpectation: XCTestExpectation?

func wait(duration: TimeInterval) {
waitExpectation = expectation(description: "wait")
Timer.scheduledTimer(timeInterval: duration, target: self,
selector: #selector(UILabelTests.onTimer), userInfo: nil, repeats: false)
waitForExpectations(timeout: duration + 3, handler: nil)
}

func onTimer() {
waitExpectation?.fulfill()
}
}

#endif
13 changes: 13 additions & 0 deletions Sources/UILabelExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ extension UILabel {
self.text = _text
}, completion: nil)
}

// Set lineSpacing for UILabel
public func setLineSpacing(lineSpacing: CGFloat) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to write a unit test for this ?

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.0
paragraphStyle.lineHeightMultiple = lineSpacing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing. Does this mean your spacing = 1.0 * lineHeight = lineSpacing. If so, you could just call the method arg as lineHeightMultiple.

paragraphStyle.alignment = self.textAlignment

let attrString = NSMutableAttributedString(string: self.text!)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could actually live as its own var. Otherwise its unnecessary allocation of the attr string per call.

attrString.addAttribute(NSFontAttributeName, value: self.font, range: NSMakeRange(0, attrString.length))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NSMakeRange is repeated, you can abstract that away as a single call.

attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
self.attributedText = attrString
}
}

#endif