Get text from UITextView - iphone

Is it possible to get the first line of text from a UITextView. I have looked through the UITextView and NSString Class References and can't find any methods that could accomplish this.

The only way to accomplish this is through a process of iteration using the multi line text methods detailed in "NSString UIKit Additions Reference". Extract the first few words from your string and start compositing these gradually until the " sizeWithFont:constrainedToSize:lineBreakMode:" returns a size which height is higher than that of a line with just 1 word.

Related

IOS 11 UILabel automatic line break doesn't work when text is too long [duplicate]

This question already has answers here:
UILabel Text Not Wrapping
(8 answers)
Closed 5 years ago.
All automatic line break of UILabel was failed when I updated the XCode to 9.1. Any body same as me? I have set the attribute like below:
cell.testLabel.text = "long text here"
cell.testLabel.lineBreakMode = .byWordWrapping
cell.testLabel.numberOfLines = 0
Anybody can help me? Thanks!!!
Since iOS11, there appears to be an intentional change in how words are wrapped within an UILabel. Word wrapping mechanism was adjusted so as to make your multiline text wrapped with enhanced proportion. If you have a long text consisting of short words standing consecutively, it is likely that your text will be wrapped avoiding orphaned words, i.e. more than one word is about to be wrapped onto the next line. If the finishing word is long enough, then there might be no need to wrap extra words, because proportion may appear justified.
Example:
1.
Both words are wrapped, even though there's a room for one 'test' on the first line.
2.
Only the longest word is wrapped, because enough space is already occupied on the second line with it.
As of now, unfortunately, you are unlikely to influence it, since no additional properties were introduced to manage this behavior.
Workaround
There's a hack which can help in certain circumstances. You can replace whitespace with a non-breaking space (U+00A0). This way two consecutive words are treated as one which may cheat wrapping mechanism.

How can i add some text in uilabel in degree?

i have some text and i want to add this text to UILabel in style like this:
(x+y)³
but instead ³ some text.
How can i do this in swift, help me please?
That what you are looking for is a Mutable Attributed String. Here is a nice answer on Stackoverflow: Attributed Strings in Swift
Alternatively, you can subclass UILabel, and draw the text yourself in drawRect. If you do it this way, just add an instance variable to tell you how much of the string to draw in one format, and draw the rest in another.
Or as a obvious solution: you create another UILabel at the end of the first UILabel.

UILabel truncation " ..." (space + ...) instead of "..."

So I am trying to implement UILabel with UILineBreakModeTailTruncation.
So, for example if the text is "StackOverflow is the best website for programmers", it gets truncated as "StackOverflow is the best..."
It needs to be "StackOverflow is the best ..." (Space + ...)
Is there any easy way to implement this ??
Any easy implementations other than subclassing & overriding drawRect, and/or playing around with the frames, (if it character limit frame size, stop it and append " ...") ??
Look forward to interesting implementations !
As far as I know, Apple does not provide any API for this. You have to write your own implementation. I would calculate the width of the UILabel with the current font, see if it exceeds the maximum width, if it does, truncate text, otherwise present text normally.

Core text exactly same as UITextView text format?

I'm trying to set up a CTFrame that exactly matches my UITextView's text format in iPad.
First of all, I converted UITextView's text to an attributed string. Then I set up a width and a height of drawing box in which Core Text will draw text.
I succeeded to draw text using Core Text, but UITextView and Core Text show slightly different results even though I used the same font and size.
Specifically, when I used [UIFont systemFontOfSize:21], each space in UITextView has one more pixel than Core Text's result.
It's okay for a short sentence or word, but if UITextView and Core Text have multiple lines, their result become very different. For example, UITextView performs word-wrapping for one word at the end of line, while Core Text keeps that word in the same line. If you see the attached picture, the start positions of the last word "paragraph" are already very different (8 pixel gap due to 8 space characters).
More badly, if I use different fonts such as a custom font added to my project, each character in UITextView has 1 pixel more.
I'm using Core Text to find the pixel-position of the current cursor in UITextView, so both of them should perfectly match each other, containing the same number of characters and words in each line.
Question: Is there a way to make Core Text object that perfectly matches UITextView's text format?
Thank you!
Here's a code how I set up attributed string. (I just followed Core Text Guide.)
CTFontRef font = CTFontCreateWithName((CFStringRef) [UIFont systemFontOfSize:21.0].fontName, 21.0, NULL);
CFMutableAttributedStringRef attrString2 = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString2, CFRangeMake(0, 0), (CFStringRef) string);
CFAttributedStringSetAttribute(attrString2, CFRangeMake(0, [string length]),kCTFontAttributeName, font);
-
-
Here's a picture.
Your solution might work for a specific font at a specific point size, but you can't rely on it in general.
CoreText is simply not compatible with normal UILabels, UITextView:s or UIStringDrawing, so you can't mix them.
Either you have to use only CT functions for all string handling (including implementing custom input if that is what you need) or not use them at all.
Answer to myself.
I just found very simple solution! Using any font editor, you can just change the width of space character (ascii value 32); an original font for UITextView and a modified font for Core Text or vice versa. I used a freeware font editor FontForge. Though I still have to do some extreme-case tests such as writing Japanese characters and English alphabets in the same line and so on, now it becomes almost possible to find a pixel-position of a cursor/caret in UITextView.

How to make the 1st line in certain amount of text in a Label bold?

I have about 400 character length string with a heading called Details. In this 'Details' is to be bold and of fontsize 19. While all the remaining text starts in the next line and should be of fontSize 18 like the contents. How can I do all this by using a UILabel?
Plz help me...
You can't do it with a stock UILabel as of SDK 3.2. You can create an NSAttributedString that specifies particular styles (like bold) for particular parts of the string, but there isn't a simple way to actually render it. This is unlike In Max OS X, which has NSAttributedString(AppKitAdditions), which allows you to draw an attributed string in a single line of code. If you want to render it, you can, but you'll have to delve into one of the lower level APIs (such as Core Text).
You're probably much better off using two UILabels. Make the first one bold, and make the second one cover multiple lines.
Since UILabel does not support attributed strings, you have two options:
Subclass UILabel and override -drawRect:
Create 2 UILabels, one of which will display the heading, the other showing the rest of your text. You can choose to wrap the two labels into your own view if necessary.
You could use a UIWebView with an HTMLString of <strong>First part</strong> second part