开发者

Justify Text in UILabel iOS

I am having a problem that in iOS I am using UILabel to display 2,3 line text, I want to align text as justified but I am not finding any option to do so. Any suggestions how to make justify text in label?

i put these li开发者_JS百科ne to make start it from top

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;

so any trick like this to make it justfiy Thanks


There is now a convenient way to justify text since iOS6. You can create an instance of NSAttributedString, set appropriate properties and assign this attributed string to a text representing view such as UILabel, UITextView, etc. It's easy as this:

  1. Create an instance of NSMutableParagraphStyle and set its properties.

    NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
    paragraphStyles.alignment = NSTextAlignmentJustified;      //justified text
    paragraphStyles.firstLineHeadIndent = 10.0;                //must have a value to make it work
    
  2. Create NSDictionary for text attributes and create attributed string.

    NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes];
    
  3. Set attributed string to a label.

    existingLabel.attributedText = attributedString;
    


Can't be done I'm afraid - well not with UILabel.

You can use the UIWebView or a 3rd party library such as OHAttributedLabel

Happy Coding :)

Update:

This answer has been obsolete since iOS6. Please refer to Tankista's answer.


As mentionned by @martin, my class OHAttributedLabel can make this very easily. (You will find it on my github and also find plenty of references to it on SO as well)


It can be done easily, but you need to use Core Text.

subclass a UIView, add an NSString property, create an NSAttributedString and pass kCTJustifiedTextAlignment value for the kCTParagraphStyleSpecifierAlignment key, then draw the NSAttributedString using Quartz or CoreText in your drawrect method.

edit: kCTParagraphStyleSpecifierAlignment key kCTJustifiedTextAlignment value should be used to create a CTParagraphStyleRef struct and passed in as a value for kCTParagraphStyleAttributeName key when creating the NSAttributedString.


SWIFT 4.x

version of approved answer:

  1. Create an instance of NSMutableParagraphStyle and set its properties.

    let justifiedParagraphStyles: NSMutableParagraphStyle = NSMutableParagraphStyle.init()
    justifiedParagraphStyles.alignment = .justified      //justified text
    justifiedParagraphStyles.firstLineHeadIndent = 10.0  //must have a value to make it work
    
  2. Create NSDictionary for text attributes and create attributed string.

    let attributes = [NSAttributedStringKey.paragraphStyle: justifiedParagraphStyles]
    let attributedString = NSAttributedString.init(string: string, attributes: attributes)
    
  3. Set attributed string to a label.

    existingLabel.attributedText = attributedString
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜