Add Carousel View inside Textview - ios

I am adding data into TextView through sqlite database. I have around 30 to 35 images to be added in it after some Para or Line of data, which i have done through NSTextAttachment. Now what i want to do is wherever there are more that 2 images i want it to be in Carousel View and remaining data after it. Have a look at my code. I tried the different way but not getting success. Any help will be much appreciated.
NSTextAttachment *textAttachment1 = [[NSTextAttachment alloc] init];
textAttachment1.image = [UIImage imageNamed:#"img1.png"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:#"img2.png"];
NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] init];
textAttachment2.image = [UIImage imageNamed:#"img3.png"];
NSTextAttachment *textAttachment3 = [[NSTextAttachment alloc] init];
CGFloat oldWidth1 = textAttachment1.image.size.width;
CGFloat oldWidth = textAttachment.image.size.width;
CGFloat oldWidth2 = textAttachment2.image.size.width;
CGFloat scaleFactor1 = oldWidth1 / (self.textViewResearch.frame.size.width + 70);
CGFloat scaleFactor = oldWidth / (self.textViewResearch.frame.size.width + 70);
CGFloat scaleFactor2 = oldWidth2 / (self.textViewResearch.frame.size.width + 70);
textAttachment1.image = [UIImage imageWithCGImage:textAttachment1.image.CGImage scale:scaleFactor1 orientation:UIImageOrientationUp];
textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];
textAttachment2.image = [UIImage imageWithCGImage:textAttachment2.image.CGImage scale:scaleFactor2 orientation:UIImageOrientationUp];
NSAttributedString *attrStringWithImage1 = [NSAttributedString attributedStringWithAttachment:textAttachment1];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
NSAttributedString *attrStringWithImage2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
[attributedString replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attrStringWithImage1];
[attributedString replaceCharactersInRange:NSMakeRange(13740, 0) withAttributedString:attrStringWithImage];
[attributedString replaceCharactersInRange:NSMakeRange(13741, 0) withAttributedString:attrStringWithImage2];
self.textView.attributedText = attributedString;

If I understood you correctly, you want to place an image inside a text view and let the text flow around it. Having more than 2 images you want to have a carousel, an instance of the class iCarousel.
Having a subview inside an instance of UITextView and letting the text float around it, is a known tasks. The easiest way seems to be to use the exclusion path of a text container like this:
// Get the dimension of the subview
iCarousel *subview = …;
UIBezierPath *subviewPath = [UIBezierPath bezierPathWithRect:subview.frame];
// Somewhere you should add it
UITextView *textView = …; // Wherever it comes from
[textView addSubview:subview];
// Let the text flow around it
UITextContainer *textContainer = textView.textContainer; // The text container is the region text is laid out in.
textContainer.exclusionPaths = [subviewPath]; // Do not lay out in this region.
Of course you have to change the exclusion region, whenever the frame of the subviews change.
A more flexible and more complex approach is to do the text layout your own. If the above approach does not work, let me know. I will add code for this.

Related

iOS: How to adjust placement of adjacent UIView based on text in UILabel next to it

There are two UIImageView and two UILabel set up like the illustration above. Both UILabel's text will be populated via Web Data and could be any length.
When the "Something" label inevitably gets populated with something longer than "something", the UIImageView to it's immediate right needs to move to the right based on the amount of text inside the "something" label.
How would one go about doing this?
Although please use auto layouts, here is a programmatic way of handling this:
// Image before Something text
UIImageView *somethingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 10.0, 40, 40)];
CGFloat somethingLabelXM = 5.0;
NSString *somethingLabelText = #"Random Text";
CGFloat somethingLabelTextW = [somethingLabelText sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:CGSizeMake(MAXFLOAT, 1000) lineBreakMode:NSLineBreakByTruncatingTail].width;
// Something text
UILabel *somethingLabel = [[UILabel alloc] initWithFrame:CGRectMake(somethingImageView.frame.origin.x + somethingImageView.frame.size.width + somethingLabelXM, somethingImageView.frame.origin.y, somethingLabelTextW, 40.0)];
somethingLabel.text = somethingLabelText;
UIImage *moreImage = [UIImage imageNamed:#"SomeImage"];
// Image after Something text
UIImageView *moreImageView = [[UIImageView alloc] initWithFrame:CGRectMake(somethingLabel.frame.origin.x + somethingLabel.frame.size.width + somethingLabelXM, somethingImageView.frame.origin.y, moreImage.size.width, moreImage.size.height)];
moreImageView.image = moreImage;
// More text
NSString *moreLabelText = #"Random Text";
CGFloat moreLabelTextW = [moreLabelText sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:CGSizeMake(MAXFLOAT, 1000) lineBreakMode:NSLineBreakByTruncatingTail].width;
UILabel *moreLabel = [[UILabel alloc] initWithFrame:CGRectMake(moreImageView.frame.origin.x + moreImageView.frame.size.width + somethingLabelXM, somethingImageView.frame.origin.y, moreLabelTextW, 40.0)];
somethingLabel.text = moreLabelText;

Duplicated UITextView doesn't show all text from the original

I'm trying to make a duplicate UITextView based on another UITextView for sharing purposes. This text view is added as a subview to viewToShare. There is an issue however, the duplicated text view doesn't show all of the original text from the text view I'm copying from. I'm using attributed text as well on the original text view, so I'm not sure if this is the issue. Setting the background color to black on textViewCopy showed me that the frame size is correct. For some reason it seems like new line \n characters from the original text view are causing havoc and preventing the text to be fully shown in the textViewCopy. I wonder if it's related to this question: NSAttributedString '\n' ignored
Screenshots:
Code:
- (UIView *)shareView
{
CGSize size = self.containerView.bounds.size;
UIView *viewToShare = [[UIView alloc]init];
viewToShare.backgroundColor = self.containerView.backgroundColor;
viewToShare.layer.cornerRadius = 6.0;
viewToShare.layer.masksToBounds = YES;
UITextView *textViewCopy = [[UITextView alloc]init];
textViewCopy.backgroundColor = [UIColor clearColor];
textViewCopy.tag = 1;
UIEdgeInsets textContainerInsets = self.textView.textContainerInset;
viewToShare.frame = CGRectMake(0, 0, size.width, size.height);
textViewCopy.frame = CGRectMake(0, 0, size.width, size.height);
textViewCopy.textContainerInset = textContainerInsets;
NSAttributedString *attributedStringCopy = [[NSAttributedString alloc]
initWithAttributedString:self.textView.attributedText];
textViewCopy.attributedText = attributedStringCopy;
[viewToShare addSubview:textViewCopy];
return viewToShare;
}
I figured out how to solve the issue based on this SO: Large Text Being Cut Off in UITextView That is Inside UIScrollView
- (UIView *)shareView
{
CGSize size = self.containerView.bounds.size;
UIView *viewToShare = [[UIView alloc]init];
viewToShare.backgroundColor = self.containerView.backgroundColor;
viewToShare.layer.cornerRadius = 6.0;
viewToShare.layer.masksToBounds = YES;
UITextView *textViewCopy = [[UITextView alloc]init];
textViewCopy.backgroundColor = [UIColor clearColor];
textViewCopy.tag = 1;
UIEdgeInsets textContainerInsets = self.textView.textContainerInset;
viewToShare.frame = CGRectMake(0, 0, size.width, size.height);
textViewCopy.frame = CGRectMake(0, 0, size.width, size.height);
// These two lines are needed to fix bug!
textViewCopy.scrollEnabled = NO;
textViewCopy.scrollEnabled = YES;
textViewCopy.textContainerInset = textContainerInsets;
NSAttributedString *attributedStringCopy = [[NSAttributedString alloc]
initWithAttributedString:self.textView.attributedText];
textViewCopy.attributedText = attributedStringCopy;
[viewToShare addSubview:textViewCopy];
return viewToShare;
}

Custom Icon For UITextView in ObjectiveC

I want to create a UITextView (or UITextField) with custom icon.
I know how to add a UIImageView to the UITextView but the cursor ignore the UIImageView and I cant edit or delete the icon (the UIImageView).
What is the best way to do it?
I know that it is possible because apple use it in the iMessage (image attached) and there is another application name "kik" with the same behavior.
Try this gever ;) :
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"before after"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:#"download.jpeg"];
CGFloat oldWidth = textAttachment.image.size.width;
CGFloat scaleFactor = oldWidth / (self.textField.frame.size.width - 10);
textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage];
self.textField.attributedText = attributedString;

Add image to the left of a UILabel

I have this relatively simple question, I think.
Imagine I have a UILabel with some text in it. Then, I want on the left, or right side of
the text also an image to be displayed (added).
Something like this:
http://www.zazzle.com/blue_arrow_button_left_business_card_templates-240863912615266256
Is there a way to do it, using, say UILabel methods? I didn't find such.
Just in case someone else look this up in the future. I would subclass the UIlabel class and add an image property.
Then you can override the setter of the text and image property.
- (void)setImage:(UIImage *)image {
_image = image;
[self repositionTextAndImage];
}
- (void)setText:(NSString *)text {
[super setText:text];
[self repositionTextAndImage];
}
In repositionTextAndImage, you can do your positioning calculation. The code I pasted, just insert an image on the left.
- (void)repositionTextAndImage {
if (!self.imageView) {
self.imageView = [[UIImageView alloc] init];
[self addSubview:self.imageView];
}
self.imageView.image = self.image;
CGFloat y = (self.frame.size.height - self.image.size.height) / 2;
self.imageView.frame = CGRectMake(0, y, self.image.size.width, self.image.size.height);
}
Lastly, override drawTextInRect: and make sure you leave space on the left of your label so that it does not overlap with the image.
- (void)drawTextInRect:(CGRect)rect {
// Leave some space to draw the image.
UIEdgeInsets insets = {0, self.image.size.width + kImageTextSpacer, 0, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
I just implemented similar thing in my Live Project, hope it will be helpful.
-(void)setImageIcon:(UIImage*)image WithText:(NSString*)strText{
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = image;
float offsetY = -4.5; //This can be dynamic with respect to size of image and UILabel
attachment.bounds = CGRectIntegral( CGRectMake(0, offsetY, attachment.image.size.width, attachment.image.size.height));
NSMutableAttributedString *attachmentString = [[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:strText];
[attachmentString appendAttributedString:myString];
_lblMail.attributedText = attachmentString;
}
Create a custom UIView that contains a UIImageView and UILabel subview. You'll have to do some geometry logic within to size the label to fit the image on the left or right, but it shouldn't be too much.
Create a UIImageView with your image and add the UILabel on top like
[imageview addSubView:label];
Set the frame of the label accordingly to your required position.

Place an UIImage after an UILabel

I want to have an UILabel with a max width of 100 and after that an UIImage, how can I do that in interface builder? I want that if the text is shorter the label is less than 100 but the UIImage is right behind the label.
You can use NSTextAttachment class available from iOS 7.No need to play with the frame.
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:#"image.png"];
NSAttributedString *attachmentAttrString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *str= [[NSMutableAttributedString alloc] initWithString:#"hello"];
[str appendAttributedString:attachmentAttrString];
myLabel.attributedText = str;
you can do it with code like this:
first init the label and the imageView
_label = [[UILabel alloc] initWithFrame:CGRectZero];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(label.frame.origin.x + label.frame.size.width, label.frame.origin.y, 30, 30)];
then when the label has text , you can calc the width of the label`s frame
CGSize size = [_label.text sizeWithFont:[UIFont boldSystemFontOfSize:15]];
CGFloat width = size.width > 100 ? 100 : size.width;
CGFloat height = size.height;
_label.frame = CGRectMake(0, 0, width, height);
I'd like to add my swift solution:
let label = UILabel()
let attributedString = NSMutableAttributedString(string: "Hello".uppercaseString)
let attachment = NSTextAttachment()
attachment.image = UIImage(named:"hello")
attributedString.appendAttributedString(NSAttributedString(attachment:attachment))
// use the following method to insert the image at any index
// attributedString.insertAttributedString(NSAttributedString(attachment:attachment), atIndex: 0)
label.attributedText = attributedString

Resources