iOS Swift Wrapping Text Around Image
I have a UILabel that will contain various lengths of text. I need to place an image in the upper left corner of the text and have the text wrap around it. How can I do this? All I could find was using a UITextView which I don’t want to use since it’s static text.
- iOS - color on xcode simulator is different from the color on device
- How to download the file from Parse.com using the REST API?
- How to make UI object responsive after CABasicAnimation
- Get last cell in a UITableview section
- Xcode Instrument : Memory Terms Live Bytes and Overall Bytes (Real Memory) confusion
- Get Device Type/ Model
2 Solutions Collect From Internet About “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).
You can achieve this using NSTextAttachment and attributed text.
NSMutableAttributedString *myText = [[NSMutableAttributedString alloc] initWithString:labelStr]; NSTextAttachment *attachment = [[NSTextAttachment alloc] init] attachment.image = yourImage; NSAttributedString *attachmentLock = [NSAttributedString attributedStringWithAttachment:attachment]; NSMutableAttributedString *lockString = [[NSMutableAttributedString alloc] initWithAttributedString:myText]; //set your image range within the text. modify it till you get it right. NSRange range = NSMakeRange(0,[labelStr length]); [lockString replaceCharactersInRange:NSMakeRange(range.location, 1) withAttributedString:attachmentLock]; yourLabel.attributedText = lockString;
- PresentViewController from custom TableCell in xib
- Why is it always possible to access the .0 element of an optional tuple in Swift?
- Getting the location of a PHAsset in Swift
- Problems with PHP and MySQL
- How can I get the iOS device CPU architecture in runtime
- Observing Asynchronous Requests
- UITableView when changing constraint which effects height of cell after dequeueing, end up with broken constraints
- viewForAnnotation confusion and customizing the pinColor iteratively
- Always seeing Mirror image while capturing from Front Camera iOS 5.0
- Convert UIImage to CMSampleBufferRef
- Random custom view selector
- Consecutive Number After 16 in iOS
- How to launch a ViewController from a Non ViewController class?
- objective-c code to right pad a NSString?
- Save event to user's calendar