Add CALayer to UILabel text - ios

Is there a way to add extra CAShapeLayer( a small box) to uilalbel text. I want it to be placed exactly after the last character of label.
Thank you!
some what like this. an extra layer near to the end of label

If you don't want to go into complexity , try unicode characters & append it in your string with some space if required. You can check in Interface builder. Select label. Go into Edit -> Emoji & Symbols & select dot .Check in attached screenshot.
EDIT
Find position of last character in UILabel text & add CAShapeLayer as subview.It should work.
OR
You can simplify your solution by having 1 UIView with label & CAShapeLayer as subviews.Set origin of layer with label width .

Related

minimum number of lines UILabel in iOS development

I was trying to make a cell in uicollectionview. please see these two images
Thats the cell of a collectionview and the first uilabel's number of line is 2. what I am trying to do is the uilabel will always take 2 lines of height regardless of the content of the uilabel, is it possible? if the text is short, second line will be blank.
If I add height constraint, the text is vertically centred. How to make it top-aligned?
There are 2 steps.
First select your label, click the add new constraints, and check the height constraints, as grow4gaurav said.
Next, go to the attributes inspector and set the number of lines to 0. This makes it so that the text uses as many lines as it wants. So, if the text is short, and it only uses one line, it will just use the top line. If it is longer, it will use the bottom line too.
Hope this helps

UIButton with two titles one on left another on right

I have a situation where a button title has two parts with different strings, and need to display one on the right and one on the left. Text will change dynamically every time based on response. I have to place this button in a tableview cell. Attaching an image below for reference.
The requirement is as follows:
.
But I managed to do the following:
I am unable to place text on two different sides of a button.
The easiest solution for you may be to place a UILabel on the left and anchor it to the left with a constraint, then place a UILabel on the right, and anchor it to the right with a constraint. - this is mentioned in the comments by #Alejandro Iván.
I would then choose a set value for the right label width and set that label. Add a constraint from the left label to the right label, so it will dynamically fill as much space as is available.
Last I would place a clear, no text button with the border on top, wrapping both labels. You can then confidently change the left label content and know that it will be encompassed in the button border.
Image attached to help you see what I'm saying although I've left the "button" text and added coloration to demonstrate.

How to concatenate UIButton along with UIText

I want to add a UIButton at the end of a sentence in UIView.
Presently I have a UILabel to display text and a button next to it. Both are subviews to UIView. I get the desired o/p if the sentence spans to 2 lines.
How should I modify it, for it to work properly even if the text changes to 3 or 4 lines given that I cannot change the size of the UIView or the UILabel?
It's not really clear to me what you are trying to achieve... anyway maybe with autolayout you could solve your problem:
The button will be placed always on the bottom right corner of the label and the width of the label itself will span from 42 to the max available space...

Customize iOS UITextView text

In my iOS application i have an UITextView in which i insert some text downloaded from the web. This text is not editable/selectable by the user and it's a sort of preview of the whole text downloaded.
So, i want to show only the first two line of the text and i want to have always a vertical centered alignment. The text can also have only one line and, if there are more than two lines or there is a very long line i want to put these ".." at the end of the visible text.
I want something like this:
I hope i explained myself.
If it is only a preview of the entire text, you can use a UILabel instead. The UILabel will have a fixed size and it will automatically truncate the text at the end and add the "..." you want. Don't forget to specify the numberOfLines property of UILabel to be 2 and the textAlignment property to NSTextAlignmentCenter if you want it centered.

UITextView word wrap breaks in the middle of a word

I'm using a UITextView to present definitions which may be one or more lines long. When it does go past a lane, if often wraps in the middle of a word.
Is this normal? What can be done to prevent this?
word wrap IS the default for IOS UITextView and the only reasons it fails Quuxplusone mentioned in his comment.
EITHER
the one word is longer (in pixels) than the textView's frame
the 'spaces' aren't really spaces but unbreakable spaces
It appears that this can occur when exclusion paths are too close to the edge of the UITextView. In this example accompanying this tutorial you'll notice that if you run the app and select the Interaction tab that while the oval is placed centrally words hyphenate and break naturally, if you drag it to the left edge then words break forcibly and are fragmented. But if you drag the oval half off the screen then wrapping works.
So when working with ovals you can use the trick of going off the left edge, and with rectangles, just don't get too close to the edge. Being up against it is fine.
You can do it as :
UITextview having a property of enable scrolling. So the text characters limit doesn't matters. If there is more text, the textview make it scrollable.
OR
Make the textview height dynamic, get the string/text height and set the height of textview accordingly.

Resources