Skip to content

Commit

Permalink
[Playground] Update Measure
Browse files Browse the repository at this point in the history
  • Loading branch information
inamiy committed Jan 6, 2017
1 parent 75b9126 commit 64a54cf
Showing 1 changed file with 76 additions and 4 deletions.
Expand Up @@ -4,27 +4,99 @@ import Flexbox


let config = Config() let config = Config()


//----------------------------------------
// Example 1
//----------------------------------------

example(
"measure (fixed size)",
Node(
size: config.flexContainerSize,
children: [
Node(minSize: CGSize(width: 20, height: 10)),
Node(minSize: CGSize(width: 40, height: 40)),
Node(
measure: { size in
print("measure size = \(size)")
return CGSize(width: 200, height: 80)
}
)
],
alignItems: .center
)
)

//----------------------------------------
// Example 2
//----------------------------------------

let attrText = NSAttributedString(string: "Hello World! Hello World! Hello World!")
let font = UIFont.systemFont(ofSize: 24)

extension NSAttributedString
{
func fitSize(font: UIFont, maxSize: CGSize) -> CGSize
{
let fontAttribute = [NSFontAttributeName: font]
let attributedTextWithFont = NSMutableAttributedString(string: self.string, attributes: fontAttribute)
let fullRange = NSMakeRange(0, (self.string as NSString).length)
attributedTextWithFont.beginEditing()
self.enumerateAttributes(in: fullRange, options: .longestEffectiveRangeNotRequired, using: { (attributes, range, _) in
attributedTextWithFont.addAttributes(attributes, range: range)
})
attributedTextWithFont.endEditing()

let options: NSStringDrawingOptions = [.usesLineFragmentOrigin]
let size = attributedTextWithFont.boundingRect(with: maxSize, options: options, context: nil).size
return size
}
}

let maxLabelWidth = config.flexContainerSize.width - 20 - 40

example( example(
"measure", "measure (NSAttributedString)",
Node( Node(
size: config.flexContainerSize, size: config.flexContainerSize,
children: [ children: [
Node(size: CGSize(width: 20, height: 10)), Node(size: CGSize(width: 20, height: 10)),
Node(size: CGSize(width: 40, height: 40)), Node(size: CGSize(width: 40, height: 40)),
Node( Node(
maxSize: CGSize(width: maxLabelWidth, height: CGFloat.greatestFiniteMagnitude), // required
measure: { size in measure: { size in
print("measure size = \(size)") print("measure size = \(size)")
var size = size
size.height -= 20 let fitSize = attrText.fitSize(font: font, maxSize: size)
return size print("attrText.fitSize = \(fitSize)")
return fitSize
} }
) )
], ],
alignItems: .center alignItems: .center
) )
) )


//----------------------------------------

let (rootView, contentView) = setup(config: config) let (rootView, contentView) = setup(config: config)
PlaygroundPage.current.liveView = rootView PlaygroundPage.current.liveView = rootView


Screenshot.save(contentView, "Measure.png") Screenshot.save(contentView, "Measure.png")

// Tweak UI after `setup`.
if true {
let label = UILabel()
label.layer.borderColor = UIColor.red.cgColor
label.layer.borderWidth = 2
label.attributedText = attrText
label.font = font
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.frame.size.width = maxLabelWidth // set frame before `sizeToFit`
label.sizeToFit()
let targetView = contentView.subviews[2].subviews[2]
targetView.addSubview(label)

print("targetView.frame = \(targetView.frame)")
print("label.frame = \(label.frame)")
}

0 comments on commit 64a54cf

Please sign in to comment.