Attributed Text Center Alignment

前端 未结 10 2454
故里飘歌
故里飘歌 2020-12-13 17:06

I have tried everything but cannot seem to center this text. Can someone please tell me where the error is.

NSMutableParagraphStyle *paragraphStyle = NSMutab         


        
10条回答
  •  甜味超标
    2020-12-13 17:18

    Swift 4+

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.center
    
    // Swift 4.2++
    let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedString.Key.paragraphStyle:paragraphStyle])
    
    // Swift 4.1--
    let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])
    
    let yourLabel = UILabel()
    yourLabel.attributedText = attributedString
    

    Objective-C

    NSString *string = @"Your String";
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes: @{NSParagraphStyleAttributeName:paragraphStyle}];
    UILabel *label = [[UILabel alloc] init];
    label.attributedText = attributedString;
    

提交回复
热议问题