รู้ว่า UILabel แยกสตริงที่ไหนเมื่อคุณสมบัติหลายบรรทัดเปิดอยู่

มีปัญหา จำเป็นต้องกำหนดข้อความให้กับชื่อเรื่องของ UIButton ตั้งค่าโหมดตัวแบ่งบรรทัดของปุ่มเป็น NSLineBreakByCharWrapping เพื่อให้สตริงถูกคั่นด้วยอักขระที่ท้ายแต่ละบรรทัดเท่านั้น แต่ฉันต้องใส่ยัติภังค์ที่ท้ายบรรทัดเพื่อแสดงความต่อเนื่องของคำ นี่คือสิ่งที่ฉันลอง -

    // 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];

นี่คือสิ่งที่ใกล้เคียงที่สุดที่ฉันสามารถทำได้

ป้อนคำอธิบายรูปภาพที่นี่

ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมอย่างมาก.


person nuteron    schedule 09.01.2013    source แหล่งที่มา
comment
ดูคำตอบนี้ มันจะให้อาร์เรย์ของบรรทัด UILabel แก่คุณ   -  person TheTiger    schedule 06.08.2013
comment
โปรดไปที่ลิงก์นี้ซึ่งจะช่วยคุณได้ stackoverflow.com/a/38146955/4940425   -  person Binoy jose    schedule 01.07.2016


คำตอบ (4)


ความยาวของคำที่สองของคุณคือความยาวของเส้น (ความกว้างของปุ่ม) มากกว่านั่นคือสาเหตุที่เกิดเหตุการณ์นี้

person Amit Battan    schedule 09.01.2013

การวางยัติภังค์ไม่ใช่ความคิดที่ดีนัก คุณควรแบ่งสายออกเป็นส่วนๆ ให้มีขนาดพอดีกับความกว้างของปุ่มโดยใช้เครื่องหมายยติภังค์เพิ่มหากจำเป็น

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

แนวคิดหลักคือการได้รับส่วนหนึ่งของสตริงเริ่มต้น (เพิ่มยัติภังค์หากคำขาด) ตรวจสอบว่าความกว้างพอดีกับความกว้างของปุ่มหรือไม่ หากความกว้างเล็กลง ให้ลองส่วนที่ใหญ่กว่า ถ้าอย่างอื่นพอดี -- ดำเนินการส่วนต่อไป

person mike-dutka    schedule 09.01.2013
comment
นี่คือสิ่งที่ฉันลองแล้ว แต่ไม่ได้ผลลัพธ์ที่แม่นยำ ข้อผิดพลาดประมาณหนึ่งตัวอักษร.. - person nuteron; 11.01.2013

ลองใช้ NSAttributedString

มันให้ความเป็นไปได้อย่างมากในการทำงานกับสตริง

WWDC 2012-230

ตัวอย่างเช่น:

- (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.
person Flop    schedule 06.08.2013
comment
ฉันสงสัยว่านี่กำลังถูกลดระดับลงเพราะเมธอด lineBreakBeforeIndex ดูเหมือนจะเป็น OS X เท่านั้นและคำถามถูกแท็ก iOS ยังคงเป็นคำตอบที่มีประโยชน์สำหรับคน OS X :) - person Kenny Winker; 17.10.2013

ดูคำตอบของฉันที่นี่สำหรับวิธีใช้ NSParagraphStyle และ NSAttributedString เพื่อรับ UILabel ที่ขึ้นต้นด้วยยัติภังค์: https://stackoverflow.com/a/19414663/196358

person Kenny Winker    schedule 16.10.2013