Last active
October 20, 2015 16:47
-
-
Save morizotter/06f25972eed87bc5bc29 to your computer and use it in GitHub Desktop.
Cellなどの高さ計算の処理をViewのstructで行う ref: http://qiita.com/morizotter/items/205edf75d6da0a7f9c11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class UserCell: UITableViewCell { | |
struct Layout { | |
static func height(vm: UserViewModel) -> CGFloat { | |
return 68.0 | |
} | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let vm = ... | |
return UserCell.Layout.height(vm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class ItemCell: UITableViewCell { | |
struct Layout { | |
static let LeftAreaMargin: CGFloat = 67.0 | |
static let RightAreaMargin: CGFloat = 14.0 | |
static let TopAreaHeight: CGFloat = 41.0 | |
static let NameFont = UIFont.boldSystemFontOfSize(12) | |
static let TitleTopHeight: CGFloat = 8.0 | |
static var TitleMaximumSize = CGSize(width: UIScreen.mainScreen().bounds.width - Layout.LeftAreaMargin - Layout.RightAreaMargin, height: CGFloat.max) | |
static let TitleFont = UIFont.boldSystemFontOfSize(16.0) | |
static let TitleAttributes = [NSFontAttributeName: Layout.TitleFont] | |
static let BottomHeight: CGFloat = 18.0 | |
static func height(vm: ItemViewModel) -> CGFloat { | |
return height(NSAttributedString(string: vm.title.value, attributes: Layout.TitleAttributes)) | |
} | |
static func height(attributedTitle: NSAttributedString) -> CGFloat { | |
var height: CGFloat = 0.0 | |
height += Layout.TopAreaHeight | |
height += Layout.TitleTopHeight | |
height += Layout.TitleHeight(attributedTitle) | |
height += Layout.BottomHeight | |
return ceil(height) | |
} | |
static func TitleHeight(attributedTitle: NSAttributedString) -> CGFloat { | |
let options : NSStringDrawingOptions = .UsesLineFragmentOrigin | .UsesFontLeading | |
let rect: CGRect = attributedTitle.boundingRectWithSize(Layout.TitleMaximumSize, options: options, context: nil) | |
return ceil(rect.size.height) | |
} | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let vm = ... | |
return ItemCell.Layout.height(vm) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment