Duplicated UITextView doesn't show all text from the original - ios

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;
}

Related

UILabelView cropping when expanded, but renders fully when inspected in view hierarchy

Image 1 shows issue. label has flexible width & height constrains, also we adding fix width & height to expand with. in View hierarchy it shows fully expanded as expected, but issue in real view:
Image 2 shows fully expanded label in View hierarchy:
Lable code
self.textLabel = [[RAVerticallyAlignedLabel alloc] initWithFrame: self.bounds andVerticalTextAlignment: [self verticalTextAlignment]];
self.textLabel.backgroundColor = ClearColour;
self.textLabel.userInteractionEnabled = NO;
self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.textLabel.numberOfLines = 0;
viewToTruncate = self.textLabel;
textWidth = MAX(MIN(screenSize.width - 20, [label.text sizeWithAttributes: #{NSFontAttributeName : label.font}].width + 10);
[UIView animateWithDuration: 0.3 animations: ^
{
self->containerView.frame = CGRectMake(xPos, top , MIN(textWidth, screenSize.width - 2 * RAPadding), newTextviewHeight);
// Container view is added inside scroll view
self->containerView.layer.shadowRadius = kRATruncationUtiltityShadowRadius;
self->containerView.layer.shadowOpacity = kRATruncationUtiltityShadowOpacity;
self->containerView.layer.shadowOffset = kRATruncationUtiltityShadowOffset;
self->containerView.layer.shadowPath = [UIBezierPath bezierPathWithRect: self->containerView.bounds].CGPath;
self->containerView.layer.masksToBounds = NO;
self->viewToExpand.frame = CGRectMake(0, 0, textWidth + componentContentWidth, self->containerView.height);
//viewToExpand is UILable, added inside container view
self->viewToExpand.layer.borderWidth = 1.0;
self->viewToExpand.layer.borderColor = borderColor.CGColor;
self->viewToExpand.layer.backgroundColor = backgroundColor.CGColor;
}];
Found issue fix, it size wasn't issue but "gradientMask" was playing up on expand as below:
CAGradientLayer *gradientMask = [CAGradientLayer layer];
gradientMask.frame = CGRectMake(0, 0, viewToTruncate.width, viewToTruncate.height);
gradientMask.colors = #[(id)WhiteColour.CGColor,(id)WhiteColour.CGColor, (id)ClearColour.CGColor];
gradientMask.locations = #[#0, #(locationOfFade), #1];
viewToTruncate.layer.mask = gradientMask;
removing mask on expand , it was rendering as expected.

IOS/Objective-C: Subview of Subview not displaying

I am trying to create a chart where a bar in the form of a UIView displays on top of a background UIView. I'd like both to display on top of the UIView for the whole screen. I have done this before successfully, but while I can get the first view to display, I somehow can't get my code to display the bar. Could it have something to do with setting the color? Or can anyone suggest why the second subview is not displaying.
My code:
//Make background box:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat graphWidth = screenWidth-40;
CGFloat graphHeight = 160;
CGRect graphBounds =CGRectMake(20, 200, graphWidth, graphHeight);
float tableStartY = graphBounds.origin.y;
UIView *graphBox = [[UIView alloc] initWithFrame:graphBounds];
graphBox.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.2]; graphBox.layer.borderWidth = 1;
graphBox.layer.borderColor = [UIColor blueColor].CGColor;
//Make Bar
CGFloat barWidth = 20;
CGFloat barHeight = 100;
CGRect aBar = CGRectMake(20, tableStartY+1, barWidth, barHeight);
UIView *barView = [[UIView alloc] initWithFrame:aBar];
barView.backgroundColor = [UIColor blueColor];
barView.layer.borderWidth = 1;
barView.layer.borderColor = [UIColor redColor].CGColor;
// [graphBox addSubview:barView];
[self.view addSubview: graphBox];
If I run the above code, it displays the graphBox. If I add the bar directly to the view as a subView instead of the graphBox, the bar displays. However, if I uncomment out the line shown and add the barView first to the graphBox and then add the graphBox to the view, the barView does not display.
Thanks in advance for any suggestions.
If I understand correctly what you need to do, you should replace
CGRect aBar = CGRectMake(20, tableStartY+1, barWidth, barHeight);
with
CGRect aBar = CGRectMake(20, 1, barWidth, barHeight);
[edit: and obviously uncomment the addSubview line]
Perhaps this is an accident in your posted code, but you have specifically commented out where the barView would be added to the screen.
// [graphBox addSubview:barView];
In addition, as another answer lists, your offset is incorrect if you are adding barView to graphBox. If you add it to self.view instead, your offset is correct.
So, you've got two choices, depending on the containment you desire in your view hierarchy:
CGRect aBar = CGRectMake(20, 1, barWidth, barHeight);
// ...
[graphBox addSubview:barView];
or
CGRect aBar = CGRectMake(20, tableStartY+1, barWidth, barHeight);
// ...
[self.view addSubview: graphBox];
[self.view addSubview:barView];
Note that in the second option, the order is important to get the barView to display over top of the graphBox as they will be siblings.

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;

Having difficulty wrapping text

I use the following code to add text in IOS
//Set up label frame
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 210, 80)];
self.answer_text_label = tempLabel;
[tempLabel release];
[self.preview_answer_container addSubview:self.answer_text_label];
//Populate label with text
self.answer_text_label.text = self.answer.text;
self.answer_text_label.numberOfLines = 4;
self.answer_text_label.lineBreakMode = UILineBreakModeWordWrap;
[self.answer_text_label sizeToFit];
However, the result I get is as such, the text seems to overflow to the right instead of staying within the frame as stipulated in my label setup 'CGRectMake(100, 10, 210, 80)'
The wrapping works if I change to self.answer_text_label.numberOfLines = 0. But this will not work for me since I need to constrain the text within my stipulated label frame.
Any way I can wrap the text and keep to only 4 lines?
EDIT:
Try the suggested code
self.answer_text_label.text = self.answer.text;
[self.answer_text_label sizeToFit];
CGRect labelRect = self.answer_text_label.frame;
labelRect.origin.y = CGRectGetMaxY(labelRect);
labelRect.size = self.answer_text_label.frame.size;
self.answer_text_label.frame = labelRect;
result as follows. Did not seem to solve my problem
Try setting frame explicitly -
[self.answer_text_label sizeToFit];
CGRect labelRect = self.answer_text_label.frame;
labelRect.origin.y = CGRectGetMaxY(labelRect);
labelRect.size = self.answer_text_label.frame.size;
self.answer_text_label.frame = labelRect;
EDIT - Don't need to use this, just use following -
remove these of code just use below, no other property of frame, remove sizeToFit as well -
self.answer_text_label.numberOfLines = 4;
self.answer_text_label.lineBreakMode = UILineBreakModeWordWrap;
For vertical alignment - (With above line of code, use this as well, and do don't use size to fit)
CGSize textSize = [self.answer_text_label.text sizeWithFont:self.answer_text_label.font
constrainedToSize:CGSizeMake(self.answer_text_label.frame.size.width, MAXFLOAT)
lineBreakMode:self.answer_text_label.lineBreakMode];
self.answer_text_label.frame = CGRectMake(20.0f, 20.0f, textSize.width, textSize.height);
In iOS 6 and later, use NSLineBreakByWordWrapping, not UILineBreakModeWordWrap.
self.answer_text_label.lineBreakMode = NSLineBreakByWordWrapping;

iOS, how to draw the dynamic length of text?

The picture is: http://www.flickr.com/photos/71607441#N07/6641626163/
The background is a UIImageView, and the blue part I want to show as "title" of the image.
I used UILabel, but the length of the text is dynamic. It can be one line or two line, at most two line. If the text is longer than two lines, it will be truncated.
The blue part looks like "highlight in Microsoft Word", but it is not "highlight in iOS UILabel.text"
Is there anyone can help me?
Try this code :----
CGSize maximumSize = CGSizeMake(320, 30);
UILabel *newsLabel = [[UILabel alloc] init];
newsLabel.textColor = [UIColor whiteColor];
newsLabel.font = [UIFont boldSystemFontOfSize:11];
newsLabel.backgroundColor = [UIColor clearColor];
newsLabel.lineBreakMode = UILineBreakModeWordWrap;
newsLabel.numberOfLines = 0;
lineBreakMode:newsLabel.lineBreakMode];
CGSize dateStringSize = [#"Text Input" sizeWithFont:newsLabel.font
constrainedToSize:maximumSize
lineBreakMode:newsLabel.lineBreakMode];
CGRect dateFrame = CGRectMake(5, 5, 320, dateStringSize.height); //breath can be any desired float value
newsLabel.text = #"Text Input";
newsLabel.textAlignment = UITextAlignmentCenter;
newsLabel.frame = dateFrame;
You can find the size in pixels required for your title using:
CGSize size = [UILabel.text sizeWithFont:yourFont];
there's also:
CGSize size = [UILabel.text sizeWithFont:yourFont lineBreakMode: yourLineBreakMode];
You could then use these dimensions (size.width, size.height) to set the frame of your UILabel.
Hope this helps. :)

Resources