• <bdo id='aPPoL'></bdo><ul id='aPPoL'></ul>

      1. <i id='aPPoL'><tr id='aPPoL'><dt id='aPPoL'><q id='aPPoL'><span id='aPPoL'><b id='aPPoL'><form id='aPPoL'><ins id='aPPoL'></ins><ul id='aPPoL'></ul><sub id='aPPoL'></sub></form><legend id='aPPoL'></legend><bdo id='aPPoL'><pre id='aPPoL'><center id='aPPoL'></center></pre></bdo></b><th id='aPPoL'></th></span></q></dt></tr></i><div id='aPPoL'><tfoot id='aPPoL'></tfoot><dl id='aPPoL'><fieldset id='aPPoL'></fieldset></dl></div>
        <tfoot id='aPPoL'></tfoot>

        <legend id='aPPoL'><style id='aPPoL'><dir id='aPPoL'><q id='aPPoL'></q></dir></style></legend>

        <small id='aPPoL'></small><noframes id='aPPoL'>

      2. 在 UILabel 中垂直对齐文本(注意:使用 AutoLayout)

        时间:2023-09-10
        <legend id='U8wGC'><style id='U8wGC'><dir id='U8wGC'><q id='U8wGC'></q></dir></style></legend>

        <small id='U8wGC'></small><noframes id='U8wGC'>

          • <bdo id='U8wGC'></bdo><ul id='U8wGC'></ul>
            <i id='U8wGC'><tr id='U8wGC'><dt id='U8wGC'><q id='U8wGC'><span id='U8wGC'><b id='U8wGC'><form id='U8wGC'><ins id='U8wGC'></ins><ul id='U8wGC'></ul><sub id='U8wGC'></sub></form><legend id='U8wGC'></legend><bdo id='U8wGC'><pre id='U8wGC'><center id='U8wGC'></center></pre></bdo></b><th id='U8wGC'></th></span></q></dt></tr></i><div id='U8wGC'><tfoot id='U8wGC'></tfoot><dl id='U8wGC'><fieldset id='U8wGC'></fieldset></dl></div>
            <tfoot id='U8wGC'></tfoot>

                  <tbody id='U8wGC'></tbody>

                  本文介绍了在 UILabel 中垂直对齐文本(注意:使用 AutoLayout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在复制

                  中查看更多信息

                  对于那些更习惯于使用开源的人,请务必查看 TTTAttributedLabel,您可以在其中设置标签的文本对齐到 TTTAttributedLabelVerticalAlignmentTop


                  诀窍是继承 UILabel 并覆盖 drawTextInRect.然后强制在标签边界的原点绘制文本.

                  这是一个您现在可以使用的简单实现:

                  斯威夫特

                  @IBDesignable 类 TopAlignedLabel: UILabel {覆盖 func drawTextInRect(rect: CGRect) {如果让 stringText = text {让 stringTextAsNSString = stringText 作为 NSStringvar labelStringSize = stringTextAsNSString.boundingRectWithSize(CGSizeMake(CGRectGetWidth(self.frame), CGFloat.max),选项:NSStringDrawingOptions.UsesLineFragmentOrigin,属性:[NSFontAttributeName:字体],上下文:无).sizesuper.drawTextInRect(CGRectMake(0, 0, CGRectGetWidth(self.frame), ceil(labelStringSize.height)))} 别的 {super.drawTextInRect(rect)}}覆盖 func prepareForInterfaceBuilder() {super.prepareForInterfaceBuilder()layer.borderWidth = 1layer.borderColor = UIColor.blackColor().CGColor}}

                  斯威夫特 3

                   @IBDesignable 类 TopAlignedLabel: UILabel {覆盖 func drawText(in rect: CGRect) {如果让 stringText = text {让 stringTextAsNSString = stringText 作为 NSString让 labelStringSize = stringTextAsNSString.boundingRect(with: CGSize(width: self.frame.width,height: CGFloat.greatestFiniteMagnitude),选项:NSStringDrawingOptions.usesLineFragmentOrigin,属性:[NSFontAttributeName:字体],上下文:无).sizesuper.drawText(in: CGRect(x:0,y: 0,width: self.frame.width, height:ceil(labelStringSize.height)))} 别的 {super.drawText(在:矩形)}}覆盖 func prepareForInterfaceBuilder() {super.prepareForInterfaceBuilder()layer.borderWidth = 1layer.borderColor = UIColor.black.cgColor}}

                  目标-C

                  IB_DESIGNABLE@interface TopAlignedLabel : UILabel@结尾@implementation TopAlignedLabel- (void)drawTextInRect:(CGRect)rect {如果(self.text){CGSize labelStringSize = [self.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame), CGFLOAT_MAX)选项:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading属性:@{NSFontAttributeName:self.font}上下文:nil].size;[super drawTextInRect:CGRectMake(0, 0, ceilf(CGRectGetWidth(self.frame)),ceilf(labelStringSize.height))];} 别的 {[超级 drawTextInRect:rect];}}- (void)prepareForInterfaceBuilder {[超级prepareForInterfaceBuilder];self.layer.borderWidth = 1;self.layer.borderColor = [UIColor blackColor].CGColor;}@结尾


                  由于我使用了 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 override drawTextInRect. 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Storyboard 遮罩 UIView 并给出圆角? 下一篇:键入时调整包含 UITextView 的 UITableViewCell 的大小

                  相关文章

                    <bdo id='QN00l'></bdo><ul id='QN00l'></ul>
                  <tfoot id='QN00l'></tfoot>

                    <small id='QN00l'></small><noframes id='QN00l'>

                  1. <legend id='QN00l'><style id='QN00l'><dir id='QN00l'><q id='QN00l'></q></dir></style></legend>
                    1. <i id='QN00l'><tr id='QN00l'><dt id='QN00l'><q id='QN00l'><span id='QN00l'><b id='QN00l'><form id='QN00l'><ins id='QN00l'></ins><ul id='QN00l'></ul><sub id='QN00l'></sub></form><legend id='QN00l'></legend><bdo id='QN00l'><pre id='QN00l'><center id='QN00l'></center></pre></bdo></b><th id='QN00l'></th></span></q></dt></tr></i><div id='QN00l'><tfoot id='QN00l'></tfoot><dl id='QN00l'><fieldset id='QN00l'></fieldset></dl></div>