How to dynamically split a UILabel string with text?

I have UILabel and I want to show the text on this shortcut. I want to increase the tag width by no more than 70% of the full screen of my device. If the text length of this label does not match this 70% of the size, then the label automatically moves to the next line as long as the text length. Each time the label length crosses 70% of the width of the main screen, then the lines also break. I tried several ways but could not solve. Please help me solve this problem.

Thanks in advance;

+4
source share
4 answers

Simple enough :)

Drag the label onto the storyboard and add top and leading restrictions to it.

, ( ViewController) ,

enter image description here

:) , ur 70% . , 0.7

enter image description here

70% !

, 70%:) 70% , :)

enter image description here

0.

Thats it:) :)

O/P:

: enter image description here

: enter image description here

EDIT:

! , :) lemme :)

EDIT:

, , , Edge

- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {0, 5, 0, 0};
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
+2

, , . , UILabel :

label.numberOfLines = 0; 
label.lineBreakMode = NSLineBreakByWordWrapping;

, UILabel , . , (NSString):

NSString *text = @"YourText";
CGFloat your70Width; // whatever your width is
CGSize constraintSize = CGSizeMake(your70Width, MAXFLOAT);
UIFont *yourLabelFont; // whatever your font is
CGRect requiredFrame = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:yourLabelFont} context:nil];

// Keeps the old x,y coordinates and replaces only the width and height.
CGRect oldLabelFrame = label.frame;
label.frame = CGRectMake(oldLabelFrame.origin.x, oldLabelFrame.origin.y, requiredFrame.size.width, requiredFrame.size.height);

.

+2

To increase the height of the label according to the content if you are using a storyboard. Give labels FOUR (Top, Bottom, Leading, Trailing), then go to the Attribute Inspector and make lines at 0, and at line break do WORD WRAP. <img src = "https://i.stack.imgur.com/8rQpN.png" alt = "enter a description of the image here">

+1
source
let lbl = UILabel()
lbl.numberOfLines = 0
lbl.lineBreakMode = .ByWordWrapping
0
source

Source: https://habr.com/ru/post/1672212/


All Articles