Skip to content

Instantly share code, notes, and snippets.

@fhefh2015
Last active June 26, 2020 03:35
Show Gist options
  • Save fhefh2015/b7afaf7a17ef69bed26f53341d92e6e0 to your computer and use it in GitHub Desktop.
Save fhefh2015/b7afaf7a17ef69bed26f53341d92e6e0 to your computer and use it in GitHub Desktop.
UILabel两端对齐
- (NSAttributedString *)textAligenJustifiedWith:(NSString *)text lineSpace:(CGFloat)lineSpace {
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
paragraphStyle.paragraphSpacing = 11.0;
paragraphStyle.paragraphSpacingBefore = 10.0;
paragraphStyle.firstLineHeadIndent = 0.0;
paragraphStyle.headIndent = 0.0;
paragraphStyle.lineSpacing = lineSpace;
NSDictionary *dict = @{
NSForegroundColorAttributeName :self.textColor,
NSFontAttributeName: self.font,
NSParagraphStyleAttributeName: paragraphStyle,
NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleNone]
};
[mutableAttributedString setAttributes:dict range:NSMakeRange(0, mutableAttributedString.length)];
NSAttributedString *attrString = [mutableAttributedString copy];
return attrString;
}
- (NSAttributedString *)textAligenJustifiedWith:(NSString *)text {
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
paragraphStyle.paragraphSpacing = 11.0;
paragraphStyle.paragraphSpacingBefore = 10.0;
paragraphStyle.firstLineHeadIndent = 0.0;
paragraphStyle.headIndent = 0.0;
NSDictionary *dict = @{
NSForegroundColorAttributeName :self.textColor,
NSFontAttributeName: self.font,
NSParagraphStyleAttributeName: paragraphStyle,
NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleNone]
};
[mutableAttributedString setAttributes:dict range:NSMakeRange(0, mutableAttributedString.length)];
NSAttributedString *attrString = [mutableAttributedString copy];
return attrString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment