Skip to content

Instantly share code, notes, and snippets.

@ingconti
Created September 17, 2019 18:10
Show Gist options
  • Save ingconti/aefc78c6d0b22f5329f906094c312a21 to your computer and use it in GitHub Desktop.
Save ingconti/aefc78c6d0b22f5329f906094c312a21 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// SampleColoredLabel
//
// Created by ing.conti on 17/09/2019.
// Copyright © 2019 ing.conti. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myLabel.numberOfLines = 0
myLabel.attributedText = myAttributedString()
}
}
typealias AttrDict = [NSAttributedString.Key : Any]
func stdTextAttribute() -> AttrDict{
let textFont = UIFont.systemFont(ofSize: 32)
let stdAttrib = [NSAttributedString.Key.font: textFont,
//NSParagraphStyleAttributeName: paragraphStyle2,
NSAttributedString.Key.foregroundColor: UIColor.red] as AttrDict
return stdAttrib
}
func smallTextAttribute() -> AttrDict{
let textFont = UIFont.systemFont(ofSize: 10)
let stdAttrib = [NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: UIColor.green] as AttrDict
return stdAttrib
}
func myAttributedString() -> NSAttributedString {
let pieces = ["hello", "word"]
let resultAttributed = NSMutableAttributedString()
var s = ""
s = pieces[0] + "\n"
resultAttributed.append(NSAttributedString(string: s,
attributes: stdTextAttribute() ))
s = pieces[1] + "\n"
resultAttributed.append(NSAttributedString(string: s,
attributes: smallTextAttribute() ))
return resultAttributed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment