iOS Swift Wrapping Text Around Image
This is a perfectly reasonable use of a UITextView. Your reasons for hesitation to use it are unclear. You can make the UITextView non-editable and non-selectable; the user will not know that it is a UITextView as opposed to to a UILabel.
If you don't like that solution, then what I would do is use, instead of a UILabel, a custom view that draws the text. You can draw the text with Text Kit and thus you can take complete charge of how the text draws. In particular, you can cause it to wrap however you like, including not drawing the text in the corner (exclusion path on the text container).
UILabel can not be wrapped and aligned at the same time
You are missing a few constraints.
To get a multiline label to wrap, it must have its width limited (how else would it know the text is too long?).
To get auto layout to adjust the cell's height, you need constraints on the content of the cell to "push down" the bottom of the cell.
So...
- Constrain your top-left label to
Leading: 0
,Top: 0
,Width: 77
(I'm using 77 as the width, based on your images). - Constrain your top-right label to
Leading: 8
(to top-left label's trailing),Top: 0
,Trailing: 0
- Constrain your bottom-left label to
Leading: 0
,Top: 8
(to top-left label's bottom),Width: 77
(or, width equal to top-left label) - Constrain your bottom-right label to
Leading: 8
(to bottom-left label's trailing),Top: 8
(to top-right label's bottom, orTop: 0
to top of bottom-left label),Trailing: 0
then, add Bottom
constraints of >= 0
to each of the bottom labels.
I'm guessing either bottom label may wrap to multiple lines, so set each one to Number of Lines: 0
The layout:
the result:
How to wrap text UILabel Xcode with Storyboard settings
You need to add leading
and trailing
constraint to your label
Or you can add a width constraint relative to the superview width
How to combine UIImage and UILabel into one image and save
Use [myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
to draw in current context.
For eg:-
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
[self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
[myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
Based on your comments, if you want to draw this in a particular frame do it as follows,
[myLabel drawTextInRect:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];
If you want to color the background, try this,
CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, rect.size.height);
CGContextSetRGBFillColor(context, 100.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f);
CGContextFillRect(context, drawRect);
or you can check this question Setting A CGContext Transparent Background.
Wrapping Text in a UITextView Around a UIImage WITHOUT CoreText
This seems to do the trick:
UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
self.textView.textContainer.exclusionPaths = @[imgRect];
Works only from iOS 7 and up.
How to resize properly the content of an UILabel? (see picture inside post to understand better)
use this func
func boundingRect(with size: CGSize,
options: NSStringDrawingOptions = [],
attributes: [String : Any]? = nil,
context: NSStringDrawingContext?) -> CGRect
e.g.
let sizeOfString = label.text.boundingRectWithSize(
CGSizeMake(self.label.frame.size.width, CGFloat.infinity),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: lbl.font],
context: nil).size
From here you get the size of the label and resize the frame of label according to you.
Dont forget to give >= contraint relation to the height of label and lines to 0 from storyboard.
Related Topics
Create a Tabbar Controller with a Master-Detail Template
How to Use Store and Use an Nsmutableattributedstring in Nsuserdefaults
How to Make Skscene Have Fixed Width
Regex to Get String Between Two % Characters
How to Apply Audio Effect to a File and Write to Filesystem - iOS
How to Add Navigation Interface After Create a Tab Bar Controller Programmatically (Swift)
Set the Status Bar to Black Colour
Displaying Text One Character at a Time in Swift 2.0
Store Time Efficiently in Firebase Database
Unrecognized Error in Mapview (Ios)
Position a Subview on the Edge of a Circular Shaped View
Duet - Merge 2 Videos Side by Side
Determine Percentage of Visibility of Horizontal Collectionview Cells on Screen
Module Compiled with Swift 5.0.1 Cannot Be Imported by the Swift 5.1 Compiler
Why Self.Locationmanager Stopupdatinglocation Doesn't Stop Location Update