垂直对齐UILabel中的文本(注意:使用AutoLayout) [英] Vertically align text within a UILabel (Note : Using AutoLayout)
问题描述
我正在复制问题
对于那些对使用开放源代码更舒服的人,请务必查看 TTTAttributedLabel ,您可以在其中设置标签的文本对齐到 TTTAttributedLabelVerticalAlignmentTop
code> UILabel 并覆盖 drawTextInRect
。
这里是一个天真的实现,你现在可以使用:
Swift
@IBDesignable类TopAlignedLabel:UILabel {
pre>
override func drawTextInRect(rect:CGRect) {
if let stringText = text {
let stringTextAsNSString = stringText as NSString
var labelStringSize = stringTextAsNSString.boundingRectWithSize(CGSizeMake(CGRectGetWidth(self.frame),CGFloat.max),
选项:NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes:[NSFontAttributeName:font],
context:nil).size
super.drawTextInRect(CGRectMake(0,0,CGRectGetWidth(self.frame),ceil (labelStringSize.height))
} else {
super.drawTextInRect(rect)
}
}
override func prepareForInterfaceBuilder(){
super。 prepareForInterfaceBuilder()
layer.borderWidth = 1
layer.borderColor = UIColor.blackColor()。CGColor
}
}
Swift 3
@IBDesignable class TopAlignedLabel:UILabel {
override func drawText(在rect中:CGRect){
如果let stringText = text {
let stringTextAsNSString = stringText as NSString
let labelStringSize = stringTextAsNSString.boundingRect(with:CGSize self.frame.width,height:CGFloat.greatestFiniteMagnitude),
options:NSStringDrawingOptions.usesLineFragmentOrigin,
attributes:[NSFontAttributeName:font],
context:nil).size
super .drawText(in:CGRect(x:0,y:0,width:self.frame.width,height:ceil(labelStringSize.height)))
} else {
super.drawText rect)
}
}
override func prepareForInterfaceBuilder(){
super.prepareForInterfaceBuilder()
layer.borderWidth = 1
layer.borderColor = UIColor。 black.cgColor
}
}
Objective-C
IB_DESIGNABLE
@interface TopAlignedLabel:UILabel
@end
@implementation TopAlignedLabel
- (void)drawTextInRect:(CGRect)rect {
if(self.text){
CGSize labelStringSize = [self.text boundingRectWithSize:CGSizeMake(CGRectGetWidth ),CGFLOAT_MAX)
选项:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@ {NSFontAttributeName:self.font}
context:nil] .size;
[super drawTextInRect:CGRectMake(0,0,ceilf(CGRectGetWidth(self.frame)),ceilf(labelStringSize.height)
} else {
[super drawTextInRect:rect];
}
}
- (void)prepareForInterfaceBuilder {
[super prepareForInterfaceBuilder];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor blackColor] .CGColor;
}
@end
由于我使用了IBDesignable,你可以将这个标签添加到故事板并观看它,这是我的样子。
>
I am Copying the same Question asked Before Question. I have tried the solutions given and was not able to solve it since sizetofit was not effective when I use Autolayout.
The expected display is like below.
解决方案Edit
In my original answer I was using the paragraph style of the label. Turns out that for multi-line labels this actually prevents the label from being multi-line. As a result I removed it from the calculation. See more about this in Github
For those of you more comfortable with using Open Source definitely look at TTTAttributedLabel where you can set the label's text alignment to
TTTAttributedLabelVerticalAlignmentTop
The trick is to subclass
UILabel
and overridedrawTextInRect
. Then enforce that the text is drawn at the origin of the label's bounds.Here's a naive implementation that you can use right now:
Swift
@IBDesignable class TopAlignedLabel: UILabel { override func drawTextInRect(rect: CGRect) { if let stringText = text { let stringTextAsNSString = stringText as NSString var labelStringSize = stringTextAsNSString.boundingRectWithSize(CGSizeMake(CGRectGetWidth(self.frame), CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil).size super.drawTextInRect(CGRectMake(0, 0, CGRectGetWidth(self.frame), ceil(labelStringSize.height))) } else { super.drawTextInRect(rect) } } override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() layer.borderWidth = 1 layer.borderColor = UIColor.blackColor().CGColor } }
Swift 3
@IBDesignable class TopAlignedLabel: UILabel { override func drawText(in rect: CGRect) { if let stringText = text { let stringTextAsNSString = stringText as NSString let labelStringSize = stringTextAsNSString.boundingRect(with: CGSize(width: self.frame.width,height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil).size super.drawText(in: CGRect(x:0,y: 0,width: self.frame.width, height:ceil(labelStringSize.height))) } else { super.drawText(in: rect) } } override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() layer.borderWidth = 1 layer.borderColor = UIColor.black.cgColor } }
Objective-C
IB_DESIGNABLE @interface TopAlignedLabel : UILabel @end @implementation TopAlignedLabel - (void)drawTextInRect:(CGRect)rect { if (self.text) { CGSize labelStringSize = [self.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil].size; [super drawTextInRect:CGRectMake(0, 0, ceilf(CGRectGetWidth(self.frame)),ceilf(labelStringSize.height))]; } else { [super drawTextInRect:rect]; } } - (void)prepareForInterfaceBuilder { [super prepareForInterfaceBuilder]; self.layer.borderWidth = 1; self.layer.borderColor = [UIColor blackColor].CGColor; } @end
Since I used IBDesignable you can add this label to a storyboard and watch it go, this is what it looks like for me
这篇关于垂直对齐UILabel中的文本(注意:使用AutoLayout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!