快速向上舍入为respondsToSelector()和sizeWithFont()

时间:2015-04-14 07:35:05

标签: ios objective-c swift

我正在将以下Objective-C代码翻译为Swift

- (void)layoutSubviews
{
    CGSize valueLabelSize = CGSizeZero;

    if ([self.valueLabel.text respondsToSelector:@selector(sizeWithAttributes:)])
    {
        valueLabelSize = [self.valueLabel.text sizeWithAttributes:@{NSFontAttributeName:self.valueLabel.font}];
    }
    else
    {
        valueLabelSize = [self.valueLabel.text sizeWithFont:self.valueLabel.font];
    }

    CGFloat xOffset = ceil((self.bounds.size.width - valueLabelSize.width) * 0.5);
    self.valueLabel.frame = CGRectMake(xOffset, ceil(self.bounds.size.height * 0.5) - ceil(valueLabelSize.height * 0.5), valueLabelSize.width, valueLabelSize.height);
}

此代码基本上在UILabel的中间动态加载UIView,尊重相对字体大小和视图的填充。这是我目前的代码:

override func layoutSubviews()
{
    var valueSize = CGSizeZero

    if valueLabel.text != nil {
        if ((self.valueLabel.text! as NSString).respondsToSelector(Selector("sizeWithAttributes:"))){
            valueSize = (self.valueLabel.text! as NSString).sizeWithAttributes([NSFontAttributeName:self.valueLabel.font])
        }
    }

    let xOffSet = ceil((self.bounds.size.width - valueSize.width) * 0.5)
    valueLabel.frame = CGRectMake(xOffSet, ceil(self.bounds.size.height * 0.5) - ceil(valueSize.height * 0.5), valueSize.width, valueSize.height)
}

这不能正常工作,因为标签出现在视图的左上角。 我认为快速有一些回合没有使用respondsToSelector()方法,但不知道如何。我还想避免使用sizeWithFont()因为 已弃用

非常欢迎任何帮助!

2 个答案:

答案 0 :(得分:2)

我终于使用superview属性:

override func layoutSubviews() {
    super.layoutSubviews()

    var valueSize = CGSizeZero
    if let textValue = valueLabel.text {
        valueSize = textValue.sizeWithAttributes([NSFontAttributeName:self.valueLabel.font])
    }
    var unitSize = CGSizeZero
    if let textUnit = unitLabel.text {
        unitSize = textUnit.sizeWithAttributes([NSFontAttributeName:self.unitLabel.font])
    }
    let xOffset = ceil((superview!.bounds.size.width - (valueSize.width + unitSize.width)) * 0.5)
    valueLabel.frame = CGRectMake(xOffset, ceil(superview!.bounds.size.height * 0.5) - ceil(valueSize.height * 0.5), valueSize.width, valueSize.height)
    unitLabel.frame = CGRectMake(CGRectGetMaxX(valueLabel.frame), ceil(superview!.bounds.size.height * 0.5) - ceil(unitSize.height * 0.5) + iMinistryValueInformationViewPadding, unitSize.width, unitSize.height)
}

无论如何,谢谢你的帮助!

答案 1 :(得分:1)

您是否尝试使用label.sizeToFit()设置正确的标签尺寸以适合您的内容?

例如:

class CustomView : UIView {
    lazy var textLabel: UILabel = {
        let label = UILabel()

        //  Setup your label's text attributes here.
        //  e.g. font size

        self.addSubview(label)

        return label
    }()

    override func layoutSubviews() {
        super.layoutSubviews()

        //  By this point the `text` for the label and all attributes
        //  that could affect the text size need to be set.
        textLabel.sizeToFit()
        textLabel.center = CGPoint(x: frame.width / 2, y: frame.height / 2)
    }
}

结果:

enter image description here

希望有所帮助!