https://github.com/hlfcoding/uitextextensions
:label: UIKit convenience extensions for text styling in code
https://github.com/hlfcoding/uitextextensions
ios swift uilabel uitextfield uitextview
Last synced: about 2 months ago
JSON representation
:label: UIKit convenience extensions for text styling in code
- Host: GitHub
- URL: https://github.com/hlfcoding/uitextextensions
- Owner: hlfcoding
- License: mit
- Created: 2017-06-11T23:52:15.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-19T00:44:42.000Z (almost 9 years ago)
- Last Synced: 2025-04-08T00:44:23.173Z (about 1 year ago)
- Topics: ios, swift, uilabel, uitextfield, uitextview
- Language: Shell
- Homepage:
- Size: 72.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TextExtensions
[](http://cocoapods.org/pods/HLFUITextExtensions)
[](http://cocoapods.org/pods/HLFUITextExtensions)
[](http://cocoapods.org/pods/HLFUITextExtensions)
[](https://codeclimate.com/github/hlfcoding/UITextExtensions)
> :label: UIKit convenience extensions for text styling in code.

## Example
```swift
let label = UILabel()
label.font = .preferredFont(forTextStyle: .title1)
label.numberOfLines = 0
label.textAlignment = .center
label.textColor = .darkText
label.hlf_updateText("Hello, playground. You are looking okay.")
label.hlf_setKerning(-0.5)
label.hlf_setLineHeight(label.hlf_pointsByEms(1.1))
// ...
label.hlf_updateText("Hello, playground. You are looking good.")
let width = label.hlf_pointsByEms(9)
label.frame.size.width = width
label.sizeToFit()
// ...
```
Compare the above with the (less flexible) below:
```swift
let label = UILabel()
label.numberOfLines = 0
var attributes = [String : Any]()
let font = UIFont.preferredFont(forTextStyle: .title1)
attributes[NSFontAttributeName] = font
attributes[NSForegroundColorAttributeName] = UIColor.darkText
attributes[NSKernAttributeName] = -0.5
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
paragraphStyle.maximumLineHeight = round(1.1 * font.pointSize)
attributes[NSParagraphStyleAttributeName] = paragraphStyle
label.attributedText = NSAttributedString(
string: "Hello, playground. You are looking okay.", attributes: attributes
)
// ...
label.attributedText = NSAttributedString(
string: "Hello, playground. You are looking good.", attributes: attributes
)
let width = round(9 * font.pointSize)
label.frame.size.width = width
label.sizeToFit()
// ...
```
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Requirements
Swift 3+
## Installation
TextExtensions is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod "HLFUITextExtensions"
```
## Author
Peng Wang, peng@pengxwang.com
## License
TextExtensions is available under the MIT license. See the LICENSE file for more info.