2

Have a problem, need to assign some text to a UIButton's title. Have set the buttons line break mode to NSLineBreakByCharWrapping so that the string is separated only by characters at end of each line. But i need to insert an hyphen at the end of the line to show continuity of the word. Heres what i tried -

    // Initialize the button

    titleButton.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    titleButton.titleLabel.backgroundColor = [UIColor yellowColor];
    [titleButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];

    // Make the string here
    NSMutableString *titleString = [[NSMutableString alloc] initWithString:@"abcdefghijklmnopqrs tuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"];
        // Insert hyphens 
        int titleLength = [titleString length];
        int hyphenIndex = 19;
        while (hyphenIndex<titleLength) { 
            UniChar charatIndex = [titleString characterAtIndex:hyphenIndex];
            if ((charatIndex - ' ') != 0) { // Check if its not a break bw two words
                [titleString insertString:@"-" atIndex:hyphenIndex]; // else insert an hyphen to  indicate word continuity
            }
            hyphenIndex += 19; //Since exactly 20 char are shown in single line of the button's label. 
        }
        //Set the hyphenated title for the button
        [titleButton setTitle:titleString forState:UIControlStateNormal];
        [titleString release];

This is the closest i could get.

enter image description here

Any help would be greatly appreciated.

stigi
  • 6,661
  • 3
  • 40
  • 50
nuteron
  • 531
  • 2
  • 8
  • 26
  • [See this answer](http://stackoverflow.com/questions/4421267/how-to-get-text-from-nth-line-of-uilabel/14413484#14413484). It will give you an array of lines of `UILabel`. – TheTiger Aug 06 '13 at 11:31
  • Please go through this link this will help you http://stackoverflow.com/a/38146955/4940425 – Binoy jose Jul 01 '16 at 18:08

4 Answers4

1

Try NSAttributedString

It give a great possibilities for working with strings.

WWDC 2012-230

For example:

- (NSUInteger)lineBreakBeforeIndex:(NSUInteger)index withinRange:(NSRange)aRange
//Returns the appropriate line break when the character at the index won’t fit on the same line as the character at the beginning of the range.
Flop
  • 153
  • 4
  • I suspect this is getting downvoted because the lineBreakBeforeIndex method seems to be OS X only, and the question is tagged iOS. Still a useful answer to have here for OS X people :) – Kenny Winker Oct 16 '13 at 21:59
0

your second word's length is more the length of line (width of button) thats why this happen.

Amit Battan
  • 2,968
  • 2
  • 32
  • 68
0

pasting hyphens is not very good idea. You should split your string on parts with size that fits button's width by using than add hyphens if needed

[String sizeWithFont:font constrainedToSize:Size lineBreakMode:LineBreakMode]

The main idea is to get portion of initial string (add hyphen if word breaks), check if its width fits button's width. If width is smaller, try bigger part, else if fits -- process further part

mike-dutka
  • 254
  • 1
  • 11
  • This is some thing i tried but not getting an accurate result. The error is approximately one character.. – nuteron Jan 11 '13 at 13:34
0

See my answer here for how to use NSParagraphStyle and NSAttributedString to get a UILabel that breaks with hyphens: https://stackoverflow.com/a/19414663/196358

Community
  • 1
  • 1
Kenny Winker
  • 11,919
  • 7
  • 56
  • 78