Skip to content

Instantly share code, notes, and snippets.

@raven
Last active May 11, 2017 20:12
Show Gist options
  • Save raven/be56c453ef76ce88199d96ed1dbc7132 to your computer and use it in GitHub Desktop.
Save raven/be56c453ef76ce88199d96ed1dbc7132 to your computer and use it in GitHub Desktop.
rdar://30739102
//
// ViewController.swift
// AttributedString
//
// Created by Peter Goldsmith on 27/02/2017.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let attributedString = NSMutableAttributedString()
// Strikethrough attribute
let strikeThroughAttributes: [String: Any] = [
NSStrikethroughStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
NSForegroundColorAttributeName: UIColor.purple
]
attributedString.append(NSAttributedString(string: "strikethrough", attributes: strikeThroughAttributes))
// If the below is appended, in iOS 10.3 (built against any SDK), it will
// prevent the strikethrough appearing from the PRECEEDING string.
let nonStrikeThroughAttributes: [String: Any] = [
NSStrikethroughStyleAttributeName: NSUnderlineStyle.styleNone.rawValue,
NSForegroundColorAttributeName: UIColor.green
]
attributedString.append(NSAttributedString(string: "no strikethrough", attributes: nonStrikeThroughAttributes))
// Build and run attached project with Xcode 8.2 with iOS 10.2 simulator.
// You should see "strikethrough" in purple that has a strikethrough, and "not strikethrough" in green that has no strikethrough.
// Using Xcode 8.3 against iOS 10.3 simulator, you should see the same, however you see "strikethrough" without any strikethrough.
label.attributedText = attributedString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment