Skip to content

Instantly share code, notes, and snippets.

@d6u
Created May 11, 2015 13:33
Show Gist options
  • Select an option

  • Save d6u/bc32d34da0386dec855b to your computer and use it in GitHub Desktop.

Select an option

Save d6u/bc32d34da0386dec855b to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Foundation
import UIKit
import XCPlayground
class DLLabel: UILabel {
let textContainer = NSTextContainer()
let layoutManager = NSLayoutManager()
let textStorage = NSTextStorage()
override var frame: CGRect {
didSet {
textContainer.size = frame.size
}
}
override var attributedText: NSAttributedString! {
didSet {
textStorage.setAttributedString(attributedText)
}
}
override var numberOfLines: Int {
didSet {
textContainer.maximumNumberOfLines = numberOfLines
}
}
convenience init() {
self.init(frame: CGRectZero)
}
override init(frame: CGRect) {
super.init(frame: frame)
textContainer.lineFragmentPadding = 0
textContainer.maximumNumberOfLines = numberOfLines
textContainer.lineBreakMode = lineBreakMode
textContainer.size = frame.size
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
userInteractionEnabled = true
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touchPoint = (Array(touches).first! as! UITouch).locationInView(self)
linkAtPoint(touchPoint)
}
func linkAtPoint(var point: CGPoint) {
let range = layoutManager.glyphRangeForTextContainer(textContainer)
var textOffset = CGPointZero
let textBounds = layoutManager.boundingRectForGlyphRange(range, inTextContainer: textContainer)
let paddingHeight = (self.bounds.size.height - textBounds.size.height) / 2
if paddingHeight > 0 {
textOffset.y = paddingHeight
}
point = CGPoint(x: point.x - textOffset.x, y: point.y - textOffset.y)
let index = layoutManager.glyphIndexForPoint(point, inTextContainer: textContainer)
let string = attributedText.string
println(string.substringToIndex(advance(string.startIndex, index)))
}
override func layoutSubviews() {
super.layoutSubviews()
textContainer.size = frame.size
}
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .ByWordWrapping
paragraphStyle.lineSpacing = 10
let font = UIFont(name: "HelveticaNeue", size: 15)!
let string1 = "I ♥ that #RainbowStarDay came back for week 2\n\nhttp://t.co/Am1qlGaYyP\n "
let text1 = NSMutableAttributedString(
string: string1,
attributes: [
NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.blackColor(),
NSParagraphStyleAttributeName: paragraphStyle
])
let text2 = NSAttributedString(
string: "New on the React blog: an extended introduction to GraphQL by @schrockn\n\nhttps://t.co/iUVyHYBJfQ",
attributes: [
NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.blackColor(),
NSParagraphStyleAttributeName: paragraphStyle
])
let text3 = NSAttributedString(
string: "UC Davis 研究表明貓的呼嚕呼嚕聲(purring)對提高骨密度可能有幫助,建議對宇航員播放類似的 25Hz 低頻聲音。",
attributes: [
NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.blackColor(),
NSParagraphStyleAttributeName: paragraphStyle
])
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 320))
func makeLable() -> UILabel {
let label = DLLabel()
label.numberOfLines = 0
label.tintColor = UIColor.redColor()
label.layer.borderColor = UIColor.grayColor().CGColor
label.layer.borderWidth = 1
view.addSubview(label)
return label
}
let label1 = makeLable()
label1.frame = CGRect(x: 10, y: 40, width: 100, height: 0)
label1.attributedText = text1
label1.sizeToFit()
let label2 = makeLable()
label2.frame = CGRect(x: 120, y: 40, width: 100, height: 0)
label2.attributedText = text2
label2.sizeToFit()
let label3 = makeLable()
label3.frame = CGRect(x: 230, y: 40, width: 100, height: 0)
label3.attributedText = text3
label3.sizeToFit()
XCPCaptureValue("view", view)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment