How to get length of text for last line in UITextView - ios

In my app, I'm trying to have the UILable time text aligned in a way like WhatsApp where if the last sentence of a UITextView's message text is too long the time would be pushed to the next line. So I'm actually trying to align the UILabel's time according to the UITextView's message box.
message box time layout image
This is a text message. 7:16PM
This is a longer
text message. 7:15PM
This is an even longer
text message till end.
7.15PM
One way I could think of was to use UITextView's class and grab the length of the last line and calculate the text width as compared with the UITextView's width to know if it exceeds but no luck. Is there any way?

This is the code that I wrote which solved my issue. Thanks to Greg for pointing me in the right direction.
+ (BOOL)didTextMoveToNewline:(NSString *)text previousSize:(CGSize)previousSize {
UIFont *messageBubbleFont = [UIFont systemFontOfSize:14.0f];
float maximumTextWidth = 188;
NSString *finalText = [[NSString alloc] init];
finalText = [NSString stringWithFormat:#"%# 10:00PM ", text];
CGRect stringRect = [finalText boundingRectWithSize:CGSizeMake(maximumTextWidth, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:#{ NSFontAttributeName : messageBubbleFont }
context:nil];
CGSize stringSize = CGRectIntegral(stringRect).size;
if (stringSize.height > previousSize.height)
return YES;
else
return NO;
}

The hack would be to automatically add a blank space the length of the time to the end of every message.
let userMessageLabel.text = userMessage + " "
Then let the time always overlap the last line. If the amount of spaces is correct, the text and time will never touch.

Related

How to keep a space at the end of the string in ios, objective C without changing its UI(position)

I have created a label programmatically.it's with is equal to the device width and I have aligned it to right.so it shows the text from right.
like this
titleLabel.textAlignment = NSTextAlignmentRight;
then I gave a text to the label.
titleLabel.text = #"Flight Summary";
but I want to keep a space after y letter in summary, without decreasing the width of the label.I tried with using string format like this.
titleLabel.text =[NSString stringWithFormat:#"%# ", #"Flight Summary "];
but nothing happned.how can I do that.hope your help for this.thanx.
To reduce the complexity of subclassing, you can take UIView and UIlabel, set frame of UIView to screen width and take UILabel frame as screenwidth - 8 (or whatever pixels is appropriate for you). Manage the frames and add both to mainview, this way you will be able to achieve the look.
Try this
titleLabel.text = #"abcd exadgdf \u{200c}""
You can also do one more thing. You can use attributed string and set attributed text to titlelabel without formatting string like this
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:#"ABCD "];
titleLabel.attributedText = attrString;

How much text a label can adopt

In my project there is a specific requirement, When UILabel's text gets truncated I need to give view more functionality. Initially there will be a CGRect given. Accordingly we need to show the label if text truncated we need to at the end of label ...View more text should be shown. Upon tapping on ...View more view more I need to make my label bigger. So I m doing
NSMutableString *truncatedString = [text mutableCopy];
[truncatedString appendString:ellipsis];
NSRange range = NSMakeRange(truncatedString.length - (ellipsis.length + 1), 1);
do {
[truncatedString deleteCharactersInRange:range];
range.location--;
[self setText:truncatedString];
} while ([self isTextTruncated]);
it works fine for smaller text since I m using it for UITableViewCell. It is lagging for bigger texts since above operation happens for every time. So I want to know the text that is adopted in UILabel so that I can do any operation with new text. Any help would be greatly appreciated.
EDIT:
I have a label and bigger text. say my text is
"Apple Inc. is an American multinational corporation headquartered in Cupertino, California, that designs, develops, and sells consumer electronics, computer software, online services, and personal computers." if my label would adopt only "Apple Inc. is an American multinational corporation.." I need this text alone
Use this method to calculate height that would be required for the text to get fit into the provided UILabel:
- (CGFloat)getLabelHeight:(UILabel*)label
{
CGSize constraint = CGSizeMake(label.frame.size.width, 20000.0f);
CGSize size;
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [yourString boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:label.font}
context:context].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
return size.height;
}
Compare the returned height with height of your label:
CGFloat heightRequired = [self getLabelHeight:myLabel];
if(myLabel.frame.size.height < heightRequired) {
//you need to show more because the text is more than the label width and height.
}
else {
//you don't need to show more because the text is not more than the label width and height.
}
EDIT: The purpose of comparing height is to check whether frame is enough to show text or not. So, even if you want to increase the width of label to show more text, it will give you desired result.
You need to calculate size of text as bellow and if returned size is bigger than text field size than you need to show ...View more
CGSize requiredSize = [text sizeWithFont:withFont constrainedToSize:textViewSize lineBreakMode:lineBreakMode];
If you didn't find an answer, i recommend this workaround:
By trying using a text with known length, get the max number of characters that the label fits,and use that value to do the following:
int maxNumOfChar = 15; //For example
if (text.length > maxNumOfChar){
NSString* viewMore = #"...View More";
text = [[text substringToIndex:maxNumOfChar - [viewMore length]] stringByAppendingString: viewMore];
}

How to shift subview of UITextView by size of one line when cursor goes to next line?

I have a textView and I added an image to this textView. When i write text and go to next line I want to shift this image down by size of one line.. Similar like in twitter app when you create a tweet with picture. In this code I can shift picture by line, but my problem is it shifts to late (only on second symbol of next line).. I tried to use different values in CGSizeMake but line was shifted too early or too late.. Any recommendations?
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
CGSize constrainedSize = CGSizeMake(self.textView.contentSize.width, self.textView.contentSize.height);
UIFont *fontText = [UIFont systemFontOfSize:17];
CGRect textRect = [textView.text boundingRectWithSize:constrainedSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:fontText}
context:nil];
size = textRect.size;
CGFloat height = ceilf(size.height);
CGFloat width = ceilf(size.width);
CGRect frame = CGRectMake(0.0f, height+50, 100.0f, 100);
_imageView.frame=frame;
return YES;
}
You are hardcoding your font size. Maybe the text field has a different font. Use textField.font instead.
Also, you should simplify your code and eliminate the unused variables, such as width.
Finally, you should set a breakpoint where you calculate the size and keep checking if it is correct. Based on that it should be easy to debug.
Also, please note that textField.text does not yet contain the text that is going to be added. Before checking the size, you would have to first construct the new resulting string yourself.
NSString *newText = [textField.text
stringByReplacingCharactersInRange:range withString:text];
I think you should add your Image as subview to superview of your textView.
In shouldChangeTextInRange: fit frame of textView to content of textView.
Then just use KVO for catch moments, when frame of textView was changed. So, now you can keep top border of image on bottom border of textview, even animated!
I wont write code instead you, but it sound very easily and interesting, right?

How to put different headIndent (alignment) for every paragraph in UITextView

In my application i need to align all the paragraph differently.
like, first paragraph's headIndent is 0.0f then second's 10.0f and third's 3.0f.
i am giving all the paragraph style to textview.attributedText. and it took only one style.
Here whole text will come dynamically by Typing. means when User will type in text view at that time. so, there are no static string to do this.
I am placing all the characters in UITextView by this...
UIFont *fontBold = [UIFont fontWithName:#"Helvetica-Bold" size:15];
attributesHelveticaBold = #{NSFontAttributeName :fontBold};
UIFont *fontNormal = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
attributesNormal = #{NSFontAttributeName :fontNormal};
if (varBold== 1) {
[textView setTypingAttributes:attributesHelveticaBold];
}
else {
[textView setTypingAttributes:attributesNormal];
}
And i want to get this kind of result in text view
When i am typing the typing become slow too.
but i think i'll come over that issue but for now i stuck on this alignment problem.
how to do it when bullet point come and when different text come.
any kind of link, code, tutorial will be great help...
---------- Edit : ----------
Please have a look in Evernote's application.
I need to do the exactly same thing in my app. for alignment of second,third,etc line when bullet come.
-------- Edit after searching :-------
I searched too much for this but ain't find anything by googling.
So, now i am asking if anyone now about How to give any paragraph style or any attribute style to a paragraph and just leave it as it is on text view and then perform other paragraph style on second paragraph. at this time the first paragraph will not pass throw "shouldChangeTextInRange" method.
yes, it's quite confusing whatever i am saying.
so i explaining it in general...
if user set the text view's first paragraph's headIndent=7.0f then when user will type next paragraph and set the headIndent = 13.0f then first paragraph will stay as it is in textview and just running paragraph will come in a chapter (means in a method).
right now i am doing these thing in shouldChangeTextInRange method to do style for each paragraph.
varStaringPointOfString = 0;
varEndingPointOfString = 0;
NSArray *sampleArrToGetattrStr = [txtViewOfNotes.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (int i=0; i<[sampleArrToGetattrStr count]; i++)
{
NSString *strtostoreLength = [NSString stringWithFormat:#"%#",[sampleArrToGetattrStr objectAtIndex:i]];
varStaringPointOfString = (int)strtostoreLength.length + varEndingPointOfString;
if ([strtostoreLength hasPrefix:#"\t•\t"])
{
[[textView textStorage] addAttribute:NSParagraphStyleAttributeName value:paragraphStyleForBullet range:NSMakeRange(varEndingPointOfString, strtostoreLength.length)];
}
else
{
[[textView textStorage] addAttribute:NSParagraphStyleAttributeName value:paragraphStyleNormal range:NSMakeRange(varEndingPointOfString, strtostoreLength.length)];
}
varEndingPointOfString = varStaringPointOfString;
strtostoreLength =#"";
}
but from this the speed of typing is become very slow.
Try this:
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstHeadLineHeadIndent:firstLineIndend]; //Only the first line
[paragraphStyle setHeadIndent:headIndent]; //The rest of the lines, except the first one
[yourAttributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:paragraphRange];
For the bullet point, that's something different. You need to find where are the bullet point, and set another indent accordingly.

Unable to calculate height of text area in iOS

I am using the ios library UIPlaceHolderTextView in my application: https://github.com/JohnnyZeng/UIPlaceHolderTextView which is linked to the following question:
Placeholder in UITextView
The library itself is working fine, however, I need to calculate the height of the area after the user has entered text, and unfortunately, I am always getting a value of 0.00000 despite the fact that the text area contains text. Why is this? Here is my relevant code:
//self.textView is a reference to a UIPlaceHolderTextView object, and text is its attribute
self.textView.text = #"Text that the user has entered, and can be of any length";
float height = MIN(175, [self.textView.attributedText boundingRectWithSize:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:NULL].size.height);
NSLog(#"textview height is: %f", self.textView.frame.origin.y);
NSLog(#"textview height is: %f", self.textView.frame.size.height);
The output in both lines are 0.00000. Can anyone see why this is happening? My guess is that the problem lies in the expression self.textView.attributedText, but not sure if I'm correct, and if so, what I should do instead.
you are assigning
`self.textView.text = #"Text that the user has entered, and can be of any length";`
and calculating heightfor self.textView.attributedText which is nil i guess please do appropriate changes. It should work.
To make is work you should assign value to self.textView.attributedText
Use a local textview of same width as real textview and set the same text with sizeToFit property.it will be returned in correct frame.
UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
//whatever alignment of real text view you have set
tv.textAlignment = NSTextAlignmentJustified;
tv.text= #"Text that the user has entered, and can be of any length";
[tv sizeToFit];
float height = tv.frame.size.height;

Resources