美文网首页
设置label文字顶格显示

设置label文字顶格显示

作者: 欲速则不达 | 来源:发表于2016-09-22 16:43 被阅读48次
    • (void)setTextTop
      {
      NSMutableString *resultStr = [self.text mutableCopy];
      NSArray *array = [resultStr componentsSeparatedByString:@"\n"];
      NSInteger temp = 0;
      NSInteger lines = self.lineBreakMode;
      for (int j = 0; j < array.count; j++) {
      NSString *str = array[j];

        if ([str isEqual:@""]) {
            temp --;
        }
        if (str.length > self.width - 40) {
            temp++;
        }
      

      }

      for (int i = 0; i < (lines - array.count - temp + 1); i++) {
      [resultStr appendString:@"\n"];
      }
      self.text = resultStr;
      }

    以下两个没试过:

    • (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];
      }

    相关文章

      网友评论

          本文标题:设置label文字顶格显示

          本文链接:https://www.haomeiwen.com/subject/qwvzettx.html