相关疑难解决方法(0)

如何使用Swift以编程方式添加约束

自上周以来我一直试图解决这个问题,而没有更进一步.好的,所以我需要在Swift中编程方式应用一些约束 来使用这段代码:UIView

var new_view:UIView! = UIView(frame: CGRectMake(0, 0, 100, 100));
new_view.backgroundColor = UIColor.redColor();
view.addSubview(new_view);

var constX:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0);
self.view.addConstraint(constX);

var constY:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0);
self.view.addConstraint(constY);

var constW:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: new_view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0);
self.view.addConstraint(constW);

var constH:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: …
Run Code Online (Sandbox Code Playgroud)

uiview ios autolayout nslayoutconstraint swift

409
推荐指数
14
解决办法
38万
查看次数

UILabel垂直对齐

在我的应用程序中,我正在使用ActiveLabelfram Github.

在这种情况下,我的标签不会显示UILabel中间的文本.如果我使用普通的UILabel它可以正常工作,但是当它设置为ActiveLabel时,它会像这样. 在此输入图像描述

(图像在运行时拍摄)

我认为这是以某种方式使用对齐的代码:

/// add line break mode
private func addLineBreak(attrString: NSAttributedString) -> NSMutableAttributedString {
    let mutAttrString = NSMutableAttributedString(attributedString: attrString)

    var range = NSRange(location: 0, length: 0)
    var attributes = mutAttrString.attributesAtIndex(0, effectiveRange: &range)

    let paragraphStyle = attributes[NSParagraphStyleAttributeName] as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
    if let lineSpacing = lineSpacing {
        paragraphStyle.lineSpacing = CGFloat(lineSpacing)
    }

    attributes[NSParagraphStyleAttributeName] = paragraphStyle
    mutAttrString.setAttributes(attributes, range: range)

    return mutAttrString
}
Run Code Online (Sandbox Code Playgroud)

ActiveLabel.swift

ActiveType.swift

任何想法如何我可以在中间这样做: 在此输入图像描述

(图片取自故事板)

uilabel ios swift

11
推荐指数
1
解决办法
2643
查看次数

标签 统计

ios ×2

swift ×2

autolayout ×1

nslayoutconstraint ×1

uilabel ×1

uiview ×1