自定义UITableViewCell标签

时间:2014-12-07 22:30:31

标签: ios uitableview

我尝试创建一个带有3个标签的自定义UITableViewCell,其风格与字幕样式相似,但右侧也有标签。到目前为止,我有2个问题:

在左侧创建标签时,我使用的是常量16偏移,但后来发现这是不正确的。分隔符插入返回15(self.separatorInset.left),但iPad的标签似乎是从左边22.5。我必须在代码中执行此操作,但是要检查iPad UI惯用语并乘以1.5正确的方法吗?

第二个问题是我使用动态字体大小:主标签为Body,副标题为Caption 1。但是,这会分别创建字体大小为17和12的标签,而系统单元格的标签为16和11.我有一个自定义UILabel,它会尝试确保系统字体大小更改得到尊重。我应该在这个类中缩小字体大小吗?

class DynamicFontLabel: UILabel {

    var fontStyle: String?

    convenience override init() {
        self.init(fontStyle: UIFontTextStyleBody)
    }

    init(fontStyle: String) {
        super.init()
        self.font = UIFont.preferredFontForTextStyle(fontStyle)
        self.setup()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setup()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setup()
    }

    func setup() {
        // Ensure the text color is correct, as per the UILabel appearance
        self.textColor = UILabel.appearance().textColor
        self.fontStyle = self.currentFontStyle()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateFontSize", name: UIContentSizeCategoryDidChangeNotification, object: nil)
        self.numberOfLines = 0
        self.lineBreakMode = NSLineBreakMode.ByWordWrapping
    }

    func currentFontStyle() -> String {
        if self.font.isEqual(UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)) {
            return UIFontTextStyleHeadline
        } else if self.font.isEqual(UIFont.preferredFontForTextStyle(UIFontTextStyleBody)) {
            return UIFontTextStyleBody
        } else if self.font.isEqual(UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1)) {
            return UIFontTextStyleCaption1
        } else if self.font.isEqual(UIFont.preferredFontForTextStyle(UIFontTextStyleCaption2)) {
            return UIFontTextStyleCaption2
        } else if self.font.isEqual(UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote)) {
            return UIFontTextStyleFootnote
        } else if self.font.isEqual(UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)) {
            return UIFontTextStyleSubheadline
        }
        return UIFontTextStyleBody
    }

    func updateFontSize() {
        if let fontStyle = self.fontStyle {
            self.font = UIFont.preferredFontForTextStyle(fontStyle)
        }
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}

0 个答案:

没有答案
相关问题