ios - aligner vertiquement le texte vers le haut dans un UILabel

Mots clés : ioscocoa-touchuikituilabeltext-alignmentios

meilleur 4 Réponses ios - aligner vertiquement le texte vers le haut dans un UILabel

vote vote

90

    [myLabel sizeToFit]; 
    myLabel.numberOfLines = 0;     [myLabel sizeToFit]; 
- (void)viewDidLoad {     [super viewDidLoad];      // 20 point top and left margin. Sized to leave 20 pt at right.     CGRect labelFrame = CGRectMake(20, 20, 280, 150);     UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];     [myLabel setBackgroundColor:[UIColor orangeColor]];      NSString *labelText = @"I am the very model of a modern Major-General, I've information vegetable, animal, and mineral";     [myLabel setText:labelText];      // Tell the label to use an unlimited number of lines     [myLabel setNumberOfLines:0];     [myLabel sizeToFit];      [self.view addSubview:myLabel]; } 
    // myLabel.textAlignment = NSTextAlignmentRight;     myLabel.textAlignment = NSTextAlignmentCenter;      [myLabel setNumberOfLines:0];     [myLabel sizeToFit]; 
    myLabel.textAlignment = NSTextAlignmentCenter;      [myLabel setNumberOfLines:0];     [myLabel sizeToFit];      CGRect myFrame = myLabel.frame;     // Resize the frame's width to 280 (320 - margins)     // width could also be myOriginalLabelFrame.size.width     myFrame = CGRectMake(myFrame.origin.x, myFrame.origin.y, 280, myFrame.size.height);     myLabel.frame = myFrame; 
CGSize maximumSize = CGSizeMake(300, 9999); NSString *dateString = @"The date today is January 1st, 1999"; UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14]; CGSize dateStringSize = [dateString sizeWithFont:dateFont          constrainedToSize:maximumSize          lineBreakMode:self.dateLabel.lineBreakMode];  CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);  self.dateLabel.frame = dateFrame; 
vote vote

86

myLabel.text = @"Some Text" 
myLabel.numberOfLines = 0 
myLabel.frame = CGRectMake(20,20,200,800) 
[myLabel sizeToFit] 
vote vote

73

for(int i=1; i< newLinesToPad; i++)      self.text = [self.text stringByAppendingString:@"\n"]; 
for(int i=0; i<newLinesToPad; i++)     self.text = [self.text stringByAppendingString:@"\n "]; 
// -- file: UILabel+VerticalAlign.h #pragma mark VerticalAlign @interface UILabel (VerticalAlign) - (void)alignTop; - (void)alignBottom; @end  // -- file: UILabel+VerticalAlign.m @implementation UILabel (VerticalAlign) - (void)alignTop {     CGSize fontSize = [self.text sizeWithFont:self.font];     double finalHeight = fontSize.height * self.numberOfLines;     double finalWidth = self.frame.size.width;    //expected width of label     CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];     int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;     for(int i=0; i<newLinesToPad; i++)         self.text = [self.text stringByAppendingString:@"\n "]; }  - (void)alignBottom {     CGSize fontSize = [self.text sizeWithFont:self.font];     double finalHeight = fontSize.height * self.numberOfLines;     double finalWidth = self.frame.size.width;    //expected width of label     CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];     int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;     for(int i=0; i<newLinesToPad; i++)         self.text = [NSString stringWithFormat:@" \n%@",self.text]; } @end 
vote vote

63

@interface MFTopAlignedLabel : UILabel  @end   @implementation MFTopAlignedLabel  - (void)drawTextInRect:(CGRect) rect {     NSAttributedString *attributedText = [[NSAttributedString alloc]     initWithString:self.text attributes:@{NSFontAttributeName:self.font}];     rect.size.height = [attributedText boundingRectWithSize:rect.size                                             options:NSStringDrawingUsesLineFragmentOrigin                                             context:nil].size.height;     if (self.numberOfLines != 0) {         rect.size.height = MIN(rect.size.height, self.numberOfLines * self.font.lineHeight);     }     [super drawTextInRect:rect]; }  @end 
class VerticalTopAlignLabel: UILabel {      override func drawText(in rect:CGRect) {         guard let labelText = text else {  return super.drawText(in: rect) }          let attributedText = NSAttributedString(string: labelText, attributes: [NSFontAttributeName: font])         var newRect = rect         newRect.size.height = attributedText.boundingRect(with: rect.size, options: .usesLineFragmentOrigin, context: nil).size.height          if numberOfLines != 0 {             newRect.size.height = min(newRect.size.height, CGFloat(numberOfLines) * font.lineHeight)         }          super.drawText(in: newRect)     }  } 
class VerticalTopAlignLabel: UILabel {      override func drawText(in rect:CGRect) {         guard let labelText = text else {  return super.drawText(in: rect) }          let attributedText = NSAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: font])         var newRect = rect         newRect.size.height = attributedText.boundingRect(with: rect.size, options: .usesLineFragmentOrigin, context: nil).size.height          if numberOfLines != 0 {             newRect.size.height = min(newRect.size.height, CGFloat(numberOfLines) * font.lineHeight)         }          super.drawText(in: newRect)     }  } 

Questions similaires