在UILabel iOS中对齐文本

Zak*_*ksh 15 iphone uilabel

我有一个问题,在iOS我使用UILabel显示2,3行文本,我想将文本对齐,但我没有找到任何选项.有关如何在标签中进行合理文本的任何建议吗?

我把这些线从顶部开始

CGSize maximumSize = CGSizeMake(300, 9999);
NSString *textString = someString;
UIFont *textFont = [UIFont fontWithName:@"Futura" size:14];
CGSize textStringSize = [textString sizeWithFont:textFont 
                               constrainedToSize:maximumSize 
                                   lineBreakMode:text.lineBreakMode];

CGRect textFrame = CGRectMake(10, 110, 300, textStringSize.height);
text.frame = textFrame;
Run Code Online (Sandbox Code Playgroud)

所以任何这样的技巧,使它只是谢谢

Pet*_*ger 35

从iOS6开始,现在有一种方便的方法来证明文本.您可以创建NSAttributedString的实例,设置适当的属性并将此属性字符串分配给表示视图的文本,如UILabel,UITextView等.这很容易:

  1. 创建NSMutableParagraphStyle的实例并设置其属性.

    NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
    paragraphStyles.alignment = NSTextAlignmentJustified;      //justified text
    paragraphStyles.firstLineHeadIndent = 10.0;                //must have a value to make it work
    
    Run Code Online (Sandbox Code Playgroud)
  2. 为文本属性创建NSDictionary并创建属性字符串.

    NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes];
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将属性字符串设置为标签.

    existingLabel.attributedText = attributedString;
    
    Run Code Online (Sandbox Code Playgroud)