Skip to content

Instantly share code, notes, and snippets.

@sk-chanch
Last active January 24, 2022 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sk-chanch/011314237befd75f66168f05d732bf5b to your computer and use it in GitHub Desktop.
Save sk-chanch/011314237befd75f66168f05d732bf5b to your computer and use it in GitHub Desktop.
NSParagraphStyleExtension
//
// NSParagraphStyleEx.swift
// AppStarterKit
//
// Created by Chanchana Koedtho on 20/1/2565 BE.
//
import Foundation
import UIKit
extension NSParagraphStyle{
static func set(with config:[ParagraphStyleSetting])->NSParagraphStyle{
let ps = NSMutableParagraphStyle()
config.forEach{
switch $0 {
case .lineSpacing(let value):
ps.lineSpacing = value
case .minimumLineHeight(let value):
ps.minimumLineHeight = value
case .maximumLineHeight(let value):
ps.maximumLineHeight = value
case .alignment(let value):
ps.alignment = value
case .lineBreakMode(let value):
ps.lineBreakMode = value
case .paragraphSpacing(let value):
ps.paragraphSpacing = value
case .firstLineHeadIndent(let value):
ps.firstLineHeadIndent = value
case .headIndent(let value):
ps.headIndent = value
case .tailIndent(let value):
ps.tailIndent = value
case .paragraphSpacingBefore(let value):
ps.paragraphSpacingBefore = value
case .hyphenationFactor(let value):
ps.hyphenationFactor = value
}
}
return ps
}
}
enum ParagraphStyleSetting{
case lineSpacing(_:CGFloat)
case minimumLineHeight(_:CGFloat)
case maximumLineHeight(_:CGFloat)
case alignment(_:NSTextAlignment)
case lineBreakMode(_:NSLineBreakMode)
case paragraphSpacing(_:CGFloat)
case firstLineHeadIndent(_:CGFloat)
case headIndent(_:CGFloat)
case tailIndent(_:CGFloat)
case paragraphSpacingBefore(_:CGFloat)
case hyphenationFactor(_:Float)
}
@sk-chanch
Copy link
Author

let ps = NSParagraphStyle.set(with: [.lineSpacing(1), .minimumLineHeight(10), .alignment(.right)])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment